From f767670c4a83d93014ff69899a876dd7f5d6c2f7 Mon Sep 17 00:00:00 2001 From: Marko Lahma Date: Fri, 26 Apr 2024 20:13:01 +0300 Subject: [PATCH] Backport improve System.Text.Json numeric type conversion under interop (#1844) --- .../InteropTests.SystemTextJson.cs | 49 +++++++++---------- .../Runtime/Interop/DefaultObjectConverter.cs | 4 +- 2 files changed, 25 insertions(+), 28 deletions(-) diff --git a/Jint.Tests.PublicInterface/InteropTests.SystemTextJson.cs b/Jint.Tests.PublicInterface/InteropTests.SystemTextJson.cs index 2fadd4e824..3c7ee4c270 100644 --- a/Jint.Tests.PublicInterface/InteropTests.SystemTextJson.cs +++ b/Jint.Tests.PublicInterface/InteropTests.SystemTextJson.cs @@ -154,6 +154,16 @@ function populateFullName() { Assert.True(engine.Evaluate("variables.employees.other == 'def'").AsBoolean()); } + [Fact] + public void AccessingSystemTextJsonNumericTypes() + { + var engine = GetEngine(); + + Assert.Equal(15, engine.SetValue("int", JsonValue.Create(15)).Evaluate("int")); + Assert.Equal(15.0, engine.SetValue("double", JsonValue.Create(15.0)).Evaluate("double")); + Assert.Equal(15f, engine.SetValue("float", JsonValue.Create(15.0f)).Evaluate("float")); + } + private static Engine GetEngine() { var engine = new Engine(options => @@ -181,34 +191,19 @@ public bool TryConvert(Engine engine, object value, out JsValue result) if (value is JsonValue jsonValue) { var valueKind = jsonValue.GetValueKind(); - switch (valueKind) + result = valueKind switch { - case JsonValueKind.Object: - case JsonValueKind.Array: - result = JsValue.FromObject(engine, jsonValue); - break; - case JsonValueKind.String: - result = jsonValue.ToString(); - break; - case JsonValueKind.Number: - result = jsonValue.TryGetValue(out var doubleValue) ? JsNumber.Create(doubleValue) : JsValue.Undefined; - break; - case JsonValueKind.True: - result = JsBoolean.True; - break; - case JsonValueKind.False: - result = JsBoolean.False; - break; - case JsonValueKind.Undefined: - result = JsValue.Undefined; - break; - case JsonValueKind.Null: - result = JsValue.Null; - break; - default: - result = JsValue.Undefined; - break; - } + JsonValueKind.Object or JsonValueKind.Array => JsValue.FromObject(engine, jsonValue), + JsonValueKind.String => jsonValue.ToString(), +#pragma warning disable IL2026, IL3050 + JsonValueKind.Number => jsonValue.TryGetValue(out var intValue) ? JsNumber.Create(intValue) : JsonSerializer.Deserialize(jsonValue), +#pragma warning restore IL2026, IL3050 + JsonValueKind.True => JsBoolean.True, + JsonValueKind.False => JsBoolean.False, + JsonValueKind.Undefined => JsValue.Undefined, + JsonValueKind.Null => JsValue.Null, + _ => JsValue.Undefined + }; return true; } diff --git a/Jint/Runtime/Interop/DefaultObjectConverter.cs b/Jint/Runtime/Interop/DefaultObjectConverter.cs index 295af89702..bd9820cc57 100644 --- a/Jint/Runtime/Interop/DefaultObjectConverter.cs +++ b/Jint/Runtime/Interop/DefaultObjectConverter.cs @@ -167,7 +167,9 @@ public static bool TryConvert(Engine engine, object value, Type? type, [NotNullW System.Text.Json.JsonValueKind.Object => JsValue.FromObject(engine, value), System.Text.Json.JsonValueKind.Array => JsValue.FromObject(engine, value), System.Text.Json.JsonValueKind.String => JsString.Create(value.ToString()), - System.Text.Json.JsonValueKind.Number => value.TryGetValue(out var doubleValue) ? JsNumber.Create(doubleValue) : JsValue.Undefined, +#pragma warning disable IL2026, IL3050 + System.Text.Json.JsonValueKind.Number => value.TryGetValue(out var intValue) ? JsNumber.Create(intValue) : System.Text.Json.JsonSerializer.Deserialize(value), +#pragma warning restore IL2026, IL3050 System.Text.Json.JsonValueKind.True => JsBoolean.True, System.Text.Json.JsonValueKind.False => JsBoolean.False, System.Text.Json.JsonValueKind.Undefined => JsValue.Undefined,