std.data.json formal review

Marc Schütz via Digitalmars-d digitalmars-d at puremagic.com
Tue Sep 29 04:06:01 PDT 2015


On Monday, 28 September 2015 at 07:02:35 UTC, Marco Leise wrote:
> Am Tue, 18 Aug 2015 09:05:32 +0000
> schrieb "Marc Schütz" <schuetzm at gmx.net>:
>
>> Or, as above, leave it to the end user and provide a `to(T)` 
>> method that can support built-in types and `BigInt` alike.
>
> You mean the user should write a JSON number parsing routine
> on their own? Then which part is responsible for validation of
> JSON contraints? If it is the to!(T) function, then it is
> code duplication with chances of getting something wrong,
> if it is the JSON parser, then the number is parsed twice.
> Besides, there is a lot of code to be shared for every T.

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;


More information about the Digitalmars-d mailing list