-->

Does Boost Property Tree have methods for defining

2019-05-29 09:16发布

问题:

I have a project that will be using a single JSON file in order to describe the inner contents of the directory it is contained within. The Boost Property Tree library appears to be the best choice for a JSON parsing library. However, this JSON file (like many others) is expected to have a very particular layout, and so far I haven't found a good manner of expressing the rules in code.

Does the Boost Property Tree library have any methods allowing the user to do any of the following?

  • Disallowing unrecognized keys.
  • Specifying keys as required or optional.
  • Specifying the expected value type of keys (null, bool, int, float, array, dictionary).

So far I've found this previous question which shines some light on how to translate some JSON key-value pairs into data structures, but it doesn't answer whether any of the capabilities listed above are possible.

回答1:

Property Tree didn't take long for me to give up on due to lack of types (as sehe pointed out).

To take sehe's recommendation further, if you are dealing with JSON, I would recommend a user-friendly library like nlohmann's library instead. It is reasonably fast and intuitive.

There is also RapidJSON, which obsesses over every bit of memory allocation, etc, impacting usability, but the best performer in (its own) tests. I encountered difficulty with its handling of single-item arrays before moving to nlohmann's.



回答2:

None of the capabilities specified are supported.

Worse, Property Tree doesn't even support JSON data types. On the JSON side, everything will be string, no matter what.

Oh and arrays aren't directly supported: prepare for an ugly kludge.

All of these are in the 20 or so lines of documentation that explain JSON backend of Property Tree.

Because of these limitations, be smart and choose for a JSON library to build your requirements upon.