JsonDefaults.cs 3.4 KB

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