From d67d0b78f0129e0b3949020862d2577090fd1398 Mon Sep 17 00:00:00 2001 From: Aaron Robinson Date: Tue, 4 Apr 2023 23:51:33 -0500 Subject: [PATCH] Fix named floating point JSON serialization --- CSharpRepl.Tests/EvalTests.cs | 1 + CSharpRepl/Eval/DisassemblyService.cs | 5 +++-- CSharpRepl/Eval/ResultModels/EvalResult.cs | 1 + CSharpRepl/Startup.cs | 2 ++ 4 files changed, 7 insertions(+), 2 deletions(-) diff --git a/CSharpRepl.Tests/EvalTests.cs b/CSharpRepl.Tests/EvalTests.cs index 50bca2c..2f5e87e 100644 --- a/CSharpRepl.Tests/EvalTests.cs +++ b/CSharpRepl.Tests/EvalTests.cs @@ -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); diff --git a/CSharpRepl/Eval/DisassemblyService.cs b/CSharpRepl/Eval/DisassemblyService.cs index 146eb80..ea102c3 100644 --- a/CSharpRepl/Eval/DisassemblyService.cs +++ b/CSharpRepl/Eval/DisassemblyService.cs @@ -65,7 +65,7 @@ public string GetIl(string code) namespace Eval {{ - public class Code + public unsafe class Code {{ public object Main() {{ @@ -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); diff --git a/CSharpRepl/Eval/ResultModels/EvalResult.cs b/CSharpRepl/Eval/ResultModels/EvalResult.cs index 0a2b83e..28daabc 100644 --- a/CSharpRepl/Eval/ResultModels/EvalResult.cs +++ b/CSharpRepl/Eval/ResultModels/EvalResult.cs @@ -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(), diff --git a/CSharpRepl/Startup.cs b/CSharpRepl/Startup.cs index aed7451..055ef0a 100644 --- a/CSharpRepl/Startup.cs +++ b/CSharpRepl/Startup.cs @@ -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(), @@ -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());