-
Type:
Bug
-
Resolution: Won't Fix
-
Priority:
Minor - P4
-
None
-
Affects Version/s: 2.12.3
-
Component/s: JSON
-
None
-
None
-
None
-
None
-
None
-
None
-
None
-
None
The JSON parser is a bit too lenient. For example, all these are acceptable:
/* 1. */ JSON.parse("{ \"a\": 1 }"); /* 2. */ JSON.parse("{ a: 1 }"); /* 3. */ JSON.parse("{ a\": 1 }");
The first example is what's required by the spec (ie, field names are strings and thus must be surrounded by quotes).
The second example is technically illegal but likely in wide use, so that behavior can't change.
The third example just looks wrong, but in this case the parser accepts it and assigns a field name of foo".
For comparison, here's how the shell handles these cases:
> db.test.insert({ "a": 1 }); WriteResult({ "nInserted" : 1 }) > db.test.insert({ a: 1 }); WriteResult({ "nInserted" : 1 }) > db.test.insert({ a": 1 }); 2014-11-03T15:32:43.803-0500 SyntaxError: Unexpected token ILLEGAL > db.test.insert({ a\": 1 }); 2014-11-03T15:32:48.482-0500 SyntaxError: Unexpected token ILLEGAL > db.test.insert({ 'a"': 1 }); WriteResult({ "nInserted" : 1 }) > db.test.find({}, { _id: 0 } ); { "a" : 1 } { "a" : 1 } { "a"" : 1 }
Looks like there may even be a bug in the shell here as well, as I would expect the third document to be printed as:
{ "a\"": 1 }