Introduction to PicoJSON
masterstd::vector and JSON objects as std::map.repository·master·Indexed 22 days ago
https://github.com/kazuho/picojsonA 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.
std::vector and JSON objects as std::map.PicoJSON provides several ways to parse JSON into a picojson::value object using a pull interface:
parse: The simplest method, taking a picojson::value and a std::string.parse: Accepts a pair of iterators (e.g., char* or std::istream_iterator) and returns the end position of the input.>> 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();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:
picojson::value(int64_t).is<int64_t>() and get<int64_t>().int64_t bounds and do not use decimal points (.) or scientific notation (e/E) are treated as int64_t.double (i.e., is<int64_t>() == true implies is<double>() == true).int64_t values are converted to double if get<double>() is called.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>().
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>()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;
}You can convert a picojson::value back into JSON format using three methods:
std::ostream: Use the << operator.std::string: Call the .serialize() method..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));