2
0

JsonDefaults.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. /// Gets the default <see cref="JsonSerializerOptions" /> options.
  13. /// </summary>
  14. /// <remarks>
  15. /// When changing these options, update
  16. /// Jellyfin.Server/Extensions/ApiServiceCollectionExtensions.cs
  17. /// -> AddJellyfinApi
  18. /// -> AddJsonOptions
  19. /// </remarks>
  20. /// <returns>The default <see cref="JsonSerializerOptions" /> options.</returns>
  21. public static JsonSerializerOptions GetOptions()
  22. {
  23. var options = new JsonSerializerOptions
  24. {
  25. ReadCommentHandling = JsonCommentHandling.Disallow,
  26. WriteIndented = false
  27. };
  28. options.Converters.Add(new JsonGuidConverter());
  29. options.Converters.Add(new JsonStringEnumConverter());
  30. options.Converters.Add(new JsonNonStringKeyDictionaryConverterFactory());
  31. return options;
  32. }
  33. /// <summary>
  34. /// Gets CamelCase json options.
  35. /// </summary>
  36. public static JsonSerializerOptions CamelCase
  37. {
  38. get
  39. {
  40. var options = GetOptions();
  41. options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
  42. return options;
  43. }
  44. }
  45. /// <summary>
  46. /// Gets PascalCase json options.
  47. /// </summary>
  48. public static JsonSerializerOptions PascalCase
  49. {
  50. get
  51. {
  52. var options = GetOptions();
  53. options.PropertyNamingPolicy = null;
  54. return options;
  55. }
  56. }
  57. }
  58. }