1
0

UniversalJsonConverter.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System.Reflection;
  2. using System.Text.Json;
  3. using System.Text.Json.Serialization;
  4. namespace Content.Server.GuideGenerator
  5. {
  6. // This class is used as a shim to help do polymorphic serialization of objects into JSON
  7. // (serializing objects that inherit abstract base classes or interfaces) since
  8. // System.Text.Json (our new JSON solution) doesn't support that while Newtonsoft.Json (our old
  9. // solution) does.
  10. public sealed class UniversalJsonConverter<T> : JsonConverter<T>
  11. {
  12. // This converter can only convert types that are T or descend from T.
  13. public override bool CanConvert(Type typeToConvert)
  14. {
  15. return typeof(T).IsAssignableFrom(typeToConvert);
  16. }
  17. // We don't support deserialization right now. In order to do so, we'd need to bundle a
  18. // field like "$type" with our objects so they'd be reserialized into the correct base class
  19. // but that presents a security hazard.
  20. public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
  21. {
  22. // Throwing a NotImplementedException here allows the Utf8JsonReader to provide
  23. // an error message that provides the specific JSON path of the problematic object
  24. // rather than a generic error message. At least in theory. Haven't tested that.
  25. throw new NotImplementedException();
  26. }
  27. // The bread and butter. Deserialize an object of parameter type T.
  28. // This method is automatically called when the JSON writer finds an object of a type
  29. // where we've registered this class as its converter using the [JsonConverter(...)] attribute
  30. public override void Write(Utf8JsonWriter writer, T obj, JsonSerializerOptions options)
  31. {
  32. // If the object is null, don't include it.
  33. if (obj is null)
  34. {
  35. writer.WriteNullValue();
  36. return;
  37. }
  38. // Use reflection to get a list of fields and properties on the object we're serializing.
  39. // Using obj.GetType() here instead of typeof(T) allows us to get the true base class rather
  40. // than the abstract ancestor, even if we're parameterized with that abstract class.
  41. FieldInfo[] fields = obj.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
  42. PropertyInfo[] properties = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
  43. // Since the JSON writer will have already written the field name, we need to write the object itself.
  44. // Since we only use this class to serialize complex objects, we know we'll be writing a JSON object, so open one.
  45. writer.WriteStartObject();
  46. // For each field, try to write it into the object.
  47. foreach (FieldInfo field in fields)
  48. {
  49. // If the field has a [JsonIgnore] attribute, skip it
  50. if (Attribute.GetCustomAttribute(field, typeof(JsonIgnoreAttribute), true) != null) continue;
  51. // exclude fields that are compiler autogenerated like "__BackingField" fields
  52. if (Attribute.GetCustomAttribute(field, typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute), true) != null) continue;
  53. // If the field has a [JsonPropertyName] attribute, get the property name. Otherwise, use the field name.
  54. JsonPropertyNameAttribute? attr = (JsonPropertyNameAttribute?) Attribute.GetCustomAttribute(field, typeof(JsonPropertyNameAttribute), true);
  55. string name = attr == null ? field.Name : attr.Name;
  56. // Write a new key/value pair into the JSON object itself.
  57. WriteKV(writer, name, field.GetValue(obj), options);
  58. }
  59. // Repeat the same process for each property.
  60. foreach (PropertyInfo prop in properties)
  61. {
  62. // If the field has a [JsonIgnore] attribute, skip it
  63. if (Attribute.GetCustomAttribute(prop, typeof(JsonIgnoreAttribute), true) != null) continue;
  64. // If the property has a [JsonPropertyName] attribute, get the property name. Otherwise, use the property name.
  65. JsonPropertyNameAttribute? attr = (JsonPropertyNameAttribute?) Attribute.GetCustomAttribute(prop, typeof(JsonPropertyNameAttribute), true);
  66. string name = attr == null ? prop.Name : attr.Name;
  67. // Write a new key/value pair into the JSON object itself.
  68. WriteKV(writer, name, prop.GetValue(obj), options);
  69. }
  70. // Close the object, we're done!
  71. writer.WriteEndObject();
  72. }
  73. // This is a little utility method to write a key/value pair inside a JSON object.
  74. // It's used for all the actual writing.
  75. public void WriteKV(Utf8JsonWriter writer, string key, object? obj, JsonSerializerOptions options)
  76. {
  77. // First, write the property name
  78. writer.WritePropertyName(key);
  79. // Then, recurse. This ensures that primitive values will be written directly, while
  80. // more complex values can use any custom converters we've registered (like this one.)
  81. JsonSerializer.Serialize(writer, obj, options);
  82. }
  83. }
  84. }