Closed
Bug 543507
Opened 14 years ago
Closed 12 years ago
JSON.stringify() doesn't work as described in MDC / Using native JSON
Categories
(Core :: JavaScript Engine, defect)
Core
JavaScript Engine
Tracking
()
RESOLVED
WORKSFORME
mozilla2.0
People
(Reporter: buggo, Unassigned)
Details
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729) Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729) With JSON.stringify(myobject, JSONreplaceReturnAll,5) it is not possible to change strings to different strings (eg. prefix or escape them), but it is possible to change them to undefined. I would like actually to escape all strings with encodeURI(), because JSON.stringify does not properly escape non-English unicode characters. But I later found that it is not possible for replacer function to change the value of a string to another arbitrary string, opposite of what the MDC documentation says. Reproducible: Always Steps to Reproduce: Run this JS code: var myobj_JSONtext1=JSON.stringify(myobject,JSONreplaceReturnAll1,5); var myobj_JSONtext2=JSON.stringify(myobject,JSONreplaceReturnAll2,5); function JSONreplaceReturnAll1(key, value) { if (key=='length') return undefined; var tv=typeof value; var retval; if (tv==='string') return undefined; return retval; } function JSONreplaceReturnAll2(key, value) { if (key=='length') return undefined; var tv=typeof value; var retval; if (tv==='string') return 'biboc'; return retval; } Actual Results: Strings do not get changed to 'biboc' with JSONreplaceReturnAll2, but the do get changed to undefined with 'JSONreplaceReturnAll1'. Expected Results: According to MDC / Using native JSON from https://developer.mozilla.org/en/Using_native_JSON ------------------------- The replacer parameter The replacer parameter can be either a function or an array. As a function, it gets called for each property on the object being stringified, and should return the value that should be added to the JSON string, as follows: * If you return a Number, the string corresponding to that number is used as the value for the property when added to the JSON string. * If you return a String, that string is used as the property's value when adding it to the JSON string. * If you return a Boolean, "true" or "false" is used as the property's value, as appropriate, when adding it to the JSON string. * If you return any other object, the object is serialized into the JSON string, unless the object is a function, in which case nothing is added to the JSON string. * If you return null, the property is not included in the output JSON string.
Comment 1•14 years ago
|
||
I can confirm this bug. There is no way to change the value of the property, but it can be removed from resulting string by returning 'undefined'.
Comment 2•14 years ago
|
||
What is the point of having a crippled buggy native JSON implementation? No one thinks this is an issue? here is another bug: https://bugzilla.mozilla.org/show_bug.cgi?id=548462 and here is a repro: http://skysanders.net/Tests/NativeJSONTests/JSON.tests.htm
I think that someone should at least edit the documentation at https://developer.mozilla.org/en/Using_native_JSON and write a note that JSON.stringify() doesn't work for strings as described in MDC. To save the poor souls some precious hours figuring out why their code doesn't work.
Comment 4•14 years ago
|
||
This is fixed in Fx4. We'll backport to 3.6
Comment 5•13 years ago
|
||
It looks like this was not backported? As the following still doesn't properly work in FF3.6: JSON.stringify({uncle:"bob", aunt: "margo"}, function(key,value) { return "uncle" == key ? "bill" : value})
Comment 6•12 years ago
|
||
If it hasn't happened by now, we're not going to backport it.
Status: UNCONFIRMED → RESOLVED
Closed: 12 years ago
Resolution: --- → WORKSFORME
Target Milestone: --- → mozilla2.0
You need to log in
before you can comment on or make changes to this bug.
Description
•