Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A bunch of random improvements #58

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Simplify ByteEnumerableJsonConverter
  • Loading branch information
Windows10CE committed Nov 13, 2024
commit 3a340321d9e184342331810f443c09354629aa91
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these should be split into separate files at this point.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do that, was just leaving it as I found it originally

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think we should for the sake of maintainability for now.

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
Loading