diff --git a/ReactWithDotNet/JsonSerializationOptionHelper.cs b/ReactWithDotNet/JsonSerializationOptionHelper.cs index 31775b35..b05bf53a 100644 --- a/ReactWithDotNet/JsonSerializationOptionHelper.cs +++ b/ReactWithDotNet/JsonSerializationOptionHelper.cs @@ -370,46 +370,14 @@ class ConverterForCompilerGeneratedClass : JsonConverter { public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { - if (reader.TokenType != JsonTokenType.StartObject) - { - throw new JsonException(); - } + var dictionary = JsonSerializer.Deserialize>(ref reader, options); - var obj = Activator.CreateInstance(typeToConvert); - - while (reader.Read()) + if (dictionary is null) { - if (reader.TokenType == JsonTokenType.EndObject) - { - return obj; - } - - if (reader.TokenType != JsonTokenType.PropertyName) - { - throw new JsonException(); - } - - var fieldName = reader.GetString(); - if (fieldName == null) - { - throw new JsonException(); - } - - var fieldInfo = typeToConvert.GetField(fieldName); - - if (fieldInfo == null) - { - throw new JsonException($"{fieldName} not deserialized."); - } - - reader.Read(); - - var fieldValue = JsonSerializer.Deserialize(ref reader, fieldInfo.FieldType, options); - - fieldInfo.SetValue(obj, fieldValue); + return null; } - throw new JsonException(); + return SerializationHelperForCompilerGeneratedClasss.Deserialize(typeToConvert, dictionary); } public override void Write(Utf8JsonWriter writer, object obj, JsonSerializerOptions options) @@ -420,15 +388,9 @@ public override void Write(Utf8JsonWriter writer, object obj, JsonSerializerOpti return; } - writer.WriteStartObject(); - - foreach (var fieldInfo in obj.GetType().GetFields()) - { - writer.WritePropertyName(fieldInfo.Name); - JsonSerializer.Serialize(writer, fieldInfo.GetValue(obj), options); - } + var dictionary = SerializationHelperForCompilerGeneratedClasss.Serialize(obj); - writer.WriteEndObject(); + JsonSerializer.Serialize(writer, dictionary, options); } } } diff --git a/ReactWithDotNet/ReflectionHelper.cs b/ReactWithDotNet/ReflectionHelper.cs index 7ee552af..ada3e8b8 100644 --- a/ReactWithDotNet/ReflectionHelper.cs +++ b/ReactWithDotNet/ReflectionHelper.cs @@ -284,13 +284,18 @@ public static IReadOnlyDictionary Serialize(object compilerGener { var compilerGeneratedType = compilerGeneratedTypeInstance.GetType(); - return compilerGeneratedType.GetFields().Select(toNameValuePair).ToDictionary(x => x.name, v => v.value); + var dictionary = new Dictionary(); - (string name, object value) toNameValuePair(FieldInfo f) + foreach (var fieldInfo in compilerGeneratedType.GetFields()) { - var name = f.Name; + var name = fieldInfo.Name; - var value = f.GetValue(compilerGeneratedTypeInstance); + var value = fieldInfo.GetValue(compilerGeneratedTypeInstance); + + if (value == compilerGeneratedTypeInstance) + { + continue; + } if (value is MulticastDelegate multicastDelegate) { @@ -301,7 +306,9 @@ public static IReadOnlyDictionary Serialize(object compilerGener } } - return (name, value); + dictionary.Add(name, value); } + + return dictionary; } } \ No newline at end of file