3

I am considering to use JSON files as input files for a c++ program. I was told JSON files are a very safe and convenient way to input data. However, I do not have any experience with javascript. I would like to ask for recommendations on a good parser to use in order to start trying JSON files in a new project.

4 Answers 4

2

you may want to take a look at Json.hpp JSON library of jtc tool:

  • it's an easy C++14 API, featuring DSL-like style for building JSON programmatically
  • no dependancies, uses STL only
  • written in idiomatic c++14 style, cross platform, thread safe, extensively debuggable
  • features fast and powerful walk interface and iterators (easy to walk even irregular JSONs)
  • caters user callbacks (hooks to be placed on labels and iterators)
  • speed optimized when searching/walking very large JSONs (built-in search cache)
3
  • 2
    Welcome aboard, Dmitry. This is an excellent first post, and I have upvoted it. However, we prefer that you disclose your activity in anything which you recommend. In this case, you appear to be the author. Thant's not so bad because your code is FOSS, but sometimes people try to recommend things they sell. Even for FOSS it helps to know that the author is active and likely to help. Once again, welcome aboard :-) Nov 13, 2018 at 10:32
  • 1
    Got it. Will do. jtc tool of mine (as well as all the included libraries) will be maintained. They all are published under MIT license. Plus I'm open for any reasonable enhancement requests.
    – Dmitry L.
    Nov 14, 2018 at 12:08
  • I look forward to seeing you be active on the site :-) Nov 14, 2018 at 12:44
1

I suggest you using, if you already didn't know them, Boost C++ libraries.

And for your actual scenario using the property_tree one.

For example a JSON like that:

{
   "menu":
   {
      "foo": true,
      "bar": "true",
      "value": 102.3E+06,
      "popup":
      [
         {"value": "New", "onclick": "CreateNewDoc()"},
         {"value": "Open", "onclick": "OpenDoc()"},
      ]
   }
}

Will be mapped in a tree like that:

menu
{
   foo true
   bar true
   value 102.3E+06
   popup
   {
      ""
      {
         value New
         onclick CreateNewDoc()
      }
      ""
      {
         value Open
         onclick OpenDoc()
      }
   }
}

On stackoverflow you can find many examples like that.

0

I mainly used the RapidJSON library.
So far I was quite satisfied with it and the documentation is also quite good.

Although I still want to try out this library. But up to now I had no time to compare them or do some benchmarks. So there is not much I can say about it.

0

Easy and Best way to deal with json is nlohmann/json

Design goals There are myriads of JSON libraries out there, and each may even have its reason to exist. Our class had these design goals:

  • Intuitive syntax. In languages such as Python, JSON feels like a first class data type. We used all the operator magic of modern C++ to achieve the same feeling in your code. Check out the examples below and you'll know what I mean.
  • Trivial integration. Our whole code consists of a single header file json.hpp. That's it. No library, no subproject, no dependencies, no complex build system. The class is written in vanilla C++11. All in all, everything should require no adjustment of your compiler flags or project settings.
  • Serious testing. Our class is heavily unit-tested and covers 100% of the code, including all exceptional behavior. Furthermore, we checked with Valgrind and the Clang Sanitizers that there are no memory leaks. Google OSS-Fuzz additionally runs fuzz tests against all parsers 24/7, effectively executing billions of tests so far. To maintain high quality, the project is following the Core Infrastructure Initiative (CII) best practices.

Other aspects were not so important to us:

  • Memory efficiency. Each JSON object has an overhead of one pointer (the maximal size of a union) and one enumeration element (1 byte). The default generalization uses the following C++ data types: std::string for strings, int64_t, uint64_t or double for numbers, std::map for objects, std::vector for arrays, and bool for Booleans. However, you can template the generalized class basic_json to your needs.
  • Speed. There are certainly faster JSON libraries out there. However, if your goal is to speed up your development by adding JSON support with a single header, then this library is the way to go. If you know how to use a std::vector or std::map, you are already set.
#include <nlohmann/json.hpp>
using namespace nlohmann;
using json = nlohmann::json;
int main{
json j;

// add a number that is stored as double (note the implicit conversion of j to an object)
j["pi"] = 3.141;

// add a Boolean that is stored as bool
j["happy"] = true;

// add a string that is stored as std::string
j["name"] = "Niels";

// add another null object by passing nullptr
j["nothing"] = nullptr;

// add an object inside the object
j["answer"]["everything"] = 42;

// add an array that is stored as std::vector (using an initializer list)
j["list"] = { 1, 0, 2 };

// add another object (using an initializer list of pairs)
j["object"] = { {"currency", "USD"}, {"value", 42.99} };

// instead, you could also write (which looks very similar to the JSON above)
json j2 = {
  {"pi", 3.141},
  {"happy", true},
  {"name", "Niels"},
  {"nothing", nullptr},
  {"answer", {
    {"everything", 42}
  }},
  {"list", {1, 0, 2}},
  {"object", {
    {"currency", "USD"},
    {"value", 42.99}
  }}
};

std::cout << j.dump() << endl ; /* or j.dump(4) serialization with pretty printing pass in the amount of spaces to indent */
std::cout << j2.dump() << endl ;

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.