Skip to content

Commit

Permalink
Fix named floating point JSON serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Windows10CE committed Jul 1, 2023
1 parent 7be9aa5 commit d67d0b7
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 2 deletions.
1 change: 1 addition & 0 deletions CSharpRepl.Tests/EvalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public EvalTests(ITestOutputHelper outputHelper)
[InlineData("typeof(int)", "System.Int32, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e", "RuntimeType")]
[InlineData("Assembly.GetExecutingAssembly()", true, "RuntimeAssembly")]
[InlineData("TimeSpan.FromSeconds(2310293892)", "26739.12:18:12", "TimeSpan")]
[InlineData("float.PositiveInfinity", "Infinity", "float")]
public async Task Eval_WellFormattedCodeExecutes(string expr, object expected, string type)
{
var (result, statusCode) = await Execute(expr);
Expand Down
5 changes: 3 additions & 2 deletions CSharpRepl/Eval/DisassemblyService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public string GetIl(string code)
namespace Eval
{{
public class Code
public unsafe class Code
{{
public object Main()
{{
Expand All @@ -82,7 +82,8 @@ public object Main()
var scriptSyntaxTree = CSharpSyntaxTree.ParseText(toExecute, opts);
var compOpts = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
.WithOptimizationLevel(OptimizationLevel.Debug)
.WithAllowUnsafe(true).WithPlatform(Platform.AnyCpu);
.WithAllowUnsafe(true)
.WithPlatform(Platform.AnyCpu);

var compilation = CSharpCompilation.Create(Guid.NewGuid().ToString(), options: compOpts, references: References)
.AddSyntaxTrees(scriptSyntaxTree);
Expand Down
1 change: 1 addition & 0 deletions CSharpRepl/Eval/ResultModels/EvalResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public override string ToString()
IncludeFields = true,
PropertyNameCaseInsensitive = true,
ReferenceHandler = ReferenceHandler.IgnoreCycles,
NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals,
Converters = {
new TypeJsonConverter(), new TypeInfoJsonConverter(),
new RuntimeTypeHandleJsonConverter(), new TypeJsonConverterFactory(), new AssemblyJsonConverter(),
Expand Down
2 changes: 2 additions & 0 deletions CSharpRepl/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public void ConfigureServices(IServiceCollection services)
IncludeFields = true,
PropertyNameCaseInsensitive = true,
ReferenceHandler = ReferenceHandler.IgnoreCycles,
NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals,
Converters = {
new TypeJsonConverter(), new TypeInfoJsonConverter(),
new RuntimeTypeHandleJsonConverter(), new TypeJsonConverterFactory(), new AssemblyJsonConverter(),
Expand All @@ -57,6 +58,7 @@ public void ConfigureServices(IServiceCollection services)
o.JsonSerializerOptions.IncludeFields = true;
o.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
o.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
o.JsonSerializerOptions.NumberHandling |= JsonNumberHandling.AllowNamedFloatingPointLiterals;
o.JsonSerializerOptions.Converters.Add(new TypeJsonConverter());
o.JsonSerializerOptions.Converters.Add(new TypeInfoJsonConverter());
o.JsonSerializerOptions.Converters.Add(new RuntimeTypeHandleJsonConverter());
Expand Down

0 comments on commit d67d0b7

Please sign in to comment.