[JAVA-1529] JSON parser allows unmatched trailing quote on field names Created: 03/Nov/14  Updated: 19/Nov/15  Resolved: 19/Nov/15

Status: Closed
Project: Java Driver
Component/s: JSON
Affects Version/s: 2.12.3
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: Michael Benoit Assignee: Unassigned
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

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 }



 Comments   
Comment by Ross Lawley [ 19/Nov/15 ]

In 3.x we have new Json parsers that won't accept this invalid content. In the shape of: BasicDBObject.parse, Document.parse and BsonDocument.parse.

As users maybe relying on the current behaviour of JSON.parse I'm marking this as a Won't Fix.

Comment by Michael Benoit [ 03/Nov/14 ]

The shell bug mentioned in the description does exist: SERVER-8497

Generated at Thu Feb 08 08:54:50 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.