JsonDefaults.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. /// <returns>The camelCase <see cref="JsonSerializerOptions" /> options.</returns>
  37. public static JsonSerializerOptions GetCamelCaseOptions()
  38. {
  39. var options = GetOptions();
  40. options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
  41. return options;
  42. }
  43. /// <summary>
  44. /// Gets PascalCase json options.
  45. /// </summary>
  46. /// <returns>The PascalCase <see cref="JsonSerializerOptions" /> options.</returns>
  47. public static JsonSerializerOptions GetPascalCaseOptions()
  48. {
  49. var options = GetOptions();
  50. options.PropertyNamingPolicy = null;
  51. return options;
  52. }
  53. }
  54. }