JsonDefaults.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. new JsonStringConverter()
  42. }
  43. };
  44. private static readonly JsonSerializerOptions _pascalCaseJsonSerializerOptions = new (_jsonSerializerOptions)
  45. {
  46. PropertyNamingPolicy = null
  47. };
  48. private static readonly JsonSerializerOptions _camelCaseJsonSerializerOptions = new (_jsonSerializerOptions)
  49. {
  50. PropertyNamingPolicy = JsonNamingPolicy.CamelCase
  51. };
  52. /// <summary>
  53. /// Gets the default <see cref="JsonSerializerOptions" /> options.
  54. /// </summary>
  55. /// <remarks>
  56. /// The return value must not be modified.
  57. /// If the defaults must be modified the author must use the copy constructor.
  58. /// </remarks>
  59. /// <returns>The default <see cref="JsonSerializerOptions" /> options.</returns>
  60. public static JsonSerializerOptions GetOptions()
  61. => _jsonSerializerOptions;
  62. /// <summary>
  63. /// Gets camelCase json options.
  64. /// </summary>
  65. /// <remarks>
  66. /// The return value must not be modified.
  67. /// If the defaults must be modified the author must use the copy constructor.
  68. /// </remarks>
  69. /// <returns>The camelCase <see cref="JsonSerializerOptions" /> options.</returns>
  70. public static JsonSerializerOptions GetCamelCaseOptions()
  71. => _camelCaseJsonSerializerOptions;
  72. /// <summary>
  73. /// Gets PascalCase json options.
  74. /// </summary>
  75. /// <remarks>
  76. /// The return value must not be modified.
  77. /// If the defaults must be modified the author must use the copy constructor.
  78. /// </remarks>
  79. /// <returns>The PascalCase <see cref="JsonSerializerOptions" /> options.</returns>
  80. public static JsonSerializerOptions GetPascalCaseOptions()
  81. => _pascalCaseJsonSerializerOptions;
  82. }
  83. }