JsonDefaults.cs 3.4 KB

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