std.data.json formal review

Marco Leise via Digitalmars-d digitalmars-d at puremagic.com
Wed Sep 30 15:26:01 PDT 2015


Am Tue, 29 Sep 2015 11:06:01 +0000
schrieb Marc Schütz <schuetzm at gmx.net>:

> No, the JSON type should just store the raw unparsed token and 
> implement:
> 
>      struct JSON {
>          T to(T) if(isNumeric!T && is(typeof(T("")))) {
>              return T(this.raw);
>          }
>      }
> 
> The end user can then call:
> 
>      auto value = json.to!BigInt;

Ah, the duck typing approach of accepting any numeric type
constructible from a string.

Still: You need to parse the number first to know how long
the digit string is that you pass to T's ctor. And then you
have two sets of syntaxes for numbers: JSON and T's ctor. T
could potentially parse numbers with the system locale's
setting for the decimal point which may be ',' while JSON uses
'.' or support hexadecimal numbers which are also invalid JSON.
On the other hand, a ctor for some integral type may not
support the exponential notation "2e10", which could
legitimately be used by JSON writers (Ruby's uses shortest way
to store numbers) to save on bandwidth.

-- 
Marco



More information about the Digitalmars-d mailing list