question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Falling back to float when serializing of JavaScript 'number'

See original GitHub issue

I have code that calls EvaluateExpressionAsync in order to return a value, the value can be an object or primitive so I don’t specify the ‘T’, which defaults to “object”. I do this so that I can later choose how to represent the value as string.

The issue I have is that ValueFromRemoteObject defaults to a floating point number if the ‘T’ isn’t int32/64.

await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);

var browser = await new Launcher().LaunchAsync(new LaunchOptions
{
    Headless = true,
});

var page = await browser.NewPageAsync();

var javascript = "<html><body><script type=\"text/javascript\">var obj = { \"int64\": 11111111111111 };</script></body></html>";

await page.SetContentAsync(javascript);

var result = await page.EvaluateExpressionAsync("obj.int64"); // result is System.Single

var str = Convert.ToString(result); // str is 1.111111E+13

browser.Dispose();

There needs to be some kind of intelligent numeric type detection such as the ParseReadNumber used by Newtonsoft Json.Net so that my result is an integer.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
tommymonkcommented, Sep 27, 2018

If I get time later this evening I’ll have a go at forking and cutting back some of the dynamic to make things like this easier.

1reaction
Meir017commented, Sep 27, 2018

and we should really deprecate/remove the dynamic overload, it’s a source of weird bugs

Read more comments on GitHub >

github_iconTop Results From Across the Web

Falling back to float when serializing of JavaScript 'number'
The issue I have is that ValueFromRemoteObject defaults to a floating point number if the 'T' isn't int32/64. await new BrowserFetcher().
Read more >
How to handle serializing floats from C# into numbers in ...
I think you can convert floats without decimal part to integers. This way you you JavaScript json will be the same as c#...
Read more >
Number - JavaScript - MDN Web Docs - Mozilla
Chrome Edge Number Full support. Chrome1. Toggle history Full support. Edge12. Toggl... EPSILON Full support. Chrome34. Toggle history Full support. Edge12. Toggl... MAX_SAFE_INTEGER Full support. Chrome34....
Read more >
JavaScript and Number Serialization | by Greg Pabian
The reverse operation goes by deserialization or unmarshalling. It converts data from a transmittable format into an in-memory object. Again, I ...
Read more >
Number Parsing at a Gigabyte per Second
Many programming languages call them double and float. JavaScript represents all its numbers, by default, with a 64-bit binary floating-point ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found