Skip to content

Commit

Permalink
Simplify ByteEnumerableJsonConverter
Browse files Browse the repository at this point in the history
  • Loading branch information
Windows10CE committed Nov 13, 2024
1 parent f9196a8 commit 3a34032
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 13 deletions.
2 changes: 1 addition & 1 deletion CSharpRepl/Eval/ResultModels/EvalResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public override string ToString()
new DirectoryInfoJsonConverter(),
new AngouriMathEntityConverter(), new AngouriMathEntityVarsConverter(),
new NumberConverter(),
new ByteEnumerableConverterFactory(),
new ByteEnumerableJsonConverter(),
new MultidimArrayConverterFactory()
}
});
Expand Down
15 changes: 5 additions & 10 deletions CSharpRepl/Infrastructure/JsonFormatters/TypeJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,22 +174,17 @@ public override void Write(Utf8JsonWriter writer, DirectoryInfo value, JsonSeria
writer.WriteStringValue(value.FullName);
}
}

public class ByteEnumerableConverterFactory : JsonConverterFactory
{
public override bool CanConvert(Type typeToConvert) => typeToConvert.GetInterfaces().Contains(typeof(IEnumerable<byte>));
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options)
=> (JsonConverter)Activator.CreateInstance(typeof(ByteEnumerableJsonConverter<>).MakeGenericType(typeToConvert));
}

public class ByteEnumerableJsonConverter<T> : JsonConverter<T> where T : IEnumerable<byte>
public class ByteEnumerableJsonConverter : JsonConverter<IEnumerable<byte>>
{
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
public override bool CanConvert(Type typeToConvert) => typeToConvert.GetInterfaces().Contains(typeof(IEnumerable<byte>));

public override IEnumerable<byte> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotImplementedException();
}

public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
public override void Write(Utf8JsonWriter writer, IEnumerable<byte> value, JsonSerializerOptions options)
{
((JsonConverter<IEnumerable<int>>)options.GetConverter(typeof(IEnumerable<int>))).Write(writer, value.Select(int (x) => x), options);
}
Expand Down
4 changes: 2 additions & 2 deletions CSharpRepl/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void ConfigureServices(IServiceCollection services)
new DirectoryInfoJsonConverter(),
new AngouriMathEntityConverter(), new AngouriMathEntityVarsConverter(),
new NumberConverter(),
new ByteEnumerableConverterFactory(),
new ByteEnumerableJsonConverter(),
new MultidimArrayConverterFactory()
}
};
Expand Down Expand Up @@ -72,7 +72,7 @@ public void ConfigureServices(IServiceCollection services)
o.JsonSerializerOptions.Converters.Add(new AngouriMathEntityConverter());
o.JsonSerializerOptions.Converters.Add(new AngouriMathEntityVarsConverter());
o.JsonSerializerOptions.Converters.Add(new NumberConverter());
o.JsonSerializerOptions.Converters.Add(new ByteEnumerableConverterFactory());
o.JsonSerializerOptions.Converters.Add(new ByteEnumerableJsonConverter());
o.JsonSerializerOptions.Converters.Add(new MultidimArrayConverterFactory());
});
services.AddSingleton(jsonOptions);
Expand Down

0 comments on commit 3a34032

Please sign in to comment.