Convert Figma logo to code with AI

open-source-parsers logojsoncpp

A C++ library for interacting with JSON.

8,835
2,723
8,835
121

Top Related Projects

49,609

JSON for Modern C++

15,057

A fast JSON parser/generator for C++ with both SAX/DOM style API

23,974

Parsing gigabytes of JSON per second : used by Facebook/Meta Velox, the Node.js runtime, ClickHouse, WatermelonDB, Apache Doris, Milvus, StarRocks

C/C++ JSON parser/generator benchmark

Quick Overview

JsonCpp is a C++ library that allows manipulating JSON values, including serialization and deserialization to and from strings. It aims to be simple and efficient, with a focus on usability and performance. The library provides a stable and well-documented API for working with JSON data in C++ applications.

Pros

  • Easy to use and integrate into existing C++ projects
  • Supports both reading and writing JSON data
  • Provides a flexible and intuitive API for JSON manipulation
  • Well-documented and actively maintained

Cons

  • Performance may not be as high as some other JSON libraries
  • Limited support for advanced JSON features like JSON Pointer or JSON Patch
  • Requires C++11 or later, which may be an issue for older codebases
  • Larger binary size compared to some lightweight alternatives

Code Examples

  1. Parsing JSON from a string:
#include <json/json.h>
#include <iostream>

int main() {
    const std::string rawJson = R"({"name": "John", "age": 30, "city": "New York"})";
    Json::Value root;
    Json::Reader reader;
    
    if (reader.parse(rawJson, root)) {
        std::cout << "Name: " << root["name"].asString() << std::endl;
        std::cout << "Age: " << root["age"].asInt() << std::endl;
        std::cout << "City: " << root["city"].asString() << std::endl;
    }
    return 0;
}
  1. Creating and writing JSON:
#include <json/json.h>
#include <iostream>

int main() {
    Json::Value root;
    root["name"] = "Alice";
    root["age"] = 25;
    root["hobbies"].append("reading");
    root["hobbies"].append("painting");

    Json::StreamWriterBuilder writer;
    std::cout << Json::writeString(writer, root) << std::endl;
    return 0;
}
  1. Modifying existing JSON data:
#include <json/json.h>
#include <iostream>

int main() {
    Json::Value root;
    root["user"]["name"] = "Bob";
    root["user"]["email"] = "bob@example.com";

    // Modify existing values
    root["user"]["name"] = "Robert";
    root["user"]["age"] = 40;

    // Remove a key
    root["user"].removeMember("email");

    Json::StreamWriterBuilder writer;
    std::cout << Json::writeString(writer, root) << std::endl;
    return 0;
}

Getting Started

To use JsonCpp in your project:

  1. Install JsonCpp using your package manager or build it from source.
  2. Include the necessary headers in your C++ file:
    #include <json/json.h>
    
  3. Link against the JsonCpp library when compiling your project.
  4. Use the Json::Value class to work with JSON data, and Json::Reader or Json::CharReader for parsing.
  5. For writing JSON, use Json::StreamWriterBuilder or Json::FastWriter.

Example compilation command:

g++ -std=c++11 your_file.cpp -ljsoncpp -o your_program

Competitor Comparisons

49,609

JSON for Modern C++

Pros of nlohmann/json

  • Header-only library, easier to integrate into projects
  • More modern C++ design with extensive use of templates and STL
  • Intuitive API with JSON literals and automatic type inference

Cons of nlohmann/json

  • Potentially longer compile times due to heavy template usage
  • May have a steeper learning curve for developers less familiar with modern C++
  • Larger binary size in some cases

Code Comparison

nlohmann/json:

#include <nlohmann/json.hpp>
using json = nlohmann::json;

json j = {{"name", "John"}, {"age", 30}};
std::string s = j.dump();

jsoncpp:

#include <json/json.h>

Json::Value root;
root["name"] = "John";
root["age"] = 30;
Json::FastWriter writer;
std::string s = writer.write(root);

Both libraries offer JSON parsing and serialization capabilities, but nlohmann/json provides a more modern and intuitive API. jsoncpp may be preferred for projects requiring broader compiler support or where compile-time performance is critical. The choice between the two often depends on specific project requirements and developer preferences.

15,057

A fast JSON parser/generator for C++ with both SAX/DOM style API

Pros of rapidjson

  • Significantly faster parsing and serialization performance
  • Lower memory usage and footprint
  • Support for in-situ parsing, reducing memory allocations

Cons of rapidjson

  • Steeper learning curve due to more complex API
  • Less intuitive for simple use cases
  • Requires more careful memory management

Code Comparison

jsoncpp:

Json::Value root;
Json::Reader reader;
bool parsingSuccessful = reader.parse(jsonString, root);
if (parsingSuccessful) {
    std::string name = root["name"].asString();
}

rapidjson:

rapidjson::Document document;
document.Parse(jsonString);
if (!document.HasParseError()) {
    const char* name = document["name"].GetString();
}

Both libraries offer JSON parsing and serialization capabilities, but rapidjson focuses on performance and efficiency at the cost of a more complex API. jsoncpp provides a simpler interface that's easier to use for basic tasks but may not be as performant for large-scale applications. The choice between the two depends on the specific requirements of your project, balancing ease of use with performance needs.

23,974

Parsing gigabytes of JSON per second : used by Facebook/Meta Velox, the Node.js runtime, ClickHouse, WatermelonDB, Apache Doris, Milvus, StarRocks

Pros of simdjson

  • Extremely fast JSON parsing using SIMD instructions
  • Low memory usage and zero-copy parsing
  • Designed for modern CPUs and optimized for performance

Cons of simdjson

  • Requires C++17 or later, limiting compatibility with older systems
  • Less mature and widely adopted compared to JsonCpp
  • Focused primarily on parsing, with fewer high-level manipulation features

Code Comparison

simdjson:

simdjson::dom::parser parser;
simdjson::dom::element doc = parser.parse(json_string);
std::string_view result = doc["key"];

JsonCpp:

Json::Value root;
Json::Reader reader;
reader.parse(json_string, root);
std::string result = root["key"].asString();

Key Differences

  • simdjson is designed for high-performance parsing, while JsonCpp offers a more comprehensive JSON manipulation toolkit
  • simdjson uses a DOM-style API, whereas JsonCpp provides both DOM and SAX-style interfaces
  • simdjson is more suitable for projects requiring fast parsing of large JSON datasets, while JsonCpp is better for general-purpose JSON handling and manipulation

Use Cases

  • Choose simdjson for applications that prioritize parsing speed and efficiency
  • Opt for JsonCpp when working with older codebases or requiring more extensive JSON manipulation features

C/C++ JSON parser/generator benchmark

Pros of nativejson-benchmark

  • Focuses on performance benchmarking of JSON parsers
  • Provides comprehensive comparisons across multiple JSON libraries
  • Useful for developers selecting a JSON parser based on performance metrics

Cons of nativejson-benchmark

  • Not a JSON parsing library itself, unlike jsoncpp
  • Limited to benchmarking functionality, doesn't offer parsing capabilities
  • May require more setup and configuration for use in projects

Code Comparison

nativejson-benchmark (benchmark execution):

int main() {
    Benchmark benchmark;
    benchmark.runOnce<RapidJsonInsituParser>();
    benchmark.runOnce<JsoncppReader>();
    benchmark.printResults();
    return 0;
}

jsoncpp (parsing JSON):

Json::Value root;
Json::Reader reader;
bool parsingSuccessful = reader.parse(jsonString, root);
if (parsingSuccessful) {
    std::string name = root["name"].asString();
}

nativejson-benchmark is primarily used for comparing JSON parser performance, while jsoncpp is a full-fledged JSON parsing library. The code examples illustrate their different purposes: nativejson-benchmark runs benchmarks on various parsers, while jsoncpp directly parses JSON data. Choose nativejson-benchmark for performance analysis and jsoncpp for actual JSON parsing in your projects.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

JsonCpp

Conan Center badge badge Coverage Status

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 deserialization/serialization steps, making it a convenient format to store user input files.

Project Status

JsonCpp is a mature project in maintenance mode. Our priority is providing a stable, reliable JSON library for the long tail of C++ development.

Current Focus

  • Security: Addressing vulnerabilities and fuzzing results.
  • Compatibility: Ensuring the library builds without warnings on the latest versions of GCC, Clang, and MSVC.
  • Reliability: Fixing regressions and critical logical bugs.

Out of Scope

  • Performance: We are not competing with SIMD-accelerated or reflection-based parsers.
  • Features: We are generally not accepting requests for new data formats or major API changes.

JsonCpp remains a primary choice for developers who require comment preservation and support for legacy toolchains where modern C++ standards are unavailable. The library is intended to be a reliable dependency that does not require frequent updates or major migration efforts.

A note on backward-compatibility

  • 1.y.z (master): Actively maintained. Requires C++11.

  • 0.y.z: Legacy support for pre-C++11 compilers. Maintenance is limited to critical security fixes.

  • 00.11.z: Discontinued.

Major versions maintain binary compatibility. Critical security fixes are accepted for both the master and 0.y.z branches.

Integration

[!NOTE] Package manager ports (vcpkg, Conan, etc.) are community-maintained. Please report outdated versions or missing generators to their respective repositories.

vcpkg

Add jsoncpp to your vcpkg.json manifest:

{
  "dependencies": ["jsoncpp"]
}

Or install via classic mode: vcpkg install jsoncpp.

Conan

conan install --requires="jsoncpp/[*]" --build=missing

If you are using a conanfile.txt in a Conan 2 project, ensure you use the appropriate generators:

[requires]
jsoncpp/[*]

[generators]
CMakeToolchain
CMakeDeps

Meson

meson wrap install jsoncpp

Amalgamated source

For projects requiring a single-header approach, JsonCpp provides a script to generate an amalgamated source and header file.

You can generate the amalgamated files by running the following Python script from the top-level directory:

python3 amalgamate.py

This will generate a dist directory containing jsoncpp.cpp, json/json.h, and json/json-forwards.h. You can then drop these files directly into your project's source tree and compile jsoncpp.cpp alongside your other source files.

Documentation

Documentation is generated via Doxygen. Additional information is available on the Project Wiki.

License

JsonCpp is licensed under the MIT license, or public domain where recognized. See LICENSE for details.