Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

The method array in Json seems to be incorrect implemented

Moderators: kris

Posted: 03/18/10 09:27:15

Dear Developers,

The method array in Json seems to be incorrect implemented.

Json!(char).Value[] values = null;
auto json = new Json!(char);

json.value = object(pair("array", array(values)));

The result is expected to be

{"array": []}

But it was

{"array": [null]}

blog: stanleyxu's area51 proj: gosurf browser

Author Message

Posted: 03/18/10 10:29:05

Another question: Why Json does not provide a serialize (or toString) method. It is very inconvenience to print a Json object as string. We have to declare a delegate function first.

blog: stanleyxu's area51
project: gosurf webbrowser

Posted: 03/18/10 20:06:53

stanleyxu2005 wrote:

Another question: Why Json does not provide a serialize (or toString) method. It is very inconvenience to print a Json object as string. We have to declare a delegate function first.

Yeah, that's a pain ... will fix it tonight (the delegate used is often part of an existing output stream, so toString() has historically not been needed so much).

Posted: 03/18/10 20:08:26

stanleyxu2005 wrote:

Dear Developers,

The method array in Json seems to be incorrect implemented.

Json!(char).Value[] values = null;
auto json = new Json!(char);

json.value = object(pair("array", array(values)));

The result is expected to be

{"array": []}

But it was

{"array": [null]}

Yeah, that looks like a bug

Posted: 03/18/10 21:58:06 -- Modified: 03/18/10 23:24:45 by
stanleyxu2005 -- Modified 2 Times

kris wrote:
stanleyxu2005 wrote:

Another question: Why Json does not provide a serialize (or toString) method. It is very inconvenience to print a Json object as string. We have to declare a delegate function first.

Yeah, that's a pain ... will fix it tonight (the delegate used is often part of an existing output stream, so toString() has historically not been needed so much).

A better name would be serialize. What does it sound like?

I just report another two problems of Json here, if you already want to fix Json problem very soon.

PROBLEM 1: Parsing negative integer failed

auto json = new Json!(char);

json.parse(`{"jsonrpc":"2.0", "error": {"code":  32700, "message": "Parse error"}, "id": null}`); // PASSED
json.parse(`{"jsonrpc":"2.0", "error": {"code": -32700, "message": "Parse error"}, "id": null}`); // object.Exception: Expected " before member name

PROBLEM 2: Integer should not be converted to a Float.

In my application, I want to send a Json RPC error response, but the error code is coded as -32700.00 not -32700.

auto json = new Json!(char);

json.value = json.object(json.pair("code", json.value(-32700)));

//   output -> {"code": -32700.00}
// expected -> {"code": -32700}

assert(json.value("code").toNumber == -32700); // PASSED

The function toNumber returns a float, but it is OK. We can convert it to the correct type easily. But the string representation should not be a float. If we send Json string to other application in other language, it may be regarded as an error.

blog: stanleyxu's area51
project: gosurf webbrowser

Posted: 03/19/10 07:52:51

I have created a ticket http://dsource.org/projects/tango/ticket/1883

blog: stanleyxu's area51
project: gosurf webbrowser

Posted: 04/03/10 15:23:25

Just a quick note about the final result. We should use "value(...)" instead of "array(...)" in case of creating an array. The rest problems are solved in the current trunk version.

Only one thing left is that Value.print() requires a delegated append function to echo the content.

blog: stanleyxu's area51
project: gosurf webbrowser