Falling back to float when serializing of JavaScript 'number'
See original GitHub issueI 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:
- Created 5 years ago
- Comments:12 (12 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
and we should really deprecate/remove the
dynamic
overload, it’s a source of weird bugs