JSON Toolkit  2.0.0
The swiss-army knife for JSON programming in C++
JSON

A full-blown ECMA-404 implementation with read, write, and iterators support. More...

Classes

class  sourcemeta::jsontoolkit::JSONArray< Value >
 
class  sourcemeta::jsontoolkit::ParseError
 
class  sourcemeta::jsontoolkit::JSONObject< Key, Value >
 
class  sourcemeta::jsontoolkit::JSON
 

Typedefs

using sourcemeta::jsontoolkit::KeyComparison = std::function< bool(const JSON::String &, const JSON::String &)>
 

Functions

auto sourcemeta::jsontoolkit::parse (std::basic_istream< JSON::Char, JSON::CharTraits > &stream) -> JSON
 
auto sourcemeta::jsontoolkit::parse (const std::basic_string< JSON::Char, JSON::CharTraits > &input) -> JSON
 
auto sourcemeta::jsontoolkit::parse (std::basic_istream< JSON::Char, JSON::CharTraits > &stream, std::uint64_t &line, std::uint64_t &column) -> JSON
 
auto sourcemeta::jsontoolkit::parse (const std::basic_string< JSON::Char, JSON::CharTraits > &input, std::uint64_t &line, std::uint64_t &column) -> JSON
 
auto sourcemeta::jsontoolkit::from_file (const std::filesystem::path &path) -> JSON
 
auto sourcemeta::jsontoolkit::stringify (const JSON &document, std::basic_ostream< JSON::Char, JSON::CharTraits > &stream) -> void
 
auto sourcemeta::jsontoolkit::prettify (const JSON &document, std::basic_ostream< JSON::Char, JSON::CharTraits > &stream) -> void
 
auto sourcemeta::jsontoolkit::stringify (const JSON &document, std::basic_ostream< JSON::Char, JSON::CharTraits > &stream, const KeyComparison &compare) -> void
 
auto sourcemeta::jsontoolkit::prettify (const JSON &document, std::basic_ostream< JSON::Char, JSON::CharTraits > &stream, const KeyComparison &compare) -> void
 
auto sourcemeta::jsontoolkit::operator<< (std::basic_ostream< JSON::Char, JSON::CharTraits > &stream, const JSON &document) -> std::basic_ostream< JSON::Char, JSON::CharTraits > &
 
auto sourcemeta::jsontoolkit::operator<< (std::basic_ostream< JSON::Char, JSON::CharTraits > &stream, const JSON::Type type) -> std::basic_ostream< JSON::Char, JSON::CharTraits > &
 

Detailed Description

This functionality is included as follows:

#include <sourcemeta/jsontoolkit/json.h>

Typedef Documentation

◆ KeyComparison

using sourcemeta::jsontoolkit::KeyComparison = typedef std::function<bool(const JSON::String &, const JSON::String &)>

A comparison function between object property keys. See https://en.cppreference.com/w/cpp/named_req/Compare

Function Documentation

◆ from_file()

auto sourcemeta::jsontoolkit::from_file ( const std::filesystem::path &  path) -> JSON

A convenience function to create a JSON document from a file. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
#include <iostream>
std::cout << std::endl;
Definition: json_value.h:30
auto stringify(const JSON &document, std::basic_ostream< JSON::Char, JSON::CharTraits > &stream) -> void
auto from_file(const std::filesystem::path &path) -> JSON

If parsing fails, sourcemeta::jsontoolkit::ParseError will be thrown.

◆ operator<<() [1/2]

auto sourcemeta::jsontoolkit::operator<< ( std::basic_ostream< JSON::Char, JSON::CharTraits > &  stream,
const JSON document 
) -> std::basic_ostream< JSON::Char, JSON::CharTraits > &

Encode the input JSON document into a given standard output stream. The JSON document is stringified or prettified depending on the presence of the NDEBUG define (for debugging purposes). For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <iostream>
#include <sstream>
std::ostringstream stream;
stream << document;
std::cout << stream.str() << std::endl;
auto parse(std::basic_istream< JSON::Char, JSON::CharTraits > &stream) -> JSON

◆ operator<<() [2/2]

auto sourcemeta::jsontoolkit::operator<< ( std::basic_ostream< JSON::Char, JSON::CharTraits > &  stream,
const JSON::Type  type 
) -> std::basic_ostream< JSON::Char, JSON::CharTraits > &

Encode the input JSON type as a string into a given standard output stream. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <iostream>
#include <sstream>
std::ostringstream stream;
stream << sourcemeta::jsontoolkit::JSON::Type::String;
// Will print "string"
std::cout << stream.str() << std::endl;

◆ parse() [1/4]

auto sourcemeta::jsontoolkit::parse ( const std::basic_string< JSON::Char, JSON::CharTraits > &  input) -> JSON

Create a JSON document from a JSON string. For example, a JSON document that represents an array can be parsed as follows:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
assert(document.is_array());
auto is_array() const noexcept -> bool

If parsing fails, sourcemeta::jsontoolkit::ParseError will be thrown.

◆ parse() [2/4]

auto sourcemeta::jsontoolkit::parse ( const std::basic_string< JSON::Char, JSON::CharTraits > &  input,
std::uint64_t &  line,
std::uint64_t &  column 
) -> JSON

Create a JSON document from a JSON string, passing your own line and column read/write position indicators. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
std::uint64_t line{1};
std::uint64_t column{0};
sourcemeta::jsontoolkit::parse("[ 1, 2, 3 ]", line, column);
assert(document.is_array());

◆ parse() [3/4]

auto sourcemeta::jsontoolkit::parse ( std::basic_istream< JSON::Char, JSON::CharTraits > &  stream) -> JSON

Create a JSON document from a C++ standard input stream. For example, a JSON document that represents an array can be parsed as follows:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
#include <sstream>
std::istringstream stream{"[ 1, 2, 3 ]"};
assert(document.is_array());

If parsing fails, sourcemeta::jsontoolkit::ParseError will be thrown.

◆ parse() [4/4]

auto sourcemeta::jsontoolkit::parse ( std::basic_istream< JSON::Char, JSON::CharTraits > &  stream,
std::uint64_t &  line,
std::uint64_t &  column 
) -> JSON

Create a JSON document from a C++ standard input stream, passing your own line and column read/write position indicators. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
#include <sstream>
std::istringstream stream{"[ 1, 2, 3 ]"};
std::uint64_t line{1};
std::uint64_t column{0};
sourcemeta::jsontoolkit::parse(stream, line, column);
assert(document.is_array());

◆ prettify() [1/2]

auto sourcemeta::jsontoolkit::prettify ( const JSON document,
std::basic_ostream< JSON::Char, JSON::CharTraits > &  stream 
) -> void

Stringify the input JSON document into a given C++ standard output stream in pretty mode, indenting the output using 4 spaces. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <iostream>
#include <sstream>
std::ostringstream stream;
std::cout << stream.str() << std::endl;
auto prettify(const JSON &document, std::basic_ostream< JSON::Char, JSON::CharTraits > &stream) -> void

◆ prettify() [2/2]

auto sourcemeta::jsontoolkit::prettify ( const JSON document,
std::basic_ostream< JSON::Char, JSON::CharTraits > &  stream,
const KeyComparison compare 
) -> void

Stringify the input JSON document into a given C++ standard output stream in pretty mode, indenting the output using 4 spaces and sorting object properties on a specific criteria. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <iostream>
#include <sstream>
auto key_compare(const sourcemeta::jsontoolkit::JSON::String &left,
-> bool {
return left < right;
}
sourcemeta::jsontoolkit::parse("{ \"foo\": 1, \"bar\": 2, \"baz\": 3 }");
std::ostringstream stream;
sourcemeta::jsontoolkit::prettify(document, stream, key_compare);
std::cout << stream.str() << std::endl;
std::basic_string< Char, CharTraits, Allocator< Char > > String
The string type used by the JSON document.
Definition: json_value.h:43

◆ stringify() [1/2]

auto sourcemeta::jsontoolkit::stringify ( const JSON document,
std::basic_ostream< JSON::Char, JSON::CharTraits > &  stream 
) -> void

Stringify the input JSON document into a given C++ standard output stream in compact mode. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <iostream>
#include <sstream>
std::ostringstream stream;
std::cout << stream.str() << std::endl;

◆ stringify() [2/2]

auto sourcemeta::jsontoolkit::stringify ( const JSON document,
std::basic_ostream< JSON::Char, JSON::CharTraits > &  stream,
const KeyComparison compare 
) -> void

Stringify the input JSON document into a given C++ standard output stream in compact mode, sorting object properties on a specific criteria. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <iostream>
#include <sstream>
auto key_compare(const sourcemeta::jsontoolkit::JSON::String &left,
-> bool {
return left < right;
}
sourcemeta::jsontoolkit::parse("{ \"foo\": 1, \"bar\": 2, \"baz\": 3 }");
std::ostringstream stream;
sourcemeta::jsontoolkit::stringify(document, stream, key_compare);
std::cout << stream.str() << std::endl;