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

Backport improve System.Text.Json numeric type conversion under interop #1844

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
49 changes: 22 additions & 27 deletions Jint.Tests.PublicInterface/InteropTests.SystemTextJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand Down Expand Up @@ -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<double>(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<int>(out var intValue) ? JsNumber.Create(intValue) : JsonSerializer.Deserialize<double>(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;
}
Expand Down
4 changes: 3 additions & 1 deletion Jint/Runtime/Interop/DefaultObjectConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<double>(out var doubleValue) ? JsNumber.Create(doubleValue) : JsValue.Undefined,
#pragma warning disable IL2026, IL3050
System.Text.Json.JsonValueKind.Number => value.TryGetValue<int>(out var intValue) ? JsNumber.Create(intValue) : System.Text.Json.JsonSerializer.Deserialize<double>(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,
Expand Down