Skip to content

Commit

Permalink
Backport improve System.Text.Json numeric type conversion under inter…
Browse files Browse the repository at this point in the history
…op (#1844)
  • Loading branch information
lahma authored Apr 26, 2024
1 parent 78bc1ec commit f767670
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 28 deletions.
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

0 comments on commit f767670

Please sign in to comment.