JsonDefaults.cs 3.3 KB

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