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

JsonValue primitives #1836

Closed
SGStino opened this issue Apr 14, 2024 · 1 comment · Fixed by #1843
Closed

JsonValue primitives #1836

SGStino opened this issue Apr 14, 2024 · 1 comment · Fixed by #1843

Comments

@SGStino
Copy link

SGStino commented Apr 14, 2024

Version used

3.1.0

Describe the bug

JsonNodes get converted to JsValues, but if a JsonValue originates from a .NET integer, the conversion results in undefined

To Reproduce
roslynpad:

#r "nuget: Jint, 3.1.0"
using System.Text.Json.Nodes;
using Jint;
var engine = new Engine();

engine.SetValue("int", JsonValue.Create(15));
engine.GetValue("int").Dump(); // JsUndefined
engine.SetValue("double", JsonValue.Create(15.0));
engine.GetValue("double").Dump(); // JsNumber(15)
engine.SetValue("float", JsonValue.Create(15.0f));
engine.GetValue("float").Dump(); // JsUndefined

Expected behavior
It should take over the JsonValue number into the JsValue for all primitive types,
it works already correct for string based json values like JsonValue.Parse("15")

Additional context
Problem seems to be situated here:

  case JsonValueKind.Number:
      result = jsonValue.TryGetValue<double>(out var doubleValue) ? JsNumber.Create(doubleValue) : JsValue.Undefined;
      break;

.TryGetValue<double>() returns false if the JsonValuePrimitive<T>'s T isn't a double.

Suggested fix

  case JsonValueKind.Number:
      var doubleValue = JsonSerializer.Deserialize<double>(jsonValue);   
      result = JsNumber.Create(doubleValue);
      break;
@lahma
Copy link
Collaborator

lahma commented Apr 26, 2024

Thanks for the detailed report and repro, fix is shipping in 3.1.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants