D JSON (WAT?!)

Ary Borenszweig via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jul 24 09:49:27 PDT 2014


On 7/24/14, 1:09 PM, Justin Whear wrote:
> On Thu, 24 Jul 2014 16:04:01 +0000, Pavel wrote:
>>
>> Thanks to all you folks who explained "in" operator for me. My bad.
>> Let's focus on the real problem, which is JSON wrapper class. Is it
>> needed? Wouldn't it be better to get AA from parseJSON?
>
> The following are valid JSON:
>
> auto json1 = parseJSON(`1`);
> auto json2 = parseJSON(`"foo"`);
> auto json3 = parseJSON(`[1, 2, 3]`);
>
> None of these fit naturally into an JSONValue[string] return type.

Nope, a JSON can only be an array or an object (hash).

In Crystal we have this definition:

alias JsonType = Nil | Bool | Int64 | Float64 | String | Array(JsonType) 
| Hash(String, JsonType)

(note that this is a recursive type definition)

Then when you do Json.parse you get a value whose type is 
Array(JsonType) | Hash(String, JsonType).

The good thing about this is that Json.parse gives you types that 
already exist: Array, Hash, Int64, etc. No need to define extra types 
and no need for users to learn a new API with new types.

Wouldn't something like this be better to do in D? That is, return 
something that is an array or an associative array...


More information about the Digitalmars-d-learn mailing list