aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Common/Json/JsonDefaults.cs
blob: 67c3dfe546f97baee9da145c7e7d3873c8a60283 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System.Text.Json;
using System.Text.Json.Serialization;
using MediaBrowser.Common.Json.Converters;

namespace MediaBrowser.Common.Json
{
    /// <summary>
    /// Helper class for having compatible JSON throughout the codebase.
    /// </summary>
    public static class JsonDefaults
    {
        /// <summary>
        /// Pascal case json profile media type.
        /// </summary>
        public const string PascalCaseMediaType = "application/json; profile=\"PascalCase\"";

        /// <summary>
        /// Camel case json profile media type.
        /// </summary>
        public const string CamelCaseMediaType = "application/json; profile=\"CamelCase\"";

        /// <summary>
        /// When changing these options, update
        ///  Jellyfin.Server/Extensions/ApiServiceCollectionExtensions.cs
        ///   -> AddJellyfinApi
        ///    -> AddJsonOptions.
        /// </summary>
        private static readonly JsonSerializerOptions _jsonSerializerOptions = new ()
        {
            ReadCommentHandling = JsonCommentHandling.Disallow,
            WriteIndented = false,
            DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
            NumberHandling = JsonNumberHandling.AllowReadingFromString,
            PropertyNameCaseInsensitive = true,
            Converters =
            {
                new JsonGuidConverter(),
                new JsonVersionConverter(),
                new JsonStringEnumConverter(),
                new JsonNullableStructConverterFactory(),
                new JsonBoolNumberConverter(),
                new JsonDateTimeConverter()
            }
        };

        private static readonly JsonSerializerOptions _pascalCaseJsonSerializerOptions = new (_jsonSerializerOptions)
        {
            PropertyNamingPolicy = null
        };

        private static readonly JsonSerializerOptions _camelCaseJsonSerializerOptions = new (_jsonSerializerOptions)
        {
            PropertyNamingPolicy = JsonNamingPolicy.CamelCase
        };

        /// <summary>
        /// Gets the default <see cref="JsonSerializerOptions" /> options.
        /// </summary>
        /// <remarks>
        /// The return value must not be modified.
        /// If the defaults must be modified the author must use the copy constructor.
        /// </remarks>
        /// <returns>The default <see cref="JsonSerializerOptions" /> options.</returns>
        public static JsonSerializerOptions GetOptions()
            => _jsonSerializerOptions;

        /// <summary>
        /// Gets camelCase json options.
        /// </summary>
        /// <remarks>
        /// The return value must not be modified.
        /// If the defaults must be modified the author must use the copy constructor.
        /// </remarks>
        /// <returns>The camelCase <see cref="JsonSerializerOptions" /> options.</returns>
        public static JsonSerializerOptions GetCamelCaseOptions()
            => _camelCaseJsonSerializerOptions;

        /// <summary>
        /// Gets PascalCase json options.
        /// </summary>
        /// <remarks>
        /// The return value must not be modified.
        /// If the defaults must be modified the author must use the copy constructor.
        /// </remarks>
        /// <returns>The PascalCase <see cref="JsonSerializerOptions" /> options.</returns>
        public static JsonSerializerOptions GetPascalCaseOptions()
            => _pascalCaseJsonSerializerOptions;
    }
}