Are comments allowed in a JSON file for boost read_json?

1.4k views Asked by At

This is a homotopy of the json file I always used to read through boost::property_tree::json_parser::read_json

And it was always working.

/**********************************************/
/*                 the title                  */
/**********************************************/



{
    "garden": {
        "side1": {
            "treeA1": "apple",
            "treeA2": "orange",
            "treeA3": "banana",
        },
        "side2": {
            "treeB1": "orange",
            "treeB2": "palm",
            "treeB3": "cherry",
        }
    },
    "house": ""
}

I upgraded my boost version from 1.58.0 to 1.65.0 and now I receive an exception because of the comments. When I remove the comments, everything is fine.

Am I making a mistake somewhere or is it a bug in the new version of boost?

2

There are 2 answers

0
sehe On BEST ANSWER

Comments are not JSON.

The old parser did have them, but didn't properly support unicode.

Here's the message in the release notes for Boost 1.59.0:

Property Tree:

  • A new JSON parser with full Unicode support.
  • Breaking change: The new parser does not support comments or string concatenation in JSON files. These features were non-standard extensions of the old parser but their removal could break code which was relying on them.
0
Remy Lebeau On

The official JSON standard does not define a syntax for comments (here's the reason why).

Support for comments is implemented (or not) on a per-parser basis. It was probably something that Boost once supported for convenience but later removed for compliance (I'm speculating, as I don't use Boost myself).

If Boost no longer supports comments, you will have to strip them out before parsing. There are plenty of 3rd party implementations for that very purpose. See Can comments be used in JSON? for suggestions.