Closed Bug 479865 Opened 15 years ago Closed 15 years ago

JSON.stringify adds extra commas when filtering out functions

Categories

(Core :: JavaScript Engine, defect)

x86
Windows XP
defect
Not set
normal

Tracking

()

RESOLVED DUPLICATE of bug 465371

People

(Reporter: joel, Unassigned)

Details

Attachments

(1 file)

User-Agent:       Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.0.6) Gecko/2009012700 SUSE/3.0.6-0.1.2 Firefox/3.0.6
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1b3pre) Gecko/20090223 Shiretoko/3.1b3pre (.NET CLR 3.5.30729)

When a Javascript object is turned into a JSON string, functions are filtered out.  During this process, an extra comma is then appended to the end of the object in the JSON string.  This causes the JSON string to be invalid (See http://json.org/object.gif).

Reproducible: Always

Steps to Reproduce:
1. var myObj = {foo: 123, bar: function () {}};
2. JSON.stringify(myObj);
Actual Results:  
{"foo":123,}

Expected Results:  
{"foo":123}

The invalid JSON cannot be parsed by some implementations of JSON parsers.  In my case, python's 2.6 implementation of JSON in the "json" module.  
Python 2.6 (r26:66714, Feb  3 2009, 20:49:49) 
[GCC 4.3.2 [gcc-4_3-branch revision 141291]] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>> json.loads('{"foo":123}')
{u'foo': 123}
>>> json.loads('{"foo":123,}')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.6/json/__init__.py", line 307, in loads
    return _default_decoder.decode(s)
  File "/usr/lib64/python2.6/json/decoder.py", line 319, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib64/python2.6/json/decoder.py", line 336, in raw_decode
    obj, end = self._scanner.iterscan(s, **kw).next()
  File "/usr/lib64/python2.6/json/scanner.py", line 55, in iterscan
    rval, next_pos = action(m, context)
  File "/usr/lib64/python2.6/json/decoder.py", line 198, in JSONObject
    raise ValueError(errmsg("Expecting property name", s, end - 1))
ValueError: Expecting property name: line 1 column 11 (char 11)
>>>
Assignee: nobody → general
Component: General → JavaScript Engine
Product: Firefox → Core
QA Contact: general → general
Status: UNCONFIRMED → RESOLVED
Closed: 15 years ago
Resolution: --- → DUPLICATE
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: