Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
beyaz committed Jan 29, 2024
1 parent 3164336 commit 8fe2f4d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 49 deletions.
50 changes: 6 additions & 44 deletions ReactWithDotNet/JsonSerializationOptionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -370,46 +370,14 @@ class ConverterForCompilerGeneratedClass : JsonConverter<object>
{
public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType != JsonTokenType.StartObject)
{
throw new JsonException();
}
var dictionary = JsonSerializer.Deserialize<Dictionary<string, object>>(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)
Expand All @@ -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);
}
}
}
Expand Down
17 changes: 12 additions & 5 deletions ReactWithDotNet/ReflectionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,18 @@ public static IReadOnlyDictionary<string, object> Serialize(object compilerGener
{
var compilerGeneratedType = compilerGeneratedTypeInstance.GetType();

return compilerGeneratedType.GetFields().Select(toNameValuePair).ToDictionary(x => x.name, v => v.value);
var dictionary = new Dictionary<string, object>();

(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)
{
Expand All @@ -301,7 +306,9 @@ public static IReadOnlyDictionary<string, object> Serialize(object compilerGener
}
}

return (name, value);
dictionary.Add(name, value);
}

return dictionary;
}
}

0 comments on commit 8fe2f4d

Please sign in to comment.