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

Ticket #1883 (closed enhancement: fixed)

Opened 14 years ago

Last modified 14 years ago

Deverse problems with Json

Reported by: stanleyxu2005 Assigned to: kris
Priority: normal Milestone: 1.0
Component: Tango Version: 0.99.9 Kai
Keywords: Cc:

Description

Dear Developers, I found four problems by using Tango Json.

PROBLEM 1: 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]}


PROBLEM 2: 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 3: 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.

PROBLEM 4: Suggest to add a serialize method for Json

It is very inconvenience to print a Json object as string. We have to declare a delegate function first.

Thanks in advance

Change History

03/19/10 08:44:11 changed by stanleyxu2005

  • priority changed from major to normal.

03/19/10 12:45:59 changed by mwarning

There is another json impl. that may fix your problems: http://dsource.org/projects/tango/ticket/1491

(follow-ups: ↓ 5 ↓ 6 ) 03/20/10 22:10:42 changed by kris

  • issue 2 & 3 were resolved in the last release (are you using a release prior to 0.99.9?)
  • issue 1 is tricky, because array(null) is a perfectly legitimate operation (an array with a null value in it). The way to obtain an empty array is to specify an empty one: array()

03/20/10 22:11:45 changed by kris

  • status changed from new to closed.
  • resolution set to fixed.

(In [5412]) fixes #1883 :: Deverse problems with Json

Added a print() method to the document, which returns the document as a text array

Thanks to stanleyxu2005

(in reply to: ↑ 3 ) 03/21/10 10:57:34 changed by stanleyxu2005

Replying to kris:

* issue 2 & 3 were resolved in the last release (are you using a release prior to 0.99.9?) * issue 1 is tricky, because array(null) is a perfectly legitimate operation (an array with a null value in it). The way to obtain an empty array is to specify an empty one: array()

Thanks for your response and the changes. I will upgrade to 0.99.9 next week ;-)

(in reply to: ↑ 3 ) 03/21/10 11:29:59 changed by stanleyxu2005

Replying to kris:

* issue 1 is tricky, because array(null) is a perfectly legitimate operation (an array with a null value in it). The way to obtain an empty array is to specify an empty one: array()

IMO, it is incorrect. If you want to insert a NULL into an array, you should call:

array([null]); // -> {"array": [null]}
array(null); // -> {"array": []}

(follow-up: ↓ 8 ) 03/21/10 18:44:28 changed by kris

that could be true, though (as always) the devil is in the details :|

(in reply to: ↑ 7 ) 03/23/10 00:59:10 changed by stanleyxu2005

Replying to kris:

that could be true, though (as always) the devil is in the details :|

According to your comment, will you fix this issue? I just reopened this ticket.

(follow-up: ↓ 10 ) 03/23/10 01:06:57 changed by kris

I'm afraid it's not a positive change overall, so we'll not be (re)implementing it in the manner suggested

(in reply to: ↑ 9 ) 03/25/10 09:01:05 changed by stanleyxu2005

Replying to kris:

I'm afraid it's not a positive change overall, so we'll not be (re)implementing it in the manner suggested

Thanks. I was my carelessness. There is a method value(Value[]). I should use this method instead.

BTW: I have upgraded to the latest tango (trunk). The print method can finally return a string back, thanks. But... the print method of Value, still requires a delegated method. Could you make the both print methods consistent?

03/25/10 09:06:21 changed by stanleyxu2005

  • status changed from closed to reopened.
  • resolution deleted.
  • type changed from defect to enhancement.

The patch code:

        Value print (void delegate(T[]) append, T[] separator = null)
        {
            //...
        }

        final T[] print (T[] separator = null)
        {
                T[] tmp;
                void append (T[] s) {tmp ~= s;}
                print (&append, separator);
                return tmp;
        }

05/10/10 01:11:37 changed by kris

  • status changed from reopened to closed.
  • resolution set to fixed.

done. Also moved the print() overload inside Value, so might have to use json.value.print(..) syntax to get the full document