PicoJSON Documentation

repository·master·Indexed 22 days ago

https://github.com/kazuho/picojson

A tiny, header-only C++ JSON parser and serializer. PicoJSON is lightweight, dependency-free, and compatible with the C++ Standard Template Library (STL), representing JSON arrays as std::vector and objects as std::map. It provides a pull interface for parsing into picojson::value objects and supports serialization to std::ostream, std::string, or output iterators. Optional experimental int64_t support is available via the PICOJSON_USE_INT64 macro.

Tokens
1.4K
Snippets
3
Records
5
Agent score
28%

What's inside PicoJSON

  1. Introduction to PicoJSON

    master
    PicoJSON is a lightweight, header-only C++ JSON parser and serializer. It has no external dependencies and relies solely on standard C++ libraries. It is designed to be STL-friendly, representing JSON arrays as std::vector and JSON objects as std::map.
  2. Parse JSON using the pull (DOM-like) interface

    master

    PicoJSON provides several ways to parse JSON into a picojson::value object using a pull interface:

    1. Two-argument parse: The simplest method, taking a picojson::value and a std::string.
    2. Four-argument parse: Accepts a pair of iterators (e.g., char* or std::istream_iterator) and returns the end position of the input.
    3. >> operator: Allows parsing directly from an input stream (e.g., std::cin >> v), though this interface is not thread-safe.

    Always check picojson::get_last_error() or the returned error string to verify if parsing succeeded.

    // Using the two-argument parse function
    std::string json = "[ \"hello JSON\" ]";
    picojson::value v;
    std::string err = picojson::parse(v, json);
    if (! err.empty()) {
      std::cerr << err << std::endl;
    }
    
    // Using the four-argument parse function with iterators
    const char* json_ptr = "{\"a\":1}";
    picojson::value v2;
    std::string err2;
    const char* json_end = picojson::parse(v2, json_ptr, json_ptr + strlen(json_ptr), &err2);
    if (! err2.empty()) {
      std::cerr << err2 << std::endl;
    }
    
    // Using the >> operator (not thread-safe)
    picojson::value v3;
    std::cin >> v3;
    std::string err3 = picojson::get_last_error();
  3. Enable experimental int64_t support

    master

    By default, PicoJSON treats numbers as double. You can enable experimental support for int64_t by defining the preprocessor macro PICOJSON_USE_INT64 during compilation.

    Changes when enabled:

    • New constructor: picojson::value(int64_t).
    • New type checks and accessors: is<int64_t>() and get<int64_t>().
    • Numeric values in JSON that are within int64_t bounds and do not use decimal points (.) or scientific notation (e/E) are treated as int64_t.
    • These values remain compatible with double (i.e., is<int64_t>() == true implies is<double>() == true).
    • int64_t values are converted to double if get<double>() is called.
  4. Access values in a picojson::value

    master

    JSON values are stored in picojson::value instances. To prevent undefined behavior, you must perform a type check using the is<T>() methods before accessing the data with get<T>().

    Supported Types and Methods

    • null: is<picojson::null>()
    • bool: is<bool>(), get<bool>()
    • double: is<double>(), get<double>()
    • std::string: is<std::string>(), get<std::string>()
    • array (std::vector<picojson::value>): is<array>(), get<array>()
    • object (std::map<std::string, picojson::value>): is<object>(), get<object>()

    Example: Iterating through a JSON object

    picojson::value v;
    // ... assume v is parsed and is an object ...
    if (! v.is<picojson::object>()) {
      std::cerr << "JSON is not an object" << std::endl;
      exit(2);
    }
    
    const picojson::value::object& obj = v.get<picojson::object>();
    for (picojson::value::object::const_iterator i = obj.begin(); i != obj.end(); ++i) {
      std::cout << i->first << ': ' << i->second.to_str() << std::endl;
    }
    picojson::value v;
    std::cin >> v;
    std::string err = picojson::get_last_error();
    if (! err.empty()) {
      std::cerr << err << std::endl;
      exit(1);
    }
    
    if (! v.is<picojson::object>()) {
      std::cerr << "JSON is not an object" << std::endl;
      exit(2);
    }
    
    const picojson::value::object& obj = v.get<picojson::object>();
    for (picojson::value::object::const_iterator i = obj.begin(); i != obj.end(); ++i) {
      std::cout << i->first << ': ' << i->second.to_str() << std::endl;
    }
  5. Serialize picojson::value to JSON

    master

    You can convert a picojson::value back into JSON format using three methods:

    1. To std::ostream: Use the << operator.
    2. To std::string: Call the .serialize() method.
    3. To an output iterator: Call .serialize(iterator).

    Additionally, .to_str() is available for casual/quick string representation.

    picojson::value v;
    // ... populate v ...
    
    // 1. To ostream
    std::cout << v;
    
    // 2. To std::string
    std::string json = v.serialize();
    
    // 3. To an output iterator
    v.serialize(std::ostream_iterator<char>(std::cout));