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::FileParseError
 
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::read_file (const std::filesystem::path &path) -> std::basic_ifstream< JSON::Char, JSON::CharTraits >
 
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

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

This functionality is included as follows:

#include <sourcemeta/jsontoolkit/json.h>

Class Documentation

◆ sourcemeta::jsontoolkit::JSONArray

class sourcemeta::jsontoolkit::JSONArray

template<typename Value>
class sourcemeta::jsontoolkit::JSONArray< Value >

Public Types

using Container = std::vector< Value, typename Value::template Allocator< Value > >
 
using value_type = typename Container::value_type
 
using allocator_type = typename Container::allocator_type
 
using size_type = typename Container::size_type
 
using difference_type = typename Container::difference_type
 
using reference = typename Container::reference
 
using const_reference = typename Container::const_reference
 
using pointer = typename Container::pointer
 
using const_pointer = typename Container::const_pointer
 
using iterator = typename Container::iterator
 
using const_iterator = typename Container::const_iterator
 
using reverse_iterator = typename Container::reverse_iterator
 
using const_reverse_iterator = typename Container::const_reverse_iterator
 

Public Member Functions

 JSONArray (std::initializer_list< Value > values)
 
auto operator< (const JSONArray< Value > &other) const noexcept -> bool
 
auto operator<= (const JSONArray< Value > &other) const noexcept -> bool
 
auto operator> (const JSONArray< Value > &other) const noexcept -> bool
 
auto operator>= (const JSONArray< Value > &other) const noexcept -> bool
 
auto operator== (const JSONArray< Value > &other) const noexcept -> bool
 
auto operator!= (const JSONArray< Value > &other) const noexcept -> bool
 
auto begin () noexcept -> iterator
 Get a mutable begin iterator on the array.
 
auto end () noexcept -> iterator
 Get a mutable end iterator on the array.
 
auto begin () const noexcept -> const_iterator
 Get a constant begin iterator on the array.
 
auto end () const noexcept -> const_iterator
 Get a constant end iterator on the array.
 
auto cbegin () const noexcept -> const_iterator
 Get a constant begin iterator on the array.
 
auto cend () const noexcept -> const_iterator
 Get a constant end iterator on the array.
 
auto rbegin () noexcept -> reverse_iterator
 Get a mutable reverse begin iterator on the array.
 
auto rend () noexcept -> reverse_iterator
 Get a mutable reverse end iterator on the array.
 
auto rbegin () const noexcept -> const_reverse_iterator
 Get a constant reverse begin iterator on the array.
 
auto rend () const noexcept -> const_reverse_iterator
 Get a constant reverse end iterator on the array.
 
auto crbegin () const noexcept -> const_reverse_iterator
 Get a constant reverse begin iterator on the array.
 
auto crend () const noexcept -> const_reverse_iterator
 Get a constant reverse end iterator on the array.
 

◆ sourcemeta::jsontoolkit::ParseError

class sourcemeta::jsontoolkit::ParseError

This class represents a parsing error

Inheritance diagram for sourcemeta::jsontoolkit::ParseError:
sourcemeta::jsontoolkit::FileParseError sourcemeta::jsontoolkit::PointerParseError

Public Member Functions

 ParseError (const std::uint64_t line, const std::uint64_t column)
 Create a parsing error.
 
auto what () const noexcept -> const char *override
 
auto line () const noexcept -> std::uint64_t
 Get the line number of the error.
 
auto column () const noexcept -> std::uint64_t
 

◆ sourcemeta::jsontoolkit::FileParseError

class sourcemeta::jsontoolkit::FileParseError

This class represents a parsing error occurring from parsing a file

Inheritance diagram for sourcemeta::jsontoolkit::FileParseError:
sourcemeta::jsontoolkit::ParseError

Public Member Functions

 FileParseError (const std::filesystem::path &path, const std::uint64_t line, const std::uint64_t column)
 Create a file parsing error.
 
 FileParseError (const std::filesystem::path &path, const ParseError &parent)
 Create a file parsing error from a parse error.
 
auto path () const noexcept -> const std::filesystem::path
 Get the fiel path of the error.
 
- Public Member Functions inherited from sourcemeta::jsontoolkit::ParseError
 ParseError (const std::uint64_t line, const std::uint64_t column)
 Create a parsing error.
 
auto what () const noexcept -> const char *override
 
auto line () const noexcept -> std::uint64_t
 Get the line number of the error.
 
auto column () const noexcept -> std::uint64_t
 

◆ sourcemeta::jsontoolkit::JSONObject

class sourcemeta::jsontoolkit::JSONObject

template<typename Key, typename Value>
class sourcemeta::jsontoolkit::JSONObject< Key, Value >

Public Types

using Container = std::unordered_map< Key, Value, std::hash< Key >, std::equal_to< Key >, typename Value::template Allocator< std::pair< const typename Value::String, Value > >>
 
using key_type = typename Container::key_type
 
using mapped_type = typename Container::mapped_type
 
using value_type = typename Container::value_type
 
using size_type = typename Container::size_type
 
using difference_type = typename Container::difference_type
 
using allocator_type = typename Container::allocator_type
 
using reference = typename Container::reference
 
using const_reference = typename Container::const_reference
 
using pointer = typename Container::pointer
 
using const_pointer = typename Container::const_pointer
 
using iterator = typename Container::iterator
 
using const_iterator = typename Container::const_iterator
 

Public Member Functions

 JSONObject (std::initializer_list< typename Container::value_type > values)
 
auto operator< (const JSONObject< Key, Value > &) const noexcept -> bool
 
auto operator<= (const JSONObject< Key, Value > &other) const noexcept -> bool
 
auto operator> (const JSONObject< Key, Value > &other) const noexcept -> bool
 
auto operator>= (const JSONObject< Key, Value > &other) const noexcept -> bool
 
auto operator== (const JSONObject< Key, Value > &other) const noexcept -> bool
 
auto operator!= (const JSONObject< Key, Value > &other) const noexcept -> bool
 
auto begin () noexcept -> iterator
 Get a mutable begin iterator on the object.
 
auto end () noexcept -> iterator
 Get a mutable end iterator on the object.
 
auto begin () const noexcept -> const_iterator
 Get a constant begin iterator on the object.
 
auto end () const noexcept -> const_iterator
 Get a constant end iterator on the object.
 
auto cbegin () const noexcept -> const_iterator
 Get a constant begin iterator on the object.
 
auto cend () const noexcept -> const_iterator
 Get a constant end iterator on the object.
 

◆ sourcemeta::jsontoolkit::JSON

class sourcemeta::jsontoolkit::JSON

Public Types

enum class  Type : std::uint8_t {
  Null = 0 , Boolean = 1 , Integer = 2 , Real = 3 ,
  String = 4 , Array = 5 , Object = 6
}
 The different types of a JSON instance.
 
using Char = char
 The character type used by the JSON document.
 
using CharTraits = std::char_traits< Char >
 The character traits used by the JSON document.
 
using Integer = std::int64_t
 The integer type used by the JSON document.
 
using Real = double
 The real type used by the JSON document.
 
template<typename T >
using Allocator = std::allocator< T >
 The allocator used by the JSON document.
 
using String = std::basic_string< Char, CharTraits, Allocator< Char > >
 The string type used by the JSON document.
 
using Array = JSONArray< JSON >
 The array type used by the JSON document.
 
using Object = JSONObject< String, JSON >
 The object type used by the JSON document.
 

Public Member Functions

 JSON (const std::int64_t value)
 
 JSON (const std::size_t value)
 
 JSON (const int value)
 
template<typename T = std::int64_t, typename = std::enable_if_t<!std::is_same_v<T, std::int64_t>>>
 JSON (const long value)
 
 JSON (const double value)
 
 JSON (const float value)
 
 JSON (const bool value)
 
 JSON (const std::nullptr_t)
 
 JSON (const String &value)
 
 JSON (const std::basic_string_view< Char, CharTraits > &value)
 
 JSON (const Char *const value)
 
 JSON (std::initializer_list< JSON > values)
 
 JSON (const Array &value)
 A copy constructor for the array type.
 
 JSON (std::initializer_list< typename Object::Container::value_type > values)
 
 JSON (const Object &value)
 A copy constructor for the object type.
 
auto operator< (const JSON &other) const noexcept -> bool
 
auto operator<= (const JSON &other) const noexcept -> bool
 
auto operator> (const JSON &other) const noexcept -> bool
 
auto operator>= (const JSON &other) const noexcept -> bool
 
auto operator== (const JSON &other) const noexcept -> bool
 
auto operator!= (const JSON &) const noexcept -> bool=default
 
auto operator+ (const JSON &other) const -> JSON
 
auto operator- (const JSON &other) const -> JSON
 
auto operator+= (const JSON &additive) -> JSON &
 
auto operator-= (const JSON &substractive) -> JSON &
 
auto is_boolean () const noexcept -> bool
 
auto is_null () const noexcept -> bool
 
auto is_integer () const noexcept -> bool
 
auto is_real () const noexcept -> bool
 
auto is_integer_real () const noexcept -> bool
 
auto is_number () const noexcept -> bool
 
auto is_positive () const noexcept -> bool
 
auto is_string () const noexcept -> bool
 
auto is_array () const noexcept -> bool
 
auto is_object () const noexcept -> bool
 
auto type () const noexcept -> Type
 
auto to_boolean () const noexcept -> bool
 
auto to_integer () const noexcept -> Integer
 
auto to_real () const noexcept -> Real
 
auto to_string () const noexcept -> const String &
 
auto to_string () noexcept -> String &
 
auto to_stringstream () const -> std::basic_istringstream< Char, CharTraits, Allocator< Char >>
 
auto as_array () const noexcept -> const Array &
 
auto as_array () noexcept -> Array &
 
auto as_object () noexcept -> Object &
 
auto as_object () const noexcept -> const Object &
 
auto as_real () const noexcept -> Real
 
auto as_integer () const noexcept -> Integer
 
auto at (const typename Array::size_type index) const -> const JSON &
 
auto at (const typename Array::size_type index) -> JSON &
 
auto at (const String &key) const -> const JSON &
 
auto at (const String &key) -> JSON &
 
auto front () -> JSON &
 
auto front () const -> const JSON &
 
auto back () -> JSON &
 
auto back () const -> const JSON &
 
auto size () const -> std::size_t
 
auto byte_size () const -> std::size_t
 
auto estimated_byte_size () const -> std::uint64_t
 
auto divisible_by (const JSON &divisor) const -> bool
 
auto empty () const -> bool
 
auto defines (const String &key) const -> bool
 
auto defines (const typename Array::size_type index) const -> bool
 
template<typename Iterator >
auto defines_any (Iterator begin, Iterator end) const -> bool
 
auto defines_any (std::initializer_list< String > keys) const -> bool
 
auto contains (const JSON &element) const -> bool
 
auto unique () const -> bool
 
auto push_back (const JSON &value) -> void
 
auto push_back (JSON &&value) -> void
 
auto push_back_if_unique (const JSON &value) -> std::pair< std::reference_wrapper< const JSON >, bool >
 
auto push_back_if_unique (JSON &&value) -> std::pair< std::reference_wrapper< const JSON >, bool >
 
auto assign (const String &key, const JSON &value) -> void
 
auto assign (const String &key, JSON &&value) -> void
 
auto assign_if_missing (const String &key, const JSON &value) -> void
 
auto assign_if_missing (const String &key, JSON &&value) -> void
 
auto erase (const String &key) -> typename Object::size_type
 
template<typename Iterator >
auto erase_keys (Iterator first, Iterator last) -> void
 
auto erase_keys (std::initializer_list< String > keys) -> void
 
auto erase (typename Array::const_iterator position) -> typename Array::iterator
 
auto erase (typename Array::const_iterator first, typename Array::const_iterator last) -> typename Array::iterator
 
auto clear () -> void
 
template<typename Iterator >
auto clear_except (Iterator first, Iterator last) -> void
 
auto clear_except (std::initializer_list< String > keys) -> void
 
auto into (const JSON &other) -> void
 
auto into (JSON &&other) noexcept -> void
 
auto into_array () -> void
 
auto into_object () -> void
 

Static Public Member Functions

static auto make_array () -> JSON
 
static auto make_object () -> JSON
 

Constructor & Destructor Documentation

◆ JSON() [1/12]

sourcemeta::jsontoolkit::JSON::JSON ( const std::int64_t  value)
explicit

This constructor creates a JSON document from an integer type. For example:

#include <sourcemeta/jsontoolkit/json.h>
const sourcemeta::jsontoolkit::JSON my_integer{4};
Definition: json_value.h:26

◆ JSON() [2/12]

sourcemeta::jsontoolkit::JSON::JSON ( const std::size_t  value)
explicit

This constructor creates a JSON document from an integer type. For example:

#include <sourcemeta/jsontoolkit/json.h>
const sourcemeta::jsontoolkit::JSON my_integer{4};

◆ JSON() [3/12]

sourcemeta::jsontoolkit::JSON::JSON ( const int  value)
explicit

This constructor creates a JSON document from an integer type. For example:

#include <sourcemeta/jsontoolkit/json.h>
const sourcemeta::jsontoolkit::JSON my_integer{4};

◆ JSON() [4/12]

sourcemeta::jsontoolkit::JSON::JSON ( const double  value)
explicit

This constructor creates a JSON document from an real number type. For example:

#include <sourcemeta/jsontoolkit/json.h>
const sourcemeta::jsontoolkit::JSON my_real{3.14};

◆ JSON() [5/12]

sourcemeta::jsontoolkit::JSON::JSON ( const float  value)
explicit

This constructor creates a JSON document from an real number type. For example:

#include <sourcemeta/jsontoolkit/json.h>
const sourcemeta::jsontoolkit::JSON my_real{3.14};

◆ JSON() [6/12]

sourcemeta::jsontoolkit::JSON::JSON ( const bool  value)
explicit

This constructor creates a JSON document from a boolean type. For example:

#include <sourcemeta/jsontoolkit/json.h>
const sourcemeta::jsontoolkit::JSON my_boolean{true};

◆ JSON() [7/12]

sourcemeta::jsontoolkit::JSON::JSON ( const std::nullptr_t  )
explicit

This constructor creates a JSON document from a null type. For example:

#include <sourcemeta/jsontoolkit/json.h>
const sourcemeta::jsontoolkit::JSON my_null{nullptr};

◆ JSON() [8/12]

sourcemeta::jsontoolkit::JSON::JSON ( const String value)
explicit

This constructor creates a JSON document from a string type. For example:

#include <sourcemeta/jsontoolkit/json.h>
const sourcemeta::jsontoolkit::JSON my_string{"foo"};

◆ JSON() [9/12]

sourcemeta::jsontoolkit::JSON::JSON ( const std::basic_string_view< Char, CharTraits > &  value)
explicit

This constructor creates a JSON document from a string type. For example:

#include <sourcemeta/jsontoolkit/json.h>
const sourcemeta::jsontoolkit::JSON my_string{"foo"};

◆ JSON() [10/12]

sourcemeta::jsontoolkit::JSON::JSON ( const Char *const  value)
explicit

This constructor creates a JSON document from a string type. For example:

#include <sourcemeta/jsontoolkit/json.h>
const sourcemeta::jsontoolkit::JSON my_string{"foo"};

◆ JSON() [11/12]

sourcemeta::jsontoolkit::JSON::JSON ( std::initializer_list< JSON values)
explicit

This constructor creates a JSON array from a set of other JSON documents. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
assert(my_array.is_array());

◆ JSON() [12/12]

sourcemeta::jsontoolkit::JSON::JSON ( std::initializer_list< typename Object::Container::value_type >  values)
explicit

This constructor creates a JSON object from a pair of other JSON documents. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
assert(my_object.is_object());

Member Function Documentation

◆ as_array() [1/2]

auto sourcemeta::jsontoolkit::JSON::as_array ( ) const -> const Array &
noexcept

Get the JSON document as an array instance. This is convenient for using constant iterators on the array. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <algorithm>
#include <iostream>
std::for_each(document.as_array().cbegin(),
document.as_array().cend(),
[](const auto &element) {
std::cout << "Element: "
<< element.to_integer()
<< "\n";
});
auto as_array() const noexcept -> const Array &
auto parse(std::basic_istream< JSON::Char, JSON::CharTraits > &stream) -> JSON

◆ as_array() [2/2]

auto sourcemeta::jsontoolkit::JSON::as_array ( ) -> Array &
noexcept

Get the JSON document as an array instance. This is convenient for using mutable iterators on the array. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <algorithm>
#include <iostream>
std::sort(document.as_array().begin(), document.as_array().end());

◆ as_integer()

auto sourcemeta::jsontoolkit::JSON::as_integer ( ) const -> Integer
noexcept

Get the JSON numeric document as an integer number if it is not one already. If the number is a real number, truncation will take place. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
const sourcemeta::jsontoolkit::JSON document{5.3};
assert(document.as_integer() == 5);

◆ as_object() [1/2]

auto sourcemeta::jsontoolkit::JSON::as_object ( ) const -> const Object &
noexcept

Get the JSON document as an object instance. This is convenient for using mutable iterators on the object. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <algorithm>
#include <iostream>
for (auto &[key, value] : document.as_object()) {
}
static auto make_object() -> JSON
auto as_object() noexcept -> Object &
auto assign(const String &key, const JSON &value) -> void

◆ as_object() [2/2]

auto sourcemeta::jsontoolkit::JSON::as_object ( ) -> Object &
noexcept

Get the JSON document as an object instance. This is convenient for using constant iterators on the object. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <algorithm>
#include <iostream>
std::for_each(document.as_object().cbegin(),
document.as_object().cend(),
[](const auto &pair) {
std::cout << "Value: "
<< pair.second.to_integer()
<< "\n";
});

◆ as_real()

auto sourcemeta::jsontoolkit::JSON::as_real ( ) const -> Real
noexcept

Get the JSON numeric document as a real number if it is not one already. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
assert(document.as_real() == 5.0);

◆ assign() [1/2]

auto sourcemeta::jsontoolkit::JSON::assign ( const String key,
const JSON value 
) -> void

This method sets or updates an object key. For example, an object can be updated to contain a new bar boolean member as follows:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
sourcemeta::jsontoolkit::parse("{ \"foo\": true }");
const sourcemeta::jsontoolkit::JSON value{false};
document.assign("bar", value);
assert(document.defines("foo"));
assert(document.defines("bar"));
auto defines(const String &key) const -> bool

◆ assign() [2/2]

auto sourcemeta::jsontoolkit::JSON::assign ( const String key,
JSON &&  value 
) -> void

This method sets or updates an object key. For example, an object can be updated to contain a new bar boolean member as follows:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
sourcemeta::jsontoolkit::parse("{ \"foo\": true }");
document.assign("bar", sourcemeta::jsontoolkit::JSON{false});
assert(document.defines("foo"));
assert(document.defines("bar"));

◆ assign_if_missing() [1/2]

auto sourcemeta::jsontoolkit::JSON::assign_if_missing ( const String key,
const JSON value 
) -> void

This method sets an object key if it is not already defined. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
sourcemeta::jsontoolkit::parse("{ \"foo\": true }");
document.assign_if_missing("foo", value_1);
document.assign_if_missing("bar", value_2);
assert(document.defines("foo"));
assert(document.at("foo").is_boolean());
assert(document.defines("bar"));
assert(document.at("bar").is_integer());
auto at(const typename Array::size_type index) const -> const JSON &
auto assign_if_missing(const String &key, const JSON &value) -> void

◆ assign_if_missing() [2/2]

auto sourcemeta::jsontoolkit::JSON::assign_if_missing ( const String key,
JSON &&  value 
) -> void

This method sets an object key if it is not already defined. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
sourcemeta::jsontoolkit::parse("{ \"foo\": true }");
assert(document.defines("foo"));
assert(document.at("foo").is_boolean());
assert(document.defines("bar"));
assert(document.at("bar").is_integer());

◆ at() [1/4]

auto sourcemeta::jsontoolkit::JSON::at ( const String key) -> JSON &

This method retrieves an object element.

For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
sourcemeta::jsontoolkit::parse("{ \"foo\": 1, \"bar\": 2 }");
assert(my_object.at("bar").to_integer() == 2);

◆ at() [2/4]

auto sourcemeta::jsontoolkit::JSON::at ( const String key) const -> const JSON &

This method retrieves an object element.

For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
sourcemeta::jsontoolkit::parse("{ \"foo\": 1, \"bar\": 2 }");
assert(my_object.at("bar").to_integer() == 2);

◆ at() [3/4]

auto sourcemeta::jsontoolkit::JSON::at ( const typename Array::size_type  index) -> JSON &

This method retrieves a element by its index. If the input JSON instance is an object, a property that corresponds to the stringified integer will be accessed.

For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
assert(my_array.at(1).to_integer() == 2);
sourcemeta::jsontoolkit::parse("{ \"1\": "foo" }");
assert(my_array.at(1).to_string() == "foo");

◆ at() [4/4]

auto sourcemeta::jsontoolkit::JSON::at ( const typename Array::size_type  index) const -> const JSON &

This method retrieves a element by its index. If the input JSON instance is an object, a property that corresponds to the stringified integer will be accessed.

For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
assert(my_array.at(1).to_integer() == 2);
sourcemeta::jsontoolkit::parse("{ \"1\": "foo" }");
assert(my_array.at(1).to_string() == "foo");

◆ back() [1/2]

auto sourcemeta::jsontoolkit::JSON::back ( ) -> JSON &

This method retrieves a reference to the last element of a JSON array. This method is undefined if the input JSON instance is an empty array. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
assert(document.back().to_integer() == 3);

◆ back() [2/2]

auto sourcemeta::jsontoolkit::JSON::back ( ) const -> const JSON &

This method retrieves a reference to the last element of a JSON array. This method is undefined if the input JSON instance is an empty array. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
assert(document.back().to_integer() == 3);

◆ byte_size()

auto sourcemeta::jsontoolkit::JSON::byte_size ( ) const -> std::size_t

If the input JSON instance is string, input JSON instance is a string, return its number of bytes. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
sourcemeta::jsontoolkit::parse("\"\\uD83D\\uDCA9\"")};
assert(my_string.size() == 2);

◆ clear()

auto sourcemeta::jsontoolkit::JSON::clear ( ) -> void

This method deletes all members of an object or all elements of an array, leaving them empty. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
sourcemeta::jsontoolkit::parse("{ \"foo\": true }");
my_object.clear();
my_array.clear();
assert(my_object.empty());
assert(my_array.empty());
auto empty() const -> bool

◆ clear_except() [1/2]

template<typename Iterator >
auto sourcemeta::jsontoolkit::JSON::clear_except ( Iterator  first,
Iterator  last 
) -> void
inline

This method deletes all members of an object except for the JSON keys declares as the second argument. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
#include <string>
#include <vector>
document.assign("foo", sourcemeta::jsontoolkit::JSON{true});
document.assign("bar", sourcemeta::jsontoolkit::JSON{false});
document.assign("baz", sourcemeta::jsontoolkit::JSON{true});
const std::vector<std::string> keys{"foo"};
document.clear_except(keys.cbegin(), keys.cend());
assert(document.defines("foo"));
assert(!document.defines("bar"));
assert(!document.defines("baz"));
auto clear_except(Iterator first, Iterator last) -> void
Definition: json_value.h:1225

◆ clear_except() [2/2]

auto sourcemeta::jsontoolkit::JSON::clear_except ( std::initializer_list< String keys) -> void

This method deletes all members of an object except for the JSON keys declares as the second argument. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
document.assign("foo", sourcemeta::jsontoolkit::JSON{true});
document.assign("bar", sourcemeta::jsontoolkit::JSON{false});
document.assign("baz", sourcemeta::jsontoolkit::JSON{true});
document.clear_except({ "foo" });
assert(document.defines("foo"));
assert(!document.defines("bar"));
assert(!document.defines("baz"));

◆ contains()

auto sourcemeta::jsontoolkit::JSON::contains ( const JSON element) const -> bool

This method checks if an JSON array contains a given JSON instance. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
auto contains(const JSON &element) const -> bool

◆ defines() [1/2]

auto sourcemeta::jsontoolkit::JSON::defines ( const String key) const -> bool

This method checks whether an input JSON object defines a specific key. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
sourcemeta::jsontoolkit::parse("{ \"foo\": 1 }");
assert(document.defines("foo"));
assert(!document.defines("bar"));

◆ defines() [2/2]

auto sourcemeta::jsontoolkit::JSON::defines ( const typename Array::size_type  index) const -> bool

This method checks whether an input JSON object defines a specific integer key. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
assert(document.defines(0));
assert(!document.defines(1));

◆ defines_any() [1/2]

template<typename Iterator >
auto sourcemeta::jsontoolkit::JSON::defines_any ( Iterator  begin,
Iterator  end 
) const -> bool
inline

This method checks whether an input JSON object defines at least one given key.

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
#include <string>
#include <vector>
sourcemeta::jsontoolkit::parse("{ \"foo\": true, \"bar\": false }");
const std::vector<std::string> keys{"foo", "qux"};
assert(document.defines_any(keys.cbegin(), keys.cend()));
auto defines_any(Iterator begin, Iterator end) const -> bool
Definition: json_value.h:895

◆ defines_any() [2/2]

auto sourcemeta::jsontoolkit::JSON::defines_any ( std::initializer_list< String keys) const -> bool

This method checks whether an input JSON object defines at least one given key.

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
sourcemeta::jsontoolkit::parse("{ \"foo\": true, \"bar\": false }");
assert(document.defines_any({ "foo", "qux" }));

◆ divisible_by()

auto sourcemeta::jsontoolkit::JSON::divisible_by ( const JSON divisor) const -> bool

Check whether a numeric instance is divisible by another numeric instance. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
const sourcemeta::jsontoolkit::JSON divisor{1.5};
assert(dividend.divisible_by(divisor));

◆ empty()

auto sourcemeta::jsontoolkit::JSON::empty ( ) const -> bool

A convenience method to check whether the input JSON document is an empty object, empty array or empty string.

For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
const sourcemeta::jsontoolkit::JSON my_string{""};
assert(my_object.empty());
assert(my_array.empty());
assert(my_string.empty());

◆ erase() [1/3]

auto sourcemeta::jsontoolkit::JSON::erase ( const String key) -> typename Object::size_type

This method deletes an object key. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
sourcemeta::jsontoolkit::parse("{ \"foo\": true }");
document.erase("foo");
assert(!document.defines("foo"));
auto erase(const String &key) -> typename Object::size_type

◆ erase() [2/3]

auto sourcemeta::jsontoolkit::JSON::erase ( typename Array::const_iterator  first,
typename Array::const_iterator  last 
) -> typename Array::iterator

This method deletes a set of array elements using iterators. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
#include <iterator>
array.erase(std::next(array.begin()), array.end());
assert(array.size(), 1);
assert(array.at(0), 1);
auto size() const -> std::size_t

◆ erase() [3/3]

auto sourcemeta::jsontoolkit::JSON::erase ( typename Array::const_iterator  position) -> typename Array::iterator

This method deletes an array element using an iterator. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
#include <iterator>
array.erase(std::next(array.begin()));
assert(array.size(), 2);
assert(array.at(0), 1);
assert(array.at(1), 3);

◆ erase_keys() [1/2]

template<typename Iterator >
auto sourcemeta::jsontoolkit::JSON::erase_keys ( Iterator  first,
Iterator  last 
) -> void
inline

This method deletes a set of object keys. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
#include <string>
#include <vector>
document.assign("foo", sourcemeta::jsontoolkit::JSON{true});
document.assign("bar", sourcemeta::jsontoolkit::JSON{false});
document.assign("baz", sourcemeta::jsontoolkit::JSON{true});
const std::vector<std::string> keys{"foo", "bar"};
document.erase_keys(keys.cbegin(), keys.cend());
assert(!document.defines("foo"));
assert(!document.defines("bar"));
assert(document.defines("baz"));
auto erase_keys(Iterator first, Iterator last) -> void
Definition: json_value.h:1124

◆ erase_keys() [2/2]

auto sourcemeta::jsontoolkit::JSON::erase_keys ( std::initializer_list< String keys) -> void

This method deletes a set of object keys. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
document.assign("foo", sourcemeta::jsontoolkit::JSON{true});
document.assign("bar", sourcemeta::jsontoolkit::JSON{false});
document.assign("baz", sourcemeta::jsontoolkit::JSON{true});
document.erase_keys({ "foo", "bar" });
assert(!document.defines("foo"));
assert(!document.defines("bar"));
assert(document.defines("baz"));

◆ estimated_byte_size()

auto sourcemeta::jsontoolkit::JSON::estimated_byte_size ( ) const -> std::uint64_t

Estimate the byte size occupied by the given parsed JSON instance (not its stringified representation). Keep in mind that as the method name implies, this is just a rough estimate. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
sourcemeta::jsontoolkit::parse("{ \"foo\": 1 }");
// Byte length of "foo" (3) + byte length of 1 (8)
assert(value.estimated_byte_size() == 11);
auto estimated_byte_size() const -> std::uint64_t

◆ front() [1/2]

auto sourcemeta::jsontoolkit::JSON::front ( ) -> JSON &

This method retrieves a reference to the first element of a JSON array. This method is undefined if the input JSON instance is an empty array. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
assert(document.front().to_integer() == 1);

◆ front() [2/2]

auto sourcemeta::jsontoolkit::JSON::front ( ) const -> const JSON &

This method retrieves a reference to the first element of a JSON array. This method is undefined if the input JSON instance is an empty array. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
assert(document.front().to_integer() == 1);

◆ into() [1/2]

auto sourcemeta::jsontoolkit::JSON::into ( const JSON other) -> void

This method sets a value to another JSON value by copying it. For example, the member of a JSON document can be transformed from a boolean to an integer as follows:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
sourcemeta::jsontoolkit::parse("{ \"foo\": true }");
document.at("foo").into(value);
assert(document.at("foo").is_integer());

◆ into() [2/2]

auto sourcemeta::jsontoolkit::JSON::into ( JSON &&  other) -> void
noexcept

This method sets a value to another JSON value. For example, the member of a JSON document can be transformed from a boolean to an integer as follows:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
sourcemeta::jsontoolkit::parse("{ \"foo\": true }");
document.at("foo").into(sourcemeta::jsontoolkit::JSON{2});
assert(document.at("foo").is_integer());

◆ into_array()

auto sourcemeta::jsontoolkit::JSON::into_array ( ) -> void

This method converts an existing JSON instance into an empty array. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
assert(document.is_boolean());
document.into_array();
assert(document.is_array());
assert(document.empty());

◆ into_object()

auto sourcemeta::jsontoolkit::JSON::into_object ( ) -> void

This method converts an existing JSON instance into an empty object. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
assert(document.is_boolean());
document.into_object();
assert(document.is_object());
assert(document.empty());

◆ is_array()

auto sourcemeta::jsontoolkit::JSON::is_array ( ) const -> bool
noexcept

Check if the input JSON document is an array. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
document=sourcemeta::jsontoolkit::parse("[ 1, 2, 3 ]");
assert(document.is_array());
auto is_array() const noexcept -> bool

◆ is_boolean()

auto sourcemeta::jsontoolkit::JSON::is_boolean ( ) const -> bool
noexcept

Check if the input JSON document is a boolean. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
const sourcemeta::jsontoolkit::JSON document{true};
assert(document.is_boolean());

◆ is_integer()

auto sourcemeta::jsontoolkit::JSON::is_integer ( ) const -> bool
noexcept

Check if the input JSON document is an integer. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
assert(document.is_integer());

◆ is_integer_real()

auto sourcemeta::jsontoolkit::JSON::is_integer_real ( ) const -> bool
noexcept

Check if the input JSON document is a real number that represents an integer. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
const sourcemeta::jsontoolkit::JSON document{5.0};
assert(document.is_integer_real());

◆ is_null()

auto sourcemeta::jsontoolkit::JSON::is_null ( ) const -> bool
noexcept

Check if the input JSON document is null. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
const sourcemeta::jsontoolkit::JSON document{nullptr};
assert(document.is_null());

◆ is_number()

auto sourcemeta::jsontoolkit::JSON::is_number ( ) const -> bool
noexcept

Check if the input JSON document is either an integer or a real type. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
assert(real.is_number());
assert(integer.is_number());

◆ is_object()

auto sourcemeta::jsontoolkit::JSON::is_object ( ) const -> bool
noexcept

Check if the input JSON document is an object. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
document=sourcemeta::jsontoolkit::parse("{ \"foo\": 1 }");
assert(document.is_object());
auto is_object() const noexcept -> bool

◆ is_positive()

auto sourcemeta::jsontoolkit::JSON::is_positive ( ) const -> bool
noexcept

Check if the input JSON document is either a positive integer or a positive real number. Zero is considered to be positive. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
assert(real.is_positive());
assert(!integer.is_positive());

◆ is_real()

auto sourcemeta::jsontoolkit::JSON::is_real ( ) const -> bool
noexcept

Check if the input JSON document is a real type. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
const sourcemeta::jsontoolkit::JSON document{3.14};
assert(document.is_real());

◆ is_string()

auto sourcemeta::jsontoolkit::JSON::is_string ( ) const -> bool
noexcept

Check if the input JSON document is a string. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
const sourcemeta::jsontoolkit::JSON document{"foo"};
assert(document.is_string());

◆ make_array()

static auto sourcemeta::jsontoolkit::JSON::make_array ( ) -> JSON
static

This function creates an empty JSON array. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
assert(document.is_array());
assert(document.empty());
static auto make_array() -> JSON

This function is particularly handy for programatically constructing arrays.

◆ make_object()

static auto sourcemeta::jsontoolkit::JSON::make_object ( ) -> JSON
static

This function creates an empty JSON object. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
assert(document.is_object());
assert(document.empty());

This function is particularly handy for programatically constructing objects.

◆ operator+()

auto sourcemeta::jsontoolkit::JSON::operator+ ( const JSON other) const -> JSON

Add two numeric JSON instances and get a new instance with the result. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
const sourcemeta::jsontoolkit::JSON result{left + right};
assert(result.is_integer());
assert(document.to_integer() == 8);

◆ operator+=()

auto sourcemeta::jsontoolkit::JSON::operator+= ( const JSON additive) -> JSON &

This operator adds a numeric JSON instance to another numeric JSON instance. For example, a numeric JSON instance 3.2 can be added to a numeric JSON instance 5 as follows:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
const sourcemeta::jsontoolkit::JSON additive{3.2};
document += additive;
assert(document.is_real());
assert(document.to_real() == 8.2);

◆ operator-()

auto sourcemeta::jsontoolkit::JSON::operator- ( const JSON other) const -> JSON

Substract two numeric JSON instances and get a new instance with the result. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
const sourcemeta::jsontoolkit::JSON result{left - right};
assert(result.is_integer());
assert(document.to_integer() == 2);

◆ operator-=()

auto sourcemeta::jsontoolkit::JSON::operator-= ( const JSON substractive) -> JSON &

This operator substracts a numeric JSON instance from another numeric JSON instance. For example, a numeric JSON instance 3.2 can be substracted from a numeric JSON instance 5 as follows:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
const sourcemeta::jsontoolkit::JSON substractive{3.2};
document -= substractive;
assert(document.is_real());
assert(document.to_real() == 1.8);

◆ push_back() [1/2]

auto sourcemeta::jsontoolkit::JSON::push_back ( const JSON value) -> void

This method inserts a new element to the end of the given array. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
document.push_back(value);
assert(document.size() == 4);
assert(document.back().to_integer() == 4);
auto push_back(const JSON &value) -> void

◆ push_back() [2/2]

auto sourcemeta::jsontoolkit::JSON::push_back ( JSON &&  value) -> void

This method inserts a new element to the end of the given array. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
assert(document.size() == 4);
assert(document.back().to_integer() == 4);

◆ push_back_if_unique() [1/2]

auto sourcemeta::jsontoolkit::JSON::push_back_if_unique ( const JSON value) -> std::pair< std::reference_wrapper< const JSON >, bool >

This method inserts a new element to the end of the given array if an equal element is not already present in the array. The return value is a pair consisting of a reference to the element in question and whether the element was inserted or not. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
const sourcemeta::jsontoolkit::JSON new_element{3};
const auto result{document.push_back_if_unique(new_element)};
assert(result.first.get().to_integer() == 3);
assert(!result.second);
auto push_back_if_unique(const JSON &value) -> std::pair< std::reference_wrapper< const JSON >, bool >

◆ push_back_if_unique() [2/2]

auto sourcemeta::jsontoolkit::JSON::push_back_if_unique ( JSON &&  value) -> std::pair< std::reference_wrapper< const JSON >, bool >

This method inserts a new element to the end of the given array if an equal element is not already present in the array. The return value is a pair consisting of a reference to the element in question and whether the element was inserted or not. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
#include <utility>
const auto result{document.push_back_if_unique(std::move(new_element)};
assert(result.first.get().to_integer() == 3);
assert(!result.second);

◆ size()

auto sourcemeta::jsontoolkit::JSON::size ( ) const -> std::size_t

If the input JSON instance is an object, return its number of pairs. If the input JSON instance is an array, return its number of elements. If the input JSON instance is a string, return its logical length.

For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
sourcemeta::jsontoolkit::parse("{ \"foo\": 1 }");
const sourcemeta::jsontoolkit::JSON my_string{"foo"};
assert(my_object.size() == 1);
assert(my_array.size() == 2);
assert(my_string.size() == 3);

◆ to_boolean()

auto sourcemeta::jsontoolkit::JSON::to_boolean ( ) const -> bool
noexcept

Convert a JSON instance into a boolean value. The result of this method is undefined unless the JSON instance holds a boolean value. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
const sourcemeta::jsontoolkit::JSON document{true};
assert(document.is_boolean());
assert(document.to_boolean());

◆ to_integer()

auto sourcemeta::jsontoolkit::JSON::to_integer ( ) const -> Integer
noexcept

Convert a JSON instance into a signed integer value. The result of this method is undefined unless the JSON instance holds an integer value. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
assert(document.is_integer());
assert(document.to_integer() == 5);

◆ to_real()

auto sourcemeta::jsontoolkit::JSON::to_real ( ) const -> Real
noexcept

Convert a JSON instance into an IEEE 64-bit floating-point value. The result of this method is undefined unless the JSON instance holds a real value. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
const sourcemeta::jsontoolkit::JSON document{3.14};
assert(document.is_real());
assert(document.to_real() == 3.14);

◆ to_string() [1/2]

auto sourcemeta::jsontoolkit::JSON::to_string ( ) const -> const String &
noexcept

Convert a JSON instance into a standard string value. The result of this method is undefined unless the JSON instance holds a string value. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
const sourcemeta::jsontoolkit::JSON document{"foo"};
assert(document.is_string());
assert(document.to_string() == "foo");

◆ to_string() [2/2]

auto sourcemeta::jsontoolkit::JSON::to_string ( ) -> String &
noexcept

Convert a JSON instance into a standard string value. The result of this method is undefined unless the JSON instance holds a string value. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
const sourcemeta::jsontoolkit::JSON document{"foo"};
assert(document.is_string());
assert(document.to_string() == "foo");

◆ to_stringstream()

auto sourcemeta::jsontoolkit::JSON::to_stringstream ( ) const -> std::basic_istringstream< Char, CharTraits, Allocator< Char >>

Get a standard input string stream from a JSON string. The result of this method is undefined unless the JSON instance holds a string value. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
const sourcemeta::jsontoolkit::JSON document{"foo"};
assert(document.is_string());
auto stream{document.to_stringstream()};
assert(stream.get() == 'f');

◆ type()

auto sourcemeta::jsontoolkit::JSON::type ( ) const -> Type
noexcept

Get the type of the JSON document. For example:

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
const sourcemeta::jsontoolkit::JSON document{true};
assert(document.type() == sourcemeta::jsontoolkit::JSON::Type::Boolean);

◆ unique()

auto sourcemeta::jsontoolkit::JSON::unique ( ) const -> bool

This method checks if an JSON array does not contain duplicated items. For example:

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

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;
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;

◆ 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());

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:39

◆ read_file()

auto sourcemeta::jsontoolkit::read_file ( const std::filesystem::path &  path) -> std::basic_ifstream< JSON::Char, JSON::CharTraits >

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

#include <sourcemeta/jsontoolkit/json.h>
#include <cassert>
#include <iostream>
auto stream = sourcemeta::jsontoolkit::read_file("/tmp/foo.json");
const auto document = sourcemeta::jsontoolkit::parse(stream);
std::cout << std::endl;
auto read_file(const std::filesystem::path &path) -> std::basic_ifstream< JSON::Char, JSON::CharTraits >

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

◆ 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;