Skip to content

JsonCpp

JSON is a lightweight data-interchange format. It can represent numbers, strings, ordered sequences of values, and collections of name/value pairs.

JsonCpp is a C++ library that allows manipulating JSON values, including serialization and deserialization to and from strings. It can also preserve existing comment in unserialization/serialization steps, making it a convenient format to store user input files.

Read from string

TEST(JsoncppTest, Default)
{
    char const * TEST_JSON = R"([{"index":1,"name":"first"}, {"index":2,"name":"second"}])";

    Json::Value root;
    Json::Reader reader;

    ASSERT_TRUE(reader.parse(TEST_JSON, root));
    ASSERT_EQ(2, root.size());
    ASSERT_EQ(1, root[0]["index"].asInt());
    ASSERT_EQ(2, root[1]["index"].asInt());
}

Write to string

Json::FastWriter fastWriter;
std::string output = fastWriter.write(root);

OR

Json::Value json = ...;
Json::StreamWriterBuilder builder;
builder["indentation"] = ""; // If you want whitespace-less output
const std::string output = Json::writeString(builder, json);

See also

Local Download

Jsoncpp v1.7.7
Jsoncpp-1.7.7.zip

Favorite site