Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-1529

JSON parser allows unmatched trailing quote on field names

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Won't Fix
    • Icon: Minor - P4 Minor - P4
    • None
    • 2.12.3
    • JSON
    • None

    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 }

      Attachments

        Activity

          People

            Unassigned Unassigned
            michael.benoit@mongodb.com Michael Benoit
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: