| | | 1 | | using System.Text.Json; |
| | | 2 | | using System.Text.Json.Serialization; |
| | | 3 | | using System.Text.Json.Serialization.Metadata; |
| | | 4 | | using Jellyfin.Extensions.Json.Converters; |
| | | 5 | | |
| | | 6 | | namespace Jellyfin.Extensions.Json |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// Helper class for having compatible JSON throughout the codebase. |
| | | 10 | | /// </summary> |
| | | 11 | | public static class JsonDefaults |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Pascal case json profile media type. |
| | | 15 | | /// </summary> |
| | | 16 | | public const string PascalCaseMediaType = "application/json; profile=\"PascalCase\""; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Camel case json profile media type. |
| | | 20 | | /// </summary> |
| | | 21 | | public const string CamelCaseMediaType = "application/json; profile=\"CamelCase\""; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// When changing these options, update |
| | | 25 | | /// Jellyfin.Server/Extensions/ApiServiceCollectionExtensions.cs |
| | | 26 | | /// -> AddJellyfinApi |
| | | 27 | | /// -> AddJsonOptions. |
| | | 28 | | /// </summary> |
| | 5 | 29 | | private static readonly JsonSerializerOptions _jsonSerializerOptions = new() |
| | 5 | 30 | | { |
| | 5 | 31 | | ReadCommentHandling = JsonCommentHandling.Disallow, |
| | 5 | 32 | | WriteIndented = false, |
| | 5 | 33 | | DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, |
| | 5 | 34 | | NumberHandling = JsonNumberHandling.AllowReadingFromString, |
| | 5 | 35 | | Converters = |
| | 5 | 36 | | { |
| | 5 | 37 | | new JsonGuidConverter(), |
| | 5 | 38 | | new JsonNullableGuidConverter(), |
| | 5 | 39 | | new JsonVersionConverter(), |
| | 5 | 40 | | new JsonFlagEnumConverterFactory(), |
| | 5 | 41 | | new JsonDefaultStringEnumConverterFactory(), |
| | 5 | 42 | | new JsonStringEnumConverter(), |
| | 5 | 43 | | new JsonNullableStructConverterFactory(), |
| | 5 | 44 | | new JsonDateTimeConverter(), |
| | 5 | 45 | | new JsonStringConverter() |
| | 5 | 46 | | }, |
| | 5 | 47 | | TypeInfoResolver = new DefaultJsonTypeInfoResolver() |
| | 5 | 48 | | }; |
| | | 49 | | |
| | 5 | 50 | | private static readonly JsonSerializerOptions _pascalCaseJsonSerializerOptions = new(_jsonSerializerOptions) |
| | 5 | 51 | | { |
| | 5 | 52 | | PropertyNamingPolicy = null |
| | 5 | 53 | | }; |
| | | 54 | | |
| | 5 | 55 | | private static readonly JsonSerializerOptions _camelCaseJsonSerializerOptions = new(_jsonSerializerOptions) |
| | 5 | 56 | | { |
| | 5 | 57 | | PropertyNamingPolicy = JsonNamingPolicy.CamelCase |
| | 5 | 58 | | }; |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Gets the default <see cref="JsonSerializerOptions" /> options. |
| | | 62 | | /// </summary> |
| | | 63 | | /// <remarks> |
| | | 64 | | /// The return value must not be modified. |
| | | 65 | | /// If the defaults must be modified the author must use the copy constructor. |
| | | 66 | | /// </remarks> |
| | | 67 | | /// <returns>The default <see cref="JsonSerializerOptions" /> options.</returns> |
| | | 68 | | public static JsonSerializerOptions Options |
| | 1713 | 69 | | => _jsonSerializerOptions; |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Gets camelCase json options. |
| | | 73 | | /// </summary> |
| | | 74 | | /// <remarks> |
| | | 75 | | /// The return value must not be modified. |
| | | 76 | | /// If the defaults must be modified the author must use the copy constructor. |
| | | 77 | | /// </remarks> |
| | | 78 | | /// <returns>The camelCase <see cref="JsonSerializerOptions" /> options.</returns> |
| | | 79 | | public static JsonSerializerOptions CamelCaseOptions |
| | 21 | 80 | | => _camelCaseJsonSerializerOptions; |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Gets PascalCase json options. |
| | | 84 | | /// </summary> |
| | | 85 | | /// <remarks> |
| | | 86 | | /// The return value must not be modified. |
| | | 87 | | /// If the defaults must be modified the author must use the copy constructor. |
| | | 88 | | /// </remarks> |
| | | 89 | | /// <returns>The PascalCase <see cref="JsonSerializerOptions" /> options.</returns> |
| | | 90 | | public static JsonSerializerOptions PascalCaseOptions |
| | 42 | 91 | | => _pascalCaseJsonSerializerOptions; |
| | | 92 | | } |
| | | 93 | | } |