JSON support for BigInt in Chrome/V8

Isiah Meadows isiahmeadows at gmail.com
Wed Jul 18 22:55:31 UTC 2018


This won't work....
>
> ``` desired object
> { a : 123, b : 123n }
> ```
>
> ``` json
> { "a":123 ,
>   "b":123 }
> ```
>
>  function test( json ) {
>    var c = json.a * 5;
>    var BIc = json.b * 5n;
> }
>
> if long numbers only translate to bigint (and small as Number); the calc for
> 'BIc' fails.
> if everything is translated to bigint  the calc for 'c' fails.

I think you missed my point: I'm suggesting an extra option to where
floats are parsed differently from integers.

```js
// JSON
{"a": 123.0, "b": 123}

// Parsed normally
{a: 123, b: 123}

// Parsed with option
{a: 123, b: 123n}
```

So given this, the test would be exactly what's expected with the option.

-----

Isiah Meadows
me at isiahmeadows.com
www.isiahmeadows.com


On Wed, Jul 18, 2018 at 5:03 AM, J Decker <d3ck0r at gmail.com> wrote:
>
>
> On Tue, Jul 17, 2018 at 9:16 PM Isiah Meadows <isiahmeadows at gmail.com>
> wrote:
>>
>> The way this conversation is going, we might as well just create a
>> schema-based JSON serialization DSL for both parsing and stringifying.
>> But I don't really see that as helpful in the *language itself* at
>> least as a mandatory part of the spec (maybe an optional built-in
>> module).
>>
>> I've in the past few months seen similar things come up a few times
>> already. I like the idea of a built-in schema-based JSON validator +
>> parser (it'd be faster than what we currently have), but most things
>> out there suck in some way, mostly just being boilerplatey, and
>> there's a lot of design work to get out of the way first before you
>> can come up with something that doesn't.
>>
>> But as it stands, the only things I'd support for the `JSON` global itself
>> are:
>>
>> 1. Adding separate prototypes for `JSON.stringify(source, options)`
>> and `JSON.parse(source, options)`, so it's easier to extend and
>> comprehend the arguments.
>> 2. Adding an option to parse anything consisting of purely digits (no
>> exponent or decimal) as a BigInt, regardless of size.
>
> This won't work....
>
> ``` desired object
> { a : 123, b : 123n }
> ```
>
> ``` json
> { "a":123 ,
>   "b":123 }
> ```
>
>  function test( json ) {
>    var c = json.a * 5;
>    var BIc = json.b * 5n;
> }
>
> if long numbers only translate to bigint (and small as Number); the calc for
> 'BIc' fails.
> if everything is translated to bigint  the calc for 'c' fails.
>
>> 3. Adding an option to stringify BigInts into integer-only numbers and
>> normal numbers into unconditional floats.
>>
>
>
>>
>> These could be either separate methods or part of a 4th options argument.
>>
>
> Well; Since JSON (JavaScript Object Notation) there's now available to JS a
> feature that an 'n' suffix can be applied to a number.  Seems JSON should
> just inherit that.
>
> Although I would like to see a better method than eval() to translate the
> input string from JSON for say { d : 123n }; right now within JS I'd have to
> use EVAL; in V8 I could just create a BigInt::new()....
>
> (and RE: eval, not saying it's a GOOD solution; it's just apparently the
> only solution ATM; other than splicing the string.)
>
>>
>> -----
>>
>> Isiah Meadows
>> me at isiahmeadows.com
>> www.isiahmeadows.com
>>
>>
>> On Tue, Jul 17, 2018 at 6:52 PM, Michael J. Ryan <tracker1 at gmail.com>
>> wrote:
>> > Out of bounds as you'd still have to parse it, but for encoding, could
>> > add
>> > BigInt.prototype.toJSON ...
>> >
>> > On Tue, Jul 17, 2018, 15:44 Andrea Giammarchi
>> > <andrea.giammarchi at gmail.com>
>> > wrote:
>> >>
>> >> I guess a better example would've been `Boolean('false')` returns true,
>> >> but yeah, I've moved slightly forward already with everything, if you
>> >> read
>> >> other messages.
>> >>
>> >> Regards.
>> >>
>> >> On Wed, Jul 18, 2018 at 12:06 AM Waldemar Horwat <waldemar at google.com>
>> >> wrote:
>> >>>
>> >>> On 07/17/2018 04:27 AM, Andrea Giammarchi wrote:
>> >>> > actually, never mind ... but I find it hilarious that
>> >>> > BigInt('55555555555555555550000000000000000000001') works but
>> >>> > BigInt('55555555555555555550000000000000000000001n') doesn't ^_^;;
>> >>>
>> >>> That's no different from how other built-in types work.
>> >>> String('"foo"')
>> >>> doesn't give you the same string as the string literal "foo".
>> >>>
>> >>>      Waldemar
>> >>
>> >> _______________________________________________
>> >> es-discuss mailing list
>> >> es-discuss at mozilla.org
>> >> https://mail.mozilla.org/listinfo/es-discuss
>> >
>> >
>> > _______________________________________________
>> > es-discuss mailing list
>> > es-discuss at mozilla.org
>> > https://mail.mozilla.org/listinfo/es-discuss
>> >
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>


More information about the es-discuss mailing list