Opened 9 years ago

Closed 8 years ago

#11433 closed defect (fixed)

dojo.toJson returns invalid JSON for undefined element

Reported by: mlutton Owned by: mlutton
Priority: high Milestone: 1.7
Component: Core Version: 1.4.3
Keywords: toJson Cc:
Blocked By: Blocking:

Description

Browser: FireFox? 3.6.6 OS: Microsoft Windows XP SP3 email: mlutton@…

Steps to reproduce:

Define an object with an undefined element, for instance:

  var a = {'a':1, 'b':null, 'c':undefined};

Call dojo.toJson(a). Result is:

  {"a":1,"b":null,"c":undefined}

The "undefined" is invalid JSON. See JSON syntax at http://www.json.org. When used for an Ajax request for a Web service that expects JSON using the Jackson serializer, the request is rejected.

Change History (6)

comment:1 Changed 9 years ago by Eugene Lazutkin

Resolution: invalid
Status: newclosed

What is the valid JSON value for undefined in your opinion? It appears that dojo.toJson() works correctly: garbage in, garbage out.

comment:2 Changed 8 years ago by jgivoni

Resolution: invalid
Status: closedreopened

Is the undefined value garbage?

Look at the discussion here: http://stackoverflow.com/questions/6185114/why-do-php-function-json-decode-fail-if-string-contains-the-word-undefined-as-a-v

Two arguments:

  1. It seems unreasonable that we have to 'sanitize for json' first, every time we want to use dojo.toJson. Admittedly it would be great if the undefined value was never returned from any 3rd party library, but we cannot expect that.
  2. Function references are not transcribed into JSON. Does that make them garbage, and why does dojo.toJson happily omit them?

A reasonable solution would be simply to omit properties with undefined values.

comment:3 Changed 8 years ago by ben hockey

Owner: changed from anonymous to mlutton
Status: reopenedpending

using dojo.toJson from dojo 1.7 in chrome omits properties with undefined values.

do we need to do anything more with this ticket?

if there's no response it will automatically close in 14 days.

comment:4 Changed 8 years ago by trac-o-bot

Resolution: invalid
Status: pendingclosed

Because we get so many tickets, we often need to return them to the initial reporter for more information. If that person does not reply within 14 days, the ticket will automatically be closed, and that has happened in this case. If you still are interested in pursuing this issue, feel free to add a comment with the requested information and we will be happy to reopen the ticket if it is still valid. Thanks!

comment:5 Changed 8 years ago by bill

Resolution: invalid
Status: closedreopened

Should be marked as fixed in 1.7, not invalid.

comment:6 Changed 8 years ago by bill

Component: GeneralCore
Milestone: tbd1.7
Resolution: fixed
Status: reopenedclosed
Note: See TracTickets for help on using tickets.