|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Concurrent; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Linq; |
| 6 | +using System.Reflection; |
| 7 | +using System.Text.Json; |
| 8 | +using System.Text.Json.Serialization; |
| 9 | + |
| 10 | +namespace Exceptionless.Serializer { |
| 11 | + |
| 12 | + internal sealed class DictionaryConverterFactory : JsonConverterFactory { |
| 13 | + private readonly static ConcurrentDictionary<Type, JsonConverter> s_lookUp = new(); |
| 14 | + public override bool CanConvert(Type typeToConvert) |
| 15 | + => typeToConvert.IsGenericType && |
| 16 | + typeToConvert.GetGenericArguments()[0] == typeof(string) && |
| 17 | + IsAssignableToGenericType(typeToConvert, typeof(IDictionary<,>)); |
| 18 | + |
| 19 | + public static bool IsAssignableToGenericType(Type givenType, Type genericType) { |
| 20 | + var interfaceTypes = givenType.GetInterfaces(); |
| 21 | + |
| 22 | + foreach (var it in interfaceTypes) { |
| 23 | + if (it.IsGenericType && it.GetGenericTypeDefinition() == genericType) |
| 24 | + return true; |
| 25 | + } |
| 26 | + |
| 27 | + if (givenType.IsGenericType && givenType.GetGenericTypeDefinition() == genericType) |
| 28 | + return true; |
| 29 | + |
| 30 | + Type baseType = givenType.BaseType; |
| 31 | + if (baseType == null) return false; |
| 32 | + |
| 33 | + return IsAssignableToGenericType(baseType, genericType); |
| 34 | + } |
| 35 | + |
| 36 | + public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) { |
| 37 | + var lookUp = s_lookUp; |
| 38 | + if (lookUp.TryGetValue(typeToConvert, out var value)) return value; |
| 39 | + |
| 40 | + var genericArgs = typeToConvert.GetGenericArguments(); |
| 41 | + Type elementType = genericArgs[1]; |
| 42 | + |
| 43 | + JsonConverter converter = (JsonConverter)Activator.CreateInstance( |
| 44 | + typeof(DictionaryConverter<>) |
| 45 | + .MakeGenericType(new Type[] { elementType }), |
| 46 | + BindingFlags.Instance | BindingFlags.Public, |
| 47 | + binder: null, |
| 48 | + args: null, |
| 49 | + culture: null)!; |
| 50 | + |
| 51 | + lookUp[typeToConvert] = converter; |
| 52 | + return converter; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + internal sealed class DictionaryConverter<TValue> : MaxDepthJsonConverter<IDictionary<string, TValue>> { |
| 57 | + public override void Write(Utf8JsonWriter writer, IDictionary<string, TValue> value, JsonSerializerOptions options) { |
| 58 | + // Skip empty collections |
| 59 | + if (value is null || value.Count == 0) return; |
| 60 | + |
| 61 | + writer.WriteStartObject(); |
| 62 | + |
| 63 | + var namingPolicy = options.PropertyNamingPolicy; |
| 64 | + var naming = namingPolicy as SnakeCaseNamingPolicy; |
| 65 | + |
| 66 | + foreach (var kv in value) { |
| 67 | + if (!naming?.IsNameAllowed(kv.Key) ?? true) continue; |
| 68 | + |
| 69 | + // We don't rename dictionary keys |
| 70 | + writer.WritePropertyName(kv.Key); |
| 71 | + |
| 72 | + JsonSerializer.Serialize(writer, kv.Value, options); |
| 73 | + } |
| 74 | + |
| 75 | + writer.WriteEndObject(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + internal sealed class MaxDepthJsonConverterFactory : JsonConverterFactory { |
| 81 | + private readonly static ConcurrentDictionary<Type, JsonConverter> s_lookUp = new(); |
| 82 | + |
| 83 | + public override bool CanConvert(Type typeToConvert) |
| 84 | + => !typeToConvert.IsValueType && |
| 85 | + typeToConvert != typeof(object) && |
| 86 | + !typeToConvert.IsArray && |
| 87 | + typeToConvert != typeof(string) && |
| 88 | + !(typeToConvert.IsGenericType && typeof(IEnumerable<>).IsAssignableFrom(typeToConvert.GetGenericTypeDefinition())) && |
| 89 | + !typeof(IEnumerable).IsAssignableFrom(typeToConvert); |
| 90 | + |
| 91 | + public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) { |
| 92 | + var lookUp = s_lookUp; |
| 93 | + if (lookUp.TryGetValue(typeToConvert, out var value)) return value; |
| 94 | + |
| 95 | + JsonConverter converter = (JsonConverter)Activator.CreateInstance( |
| 96 | + typeof(MaxDepthJsonConverter<>) |
| 97 | + .MakeGenericType(new Type[] { typeToConvert }), |
| 98 | + BindingFlags.Instance | BindingFlags.Public, |
| 99 | + binder: null, |
| 100 | + args: null, |
| 101 | + culture: null)!; |
| 102 | + |
| 103 | + lookUp[typeToConvert] = converter; |
| 104 | + return converter; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + internal class MaxDepthJsonConverter { |
| 109 | + private readonly static ConcurrentDictionary<Type, (FieldInfo[], PropertyInfo[])> s_lookUp = new(); |
| 110 | + |
| 111 | + public static (FieldInfo[], PropertyInfo[]) GetFieldsAndProperties(Type type) { |
| 112 | + var lookUp = s_lookUp; |
| 113 | + if (lookUp.TryGetValue(type, out var value)) return value; |
| 114 | + |
| 115 | + value = ( |
| 116 | + type.GetFields(BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Instance).ToArray(), |
| 117 | + type.GetProperties(BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Instance).Where(p => p.GetIndexParameters().Length == 0).ToArray() |
| 118 | + ); |
| 119 | + |
| 120 | + lookUp[type] = value; |
| 121 | + return value; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + internal class MaxDepthJsonConverter<T> : JsonConverter<T> { |
| 126 | + public sealed override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { |
| 127 | + throw new NotImplementedException(); |
| 128 | + } |
| 129 | + |
| 130 | + public override void Write(Utf8JsonWriter writer, T objectValue, JsonSerializerOptions options) { |
| 131 | + writer.WriteStartObject(); |
| 132 | + |
| 133 | + var (fields, properties) = MaxDepthJsonConverter.GetFieldsAndProperties(objectValue.GetType()); |
| 134 | + |
| 135 | + var namingPolicy = options.PropertyNamingPolicy; |
| 136 | + for (int i = 0; i < fields.Length; i++) { |
| 137 | + var field = fields[i]; |
| 138 | + var type = field.FieldType; |
| 139 | + if (type.IsClass && !type.IsArray && type != typeof(string) && writer.CurrentDepth >= options.MaxDepth - DefaultJsonSerializer.MaxDepthBuffer) { |
| 140 | + continue; |
| 141 | + } |
| 142 | + var value = field.GetValueDirect(__makeref(objectValue)); |
| 143 | + if (value is ICollection col && col.Count == 0) { |
| 144 | + continue; |
| 145 | + } |
| 146 | + // TODO: Attribute for name? |
| 147 | + if (namingPolicy is SnakeCaseNamingPolicy naming && !naming.IsNameAllowed(field.Name)) { |
| 148 | + continue; |
| 149 | + } |
| 150 | + |
| 151 | + writer.WritePropertyName(namingPolicy.ConvertName(field.Name)); |
| 152 | + |
| 153 | + JsonSerializer.Serialize(writer, value, options); |
| 154 | + } |
| 155 | + |
| 156 | + for (int i = 0; i < properties.Length; i++) { |
| 157 | + var property = properties[i]; |
| 158 | + var type = property.PropertyType; |
| 159 | + if (!type.IsValueType && !type.IsArray && type != typeof(string) && writer.CurrentDepth >= options.MaxDepth - DefaultJsonSerializer.MaxDepthBuffer) { |
| 160 | + continue; |
| 161 | + } |
| 162 | + var value = property.GetValue(objectValue); |
| 163 | + if (value is ICollection col && col.Count == 0) { |
| 164 | + continue; |
| 165 | + } |
| 166 | + // TODO: Attribute for name? |
| 167 | + if (namingPolicy is SnakeCaseNamingPolicy naming && !naming.IsNameAllowed(property.Name)) { |
| 168 | + continue; |
| 169 | + } |
| 170 | + |
| 171 | + writer.WritePropertyName(namingPolicy.ConvertName(property.Name)); |
| 172 | + JsonSerializer.Serialize(writer, value, options); |
| 173 | + } |
| 174 | + |
| 175 | + writer.WriteEndObject(); |
| 176 | + } |
| 177 | + } |
| 178 | +} |
0 commit comments