Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
JSON

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

Concepts

concept  sourcemeta::core::json_auto_has_mapped_type
concept  sourcemeta::core::json_auto_has_method_from
concept  sourcemeta::core::json_auto_has_method_to
concept  sourcemeta::core::json_auto_supports_auto
concept  sourcemeta::core::json_auto_list_like
concept  sourcemeta::core::json_auto_map_like
concept  sourcemeta::core::json_auto_has_reverse_iterator
concept  sourcemeta::core::json_auto_tuple_mono
concept  sourcemeta::core::json_auto_tuple_poly

Classes

class  sourcemeta::core::JSONArray< Value >
struct  sourcemeta::core::json_auto_is_basic_string< T >
struct  sourcemeta::core::json_auto_is_basic_string_view< T >
struct  sourcemeta::core::json_auto_is_bitset< T >
struct  sourcemeta::core::json_auto_bitset_size< T >
struct  sourcemeta::core::json_auto_is_pair< T >
struct  sourcemeta::core::json_auto_is_pair< std::pair< U, V > >
struct  sourcemeta::core::json_auto_is_variant< T >
struct  sourcemeta::core::json_auto_is_variant< std::variant< Ts... > >
class  sourcemeta::core::JSONParseError
class  sourcemeta::core::JSONFileParseError
struct  sourcemeta::core::HashJSON< T >
struct  sourcemeta::core::PropertyHashJSON< T >
struct  sourcemeta::core::EqualJSON< T >
class  sourcemeta::core::JSONObject< Key, Value, Hash >
class  sourcemeta::core::JSON

Functions

SOURCEMETA_CORE_JSON_EXPORT auto sourcemeta::core::parse_json (std::basic_istream< JSON::Char, JSON::CharTraits > &stream) -> JSON
SOURCEMETA_CORE_JSON_EXPORT auto sourcemeta::core::parse_json (const std::basic_string_view< JSON::Char, JSON::CharTraits > input) -> JSON
SOURCEMETA_CORE_JSON_EXPORT auto sourcemeta::core::try_parse_json (const std::basic_string_view< JSON::Char, JSON::CharTraits > input) -> std::optional< JSON >
SOURCEMETA_CORE_JSON_EXPORT auto sourcemeta::core::parse_json (std::basic_istream< JSON::Char, JSON::CharTraits > &stream, std::uint64_t &line, std::uint64_t &column) -> JSON
SOURCEMETA_CORE_JSON_EXPORT auto sourcemeta::core::parse_json (const std::basic_string_view< JSON::Char, JSON::CharTraits > input, std::uint64_t &line, std::uint64_t &column) -> JSON
SOURCEMETA_CORE_JSON_EXPORT auto sourcemeta::core::read_json (const std::filesystem::path &path) -> JSON
SOURCEMETA_CORE_JSON_EXPORT auto sourcemeta::core::parse_json (std::basic_istream< JSON::Char, JSON::CharTraits > &stream, JSON &output, const JSON::ParseCallback &callback) -> void
SOURCEMETA_CORE_JSON_EXPORT auto sourcemeta::core::parse_json (const std::basic_string_view< JSON::Char, JSON::CharTraits > input, JSON &output, const JSON::ParseCallback &callback) -> void
SOURCEMETA_CORE_JSON_EXPORT auto sourcemeta::core::parse_json (std::basic_istream< JSON::Char, JSON::CharTraits > &stream, std::uint64_t &line, std::uint64_t &column, JSON &output, const JSON::ParseCallback &callback) -> void
SOURCEMETA_CORE_JSON_EXPORT auto sourcemeta::core::parse_json (const std::basic_string_view< JSON::Char, JSON::CharTraits > input, std::uint64_t &line, std::uint64_t &column, JSON &output, const JSON::ParseCallback &callback) -> void
SOURCEMETA_CORE_JSON_EXPORT auto sourcemeta::core::read_json (const std::filesystem::path &path, JSON &output, const JSON::ParseCallback &callback) -> void
SOURCEMETA_CORE_JSON_EXPORT auto sourcemeta::core::stringify (const JSON &document, std::basic_ostream< JSON::Char, JSON::CharTraits > &stream) -> void
SOURCEMETA_CORE_JSON_EXPORT auto sourcemeta::core::prettify (const JSON &document, std::basic_ostream< JSON::Char, JSON::CharTraits > &stream, const std::size_t spaces=2) -> void
SOURCEMETA_FORCEINLINE auto sourcemeta::core::make_set (std::initializer_list< JSON::Type > types) -> JSON::TypeSet
template<typename T>
auto sourcemeta::core::to_json (const T &value) -> JSON
template<typename T>
auto sourcemeta::core::from_json (const JSON &value) -> std::optional< T >
template<typename T>
auto sourcemeta::core::to_json (const T value) -> JSON
template<typename T>
auto sourcemeta::core::to_json (const std::optional< T > &value) -> JSON
template<json_auto_list_like T>
auto sourcemeta::core::to_json (typename T::const_iterator begin, typename T::const_iterator end) -> JSON
template<json_auto_list_like T, std::invocable< const typename T::value_type & > F>
auto sourcemeta::core::to_json (typename T::const_iterator begin, typename T::const_iterator end, const F &callback) -> JSON
template<json_auto_list_like T, std::invocable< const typename T::value_type & > F>
auto sourcemeta::core::to_json (const T &value, const F &callback) -> JSON
template<json_auto_list_like T>
auto sourcemeta::core::from_json (const JSON &value, const std::function< std::optional< typename T::value_type >(const JSON &)> &callback) -> std::optional< T >
template<json_auto_map_like T>
auto sourcemeta::core::from_json (const JSON &value, const std::function< std::optional< typename T::mapped_type >(const JSON &)> &callback) -> std::optional< T >
template<typename L, typename R>
auto sourcemeta::core::to_json (const std::pair< L, R > &value) -> JSON

Detailed Description

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

This functionality is included as follows:

#include <sourcemeta/core/json.h>

Class Documentation

◆ sourcemeta::core::JSONArray

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

An ordered sequence of JSON values

Public Types

using Container
 The underlying container type that holds the array elements.

Public Member Functions

 JSONArray (std::initializer_list< Value > values)
 Construct an array from a list of values.
auto size () const noexcept -> size_type
 Get array size.
auto reserve (const size_type capacity) -> void
 Reserve capacity for a given number of elements.

Member Typedef Documentation

◆ Container

template<typename Value>
using sourcemeta::core::JSONArray< Value >::Container
Initial value:
std::vector<Value, typename Value::template Allocator<Value>>

The underlying container type that holds the array elements.

◆ sourcemeta::core::json_auto_is_basic_string

struct sourcemeta::core::json_auto_is_basic_string
template<typename T>
struct sourcemeta::core::json_auto_is_basic_string< T >
Inheritance diagram for sourcemeta::core::json_auto_is_basic_string< T >:

◆ sourcemeta::core::json_auto_is_basic_string_view

struct sourcemeta::core::json_auto_is_basic_string_view
template<typename T>
struct sourcemeta::core::json_auto_is_basic_string_view< T >
Inheritance diagram for sourcemeta::core::json_auto_is_basic_string_view< T >:

◆ sourcemeta::core::json_auto_is_bitset

struct sourcemeta::core::json_auto_is_bitset
template<typename T>
struct sourcemeta::core::json_auto_is_bitset< T >
Inheritance diagram for sourcemeta::core::json_auto_is_bitset< T >:

◆ sourcemeta::core::json_auto_bitset_size

struct sourcemeta::core::json_auto_bitset_size
template<typename T>
struct sourcemeta::core::json_auto_bitset_size< T >

◆ sourcemeta::core::json_auto_is_pair

struct sourcemeta::core::json_auto_is_pair
template<typename T>
struct sourcemeta::core::json_auto_is_pair< T >
Inheritance diagram for sourcemeta::core::json_auto_is_pair< T >:

◆ sourcemeta::core::json_auto_is_pair< std::pair< U, V > >

struct sourcemeta::core::json_auto_is_pair< std::pair< U, V > >
template<typename U, typename V>
struct sourcemeta::core::json_auto_is_pair< std::pair< U, V > >
Inheritance diagram for sourcemeta::core::json_auto_is_pair< std::pair< U, V > >:

◆ sourcemeta::core::json_auto_is_variant

struct sourcemeta::core::json_auto_is_variant
template<typename T>
struct sourcemeta::core::json_auto_is_variant< T >
Inheritance diagram for sourcemeta::core::json_auto_is_variant< T >:

◆ sourcemeta::core::json_auto_is_variant< std::variant< Ts... > >

struct sourcemeta::core::json_auto_is_variant< std::variant< Ts... > >
template<typename... Ts>
struct sourcemeta::core::json_auto_is_variant< std::variant< Ts... > >
Inheritance diagram for sourcemeta::core::json_auto_is_variant< std::variant< Ts... > >:

◆ sourcemeta::core::JSONParseError

class sourcemeta::core::JSONParseError

This class represents a parsing error

Inheritance diagram for sourcemeta::core::JSONParseError:
sourcemeta::core::JSONFileParseError sourcemeta::core::PointerParseError

Public Member Functions

 JSONParseError (const std::uint64_t line, const std::uint64_t column)
 Create a parsing error.
 JSONParseError (const std::uint64_t line, const std::uint64_t column, const char *message)
 Create a parsing error with a custom error.
 JSONParseError (const std::uint64_t line, const std::uint64_t column, std::string message)=delete
 JSONParseError (const std::uint64_t line, const std::uint64_t column, std::string &&message)=delete
 JSONParseError (const std::uint64_t line, const std::uint64_t column, std::string_view message)=delete
auto line () const noexcept -> std::uint64_t
 Get the line number of the error.
auto column () const noexcept -> std::uint64_t
 Get the column number of the error.

◆ sourcemeta::core::JSONFileParseError

class sourcemeta::core::JSONFileParseError

This class represents a parsing error occurring from parsing a file

Inheritance diagram for sourcemeta::core::JSONFileParseError:
sourcemeta::core::JSONParseError

Public Member Functions

 JSONFileParseError (std::filesystem::path path, const std::uint64_t line, const std::uint64_t column, const char *message)
 Create a file parsing error.
 JSONFileParseError (std::filesystem::path path, const std::uint64_t line, const std::uint64_t column, std::string message)=delete
 JSONFileParseError (std::filesystem::path path, const std::uint64_t line, const std::uint64_t column, std::string &&message)=delete
 JSONFileParseError (std::filesystem::path path, const std::uint64_t line, const std::uint64_t column, std::string_view message)=delete
 JSONFileParseError (std::filesystem::path path, const JSONParseError &parent)
 Create a file parsing error from a parse error.
auto path () const noexcept -> const std::filesystem::path &
 Get the file path of the error.
Public Member Functions inherited from sourcemeta::core::JSONParseError
 JSONParseError (const std::uint64_t line, const std::uint64_t column)
 Create a parsing error.
 JSONParseError (const std::uint64_t line, const std::uint64_t column, const char *message)
 Create a parsing error with a custom error.
 JSONParseError (const std::uint64_t line, const std::uint64_t column, std::string message)=delete
 JSONParseError (const std::uint64_t line, const std::uint64_t column, std::string &&message)=delete
 JSONParseError (const std::uint64_t line, const std::uint64_t column, std::string_view message)=delete
auto line () const noexcept -> std::uint64_t
 Get the line number of the error.
auto column () const noexcept -> std::uint64_t
 Get the column number of the error.

◆ sourcemeta::core::HashJSON

struct sourcemeta::core::HashJSON
template<typename T>
struct sourcemeta::core::HashJSON< T >

A hash function object for JSON values

Public Member Functions

auto is_perfect (const hash_type) const noexcept -> bool
 Check whether the given hash is a perfect hash.

◆ sourcemeta::core::PropertyHashJSON

struct sourcemeta::core::PropertyHashJSON
template<typename T>
struct sourcemeta::core::PropertyHashJSON< T >

A hash function object for JSON object property keys

Public Member Functions

constexpr auto perfect (const char *data, const std::size_t size) const noexcept -> hash_type
 Compute a perfect hash from raw data.
constexpr auto is_perfect (const hash_type &hash) const noexcept -> bool
 Check whether the given hash is a perfect hash.

◆ sourcemeta::core::EqualJSON

struct sourcemeta::core::EqualJSON
template<typename T>
struct sourcemeta::core::EqualJSON< T >

Until C++26, std::reference_wrapper does not overload operator==, so we need custom comparisons for use in i.e. unordered_set See https://en.cppreference.com/w/cpp/utility/functional/reference_wrapper/operator_cmp.html

◆ sourcemeta::core::JSONObject

class sourcemeta::core::JSONObject
template<typename Key, typename Value, typename Hash>
class sourcemeta::core::JSONObject< Key, Value, Hash >

A JSON object mapping property keys to values

Public Types

using KeyView
 The string view type used to look up object keys.

Public Member Functions

 JSONObject (std::initializer_list< pair_value_type > entries)
 Construct an object from a list of key and value pairs.
auto find (const Key &key) const -> const_iterator
 Attempt to find an entry by key.
template<typename T>
auto find (T key) const -> const_iterator
 Attempt to find an entry by key.
auto defines (const Key &key, const hash_type hash) const -> bool
 Check if an entry with the given key exists.
template<typename T>
auto defines (T key, const hash_type hash) const -> bool
 Check if an entry with the given key exists.
auto size () const -> std::size_t
 Check the size of the object.
auto empty () const -> bool
 Check if the object is empty.
auto reserve (const size_type capacity) -> void
 Reserve capacity for a given number of entries.
auto at (const size_type index) const -> const Entry &
 Access an object entry by its underlying positional index.
auto at (const Key &key, const hash_type key_hash) const -> const mapped_type &
 Access an object entry by its key name.
template<typename T>
auto at (T key, const hash_type key_hash) const -> const mapped_type &
 Access an object entry by its key name.
auto at (const Key &key, const hash_type key_hash) -> mapped_type &
 Access an object entry by its key name.
template<typename T>
auto at (T key, const hash_type key_hash) -> mapped_type &
 Access an object entry by its key name.
auto try_at (const Key &key, const hash_type key_hash) -> mapped_type *
 Try to access an object entry by its key name.
template<typename T>
auto try_at (T key, const hash_type key_hash) -> mapped_type *
 Try to access an object entry by its key name.
auto try_at (const Key &key, const hash_type key_hash) const -> const mapped_type *
 Try to access an object entry by its underlying positional index.
template<typename T>
auto try_at (T key, const hash_type key_hash) const -> const mapped_type *
 Try to access an object entry by its underlying positional index.
auto try_at (const Key &key, const hash_type key_hash, size_type &start) const -> const mapped_type *
template<typename T>
auto try_at (T key, const hash_type key_hash, size_type &start) const -> const mapped_type *
auto try_emplace_before (const Key &key, const mapped_type &value, const Key &suffix) -> hash_type
 Try to emplace a property before another property.
auto emplace (Key &&key, mapped_type &&value) -> hash_type
 Emplace an object property.
auto emplace (const Key &key, mapped_type &&value) -> hash_type
 Emplace an object property.
auto emplace (const Key &key, const mapped_type &value) -> hash_type
 Emplace an object property.
auto emplace_assume_new (Key &&key, mapped_type &&value) -> hash_type
 Emplace an object property assuming the key does not already exist.
auto emplace_assume_new (const Key &key, mapped_type &&value) -> hash_type
 Emplace an object property assuming the key does not already exist.
auto emplace_assume_new (Key &&key, mapped_type &&value, const hash_type key_hash) -> mapped_type &
auto emplace_assume_new (const Key &key, mapped_type &&value, const hash_type key_hash) -> void
 Emplace an object property with a pre-computed hash.
auto back_key () const noexcept -> const Key &
 Get the key of the last-inserted property.
auto clear () noexcept -> void
 Remove every property in the object.
auto rename (const Key &key, const hash_type key_hash, Key &&to, const hash_type to_hash) -> void
 Rename an object property in place.
auto erase (const Key &key, const hash_type key_hash) -> size_type
 Erase an object property.
template<typename T>
auto erase (T key, const hash_type key_hash) -> size_type
 Erase an object property.
auto erase (const Key &key) -> size_type
 Erase an object property.
template<typename T>
auto erase (T key) -> size_type
 Erase an object property.
template<typename Compare>
auto reorder (const Compare &compare) -> void
 Reorder object properties by keys according to a comparator function.

Static Public Member Functions

static constexpr auto hash (const Key &key) noexcept -> hash_type
 Compute a hash for a key.
template<typename T>
static constexpr auto hash (T key) noexcept -> hash_type
 Compute a hash for a key.
static constexpr auto hash (const char *raw_data, const std::size_t raw_size) noexcept -> hash_type
 Compute a hash from raw data.

Member Typedef Documentation

◆ KeyView

template<typename Key, typename Value, typename Hash>
using sourcemeta::core::JSONObject< Key, Value, Hash >::KeyView
Initial value:
std::basic_string_view<typename Key::value_type,
typename Key::traits_type>

The string view type used to look up object keys.

Member Function Documentation

◆ emplace_assume_new()

template<typename Key, typename Value, typename Hash>
auto sourcemeta::core::JSONObject< Key, Value, Hash >::emplace_assume_new ( Key && key,
mapped_type && value,
const hash_type key_hash ) -> mapped_type &
inline

Emplace an object property with a pre-computed hash, returning the inserted value

◆ try_at() [1/2]

template<typename Key, typename Value, typename Hash>
auto sourcemeta::core::JSONObject< Key, Value, Hash >::try_at ( const Key & key,
const hash_type key_hash,
size_type & start ) const -> const mapped_type *
inlinenodiscard

Try to access an object entry, scanning from a caller-provided start offset. On hit, advances start past the found index

◆ try_at() [2/2]

template<typename Key, typename Value, typename Hash>
template<typename T>
auto sourcemeta::core::JSONObject< Key, Value, Hash >::try_at ( T key,
const hash_type key_hash,
size_type & start ) const -> const mapped_type *
inlinenodiscard

Try to access an object entry, scanning from a caller-provided start offset. On hit, advances start past the found index

◆ sourcemeta::core::JSON

class sourcemeta::core::JSON

This class represents a JSON document.

Public Types

enum class  ParsePhase : std::uint8_t { Pre , Post }
 The parsing phase of a JSON document. More...
enum class  Type : std::uint8_t {
  Null = 0 , Boolean = 1 , Integer = 2 , Real = 3 ,
  String = 4 , Array = 5 , Object = 6 , Decimal = 7
}
 The different types of a JSON instance. More...
enum class  ParseContext : std::uint8_t { Root , Property , Index }
 The context type for parse callbacks. More...
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 StringView = std::basic_string_view<Char, CharTraits>
 The string view type used by the JSON document.
using Array = JSONArray<JSON>
 The array type used by the JSON document.
using Object = JSONObject<String, JSON, PropertyHashJSON<JSON::String>>
 The object type used by the JSON document.
using TypeSet = std::bitset<8>
 A set of types.
using ParseCallback
using KeyComparison = std::function<bool(const String &, const String &)>

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>
requires (!std::is_same_v<T, std::int64_t>)
 JSON (const long value)
 This constructor creates a JSON document from an integer type.
 JSON (const double value)
 JSON (const float value)
 JSON (const bool value)
 JSON (const std::nullptr_t)
 JSON (const String &value)
 JSON (String &&value)
 JSON (const std::basic_string_view< Char, CharTraits > &value)
 JSON (const Char *const value)
 JSON (const Array &value)
 A copy constructor for the array type.
 JSON (std::initializer_list< typename Object::pair_value_type > values)
 JSON (const Object &value)
 A copy constructor for the object type.
 JSON (const Decimal &value)
 A copy constructor for the decimal type.
 JSON (Decimal &&value)
 A move constructor for the decimal type.
 JSON (const JSON &)
 Misc constructors.
 JSON (JSON &&) noexcept
 A move constructor.
 ~JSON ()
 Destructor.
SOURCEMETA_FORCEINLINE auto is_boolean () const noexcept -> bool
SOURCEMETA_FORCEINLINE auto is_null () const noexcept -> bool
SOURCEMETA_FORCEINLINE auto is_integer () const noexcept -> bool
SOURCEMETA_FORCEINLINE auto is_real () const noexcept -> bool
SOURCEMETA_FORCEINLINE auto is_integral () const noexcept -> bool
SOURCEMETA_FORCEINLINE auto is_number () const noexcept -> bool
auto is_positive () const noexcept -> bool
SOURCEMETA_FORCEINLINE auto is_string () const noexcept -> bool
SOURCEMETA_FORCEINLINE auto is_array () const noexcept -> bool
SOURCEMETA_FORCEINLINE auto is_object () const noexcept -> bool
SOURCEMETA_FORCEINLINE auto is_decimal () const noexcept -> bool
SOURCEMETA_FORCEINLINE auto type () const noexcept -> Type
SOURCEMETA_FORCEINLINE auto to_boolean () const noexcept -> bool
SOURCEMETA_FORCEINLINE auto to_integer () const noexcept -> Integer
SOURCEMETA_FORCEINLINE auto to_real () const noexcept -> Real
SOURCEMETA_FORCEINLINE auto to_decimal () const noexcept -> const Decimal &
SOURCEMETA_FORCEINLINE auto to_string () const noexcept -> const String &
auto to_stringstream () const -> std::basic_istringstream< Char, CharTraits, Allocator< Char > >
SOURCEMETA_FORCEINLINE auto as_array () const noexcept -> const Array &
SOURCEMETA_FORCEINLINE auto as_array () noexcept -> Array &
SOURCEMETA_FORCEINLINE auto as_object () noexcept -> Object &
SOURCEMETA_FORCEINLINE auto as_object () const noexcept -> const Object &
SOURCEMETA_FORCEINLINE auto as_real () const -> Real
SOURCEMETA_FORCEINLINE auto as_integer () const -> Integer
SOURCEMETA_FORCEINLINE auto at (const typename Array::size_type index) const -> const JSON &
SOURCEMETA_FORCEINLINE auto at (const typename Array::size_type index) -> JSON &
SOURCEMETA_FORCEINLINE auto at (const String &key) const -> const JSON &
template<typename T>
SOURCEMETA_FORCEINLINE auto at (T key) const -> const JSON &
 This method retrieves an object element by string view key.
SOURCEMETA_FORCEINLINE auto at (const String &key, const typename Object::hash_type hash) const -> const JSON &
template<typename T>
SOURCEMETA_FORCEINLINE auto at (T key, const typename Object::hash_type hash) const -> const JSON &
SOURCEMETA_FORCEINLINE auto at (const String &key) -> JSON &
template<typename T>
SOURCEMETA_FORCEINLINE auto at (T key) -> JSON &
 This method retrieves an object element by string view key.
SOURCEMETA_FORCEINLINE auto at (const String &key, const typename Object::hash_type hash) -> JSON &
template<typename T>
SOURCEMETA_FORCEINLINE auto at (T key, const typename Object::hash_type hash) -> JSON &
auto at_or (const String &key, const JSON &otherwise) const -> const JSON &
auto at_or (const String &key, JSON &&otherwise) const -> const JSON &=delete
auto at_or (const String &key, const typename Object::hash_type hash, const JSON &otherwise) const -> const JSON &
auto at_or (const String &key, const typename Object::hash_type hash, JSON &&otherwise) const -> const JSON &=delete
SOURCEMETA_FORCEINLINE auto front () -> JSON &
SOURCEMETA_FORCEINLINE auto front () const -> const JSON &
SOURCEMETA_FORCEINLINE auto back () -> JSON &
SOURCEMETA_FORCEINLINE auto back () const -> const JSON &
SOURCEMETA_FORCEINLINE auto size () const -> std::size_t
SOURCEMETA_FORCEINLINE auto string_size () const -> std::size_t
SOURCEMETA_FORCEINLINE auto array_size () const -> std::size_t
SOURCEMETA_FORCEINLINE auto object_size () const -> std::size_t
SOURCEMETA_FORCEINLINE auto byte_size () const -> std::size_t
auto estimated_byte_size () const -> std::uint64_t
auto fast_hash () const -> std::uint64_t
auto divisible_by (const JSON &divisor) const -> bool
SOURCEMETA_FORCEINLINE auto empty () const -> bool
SOURCEMETA_FORCEINLINE auto try_at (const String &key) const -> const JSON *
template<typename T>
SOURCEMETA_FORCEINLINE auto try_at (T key) const -> const JSON *
 This method tries to retrieve an object element by string view key.
SOURCEMETA_FORCEINLINE auto try_at (const String &key, const typename Object::hash_type hash) const -> const JSON *
template<typename T>
SOURCEMETA_FORCEINLINE auto try_at (T key, const typename Object::hash_type hash) const -> const JSON *
SOURCEMETA_FORCEINLINE auto try_at (const String &key) -> JSON *
template<typename T>
SOURCEMETA_FORCEINLINE auto try_at (T key) -> JSON *
 This method tries to retrieve a mutable object element by string view key.
SOURCEMETA_FORCEINLINE auto try_at (const String &key, const typename Object::hash_type hash) -> JSON *
template<typename T>
SOURCEMETA_FORCEINLINE auto try_at (T key, const typename Object::hash_type hash) -> JSON *
SOURCEMETA_FORCEINLINE auto try_at (const String &key, const typename Object::hash_type hash, typename Object::size_type &start) const -> const JSON *
template<typename T>
SOURCEMETA_FORCEINLINE auto try_at (T key, const typename Object::hash_type hash, typename Object::size_type &start) const -> const JSON *
SOURCEMETA_FORCEINLINE auto defines (const String &key) const -> bool
template<typename T>
SOURCEMETA_FORCEINLINE auto defines (T key) const -> bool
SOURCEMETA_FORCEINLINE auto defines (const String &key, const typename Object::hash_type hash) const -> bool
template<typename T>
SOURCEMETA_FORCEINLINE auto defines (T key, const typename Object::hash_type hash) const -> bool
SOURCEMETA_FORCEINLINE 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 contains (const StringView element) const -> bool
SOURCEMETA_FORCEINLINE auto array_member_contains (const StringView key, const typename Object::hash_type hash, const StringView value) const -> bool
SOURCEMETA_FORCEINLINE auto array_member_contains (const StringView key, const StringView value) const -> bool
auto is_array_of_strings () const -> bool
auto includes (const String &input) const -> bool
auto includes (const String::value_type input) const -> bool
auto unique () const -> bool
auto unique_keys () 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
template<typename T>
auto assign (T key, const JSON &value) -> void
auto assign (const String &key, JSON &&value) -> void
template<typename T>
auto assign (T key, JSON &&value) -> void
auto try_assign_before (const String &key, const JSON &value, const String &other) -> void
auto assign_if_missing (const String &key, const JSON &value) -> void
template<typename T>
auto assign_if_missing (T key, const JSON &value) -> void
auto assign_if_missing (const String &key, JSON &&value) -> void
template<typename T>
auto assign_if_missing (T key, JSON &&value) -> void
auto assign_assume_new (const String &key, JSON &&value) -> void
auto assign_assume_new (String &&key, JSON &&value) -> void
auto assign_assume_new (String &&key, JSON &&value, Object::hash_type hash) -> JSON &
SOURCEMETA_FORCEINLINE auto assign_if_nonempty (const StringView key, const typename Object::hash_type hash, const StringView value) -> void
SOURCEMETA_FORCEINLINE auto assign_if_nonempty (const StringView key, const StringView value) -> void
auto assign_if_nonempty (const StringView key, const typename Object::hash_type hash, const std::span< const StringView > values) -> void
auto assign_if_nonempty (const StringView key, const std::span< const StringView > values) -> void
auto erase (const String &key) -> typename Object::size_type
template<typename T>
auto erase (T key) -> typename Object::size_type
 This method deletes an object key by string view.
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 erase_if (const std::function< bool(const JSON &)> &predicate) -> void
auto clear () -> void
template<typename Iterator>
auto clear_except (Iterator first, Iterator last) -> void
auto clear_except (std::initializer_list< String > keys) -> void
auto merge (const JSON::Object &other) -> void
auto trim () const -> JSON::String
auto trim () -> const JSON::String &
auto is_trimmed () const noexcept -> bool
auto reorder (const KeyComparison &compare) -> void
auto rename (const JSON::String &key, JSON::String &&to) -> 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_array (std::initializer_list< JSON > values) -> JSON
static auto make_object () -> JSON
static auto size (const String &value) noexcept -> std::size_t

Member Typedef Documentation

◆ KeyComparison

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

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

◆ ParseCallback

Initial value:
std::function<void(
const ParsePhase phase, const Type type, const std::uint64_t line,
const std::uint64_t column, const ParseContext context,
const std::size_t index, const String &property)>
SOURCEMETA_FORCEINLINE auto type() const noexcept -> Type
Definition json_value.h:606
std::basic_string< Char, CharTraits, Allocator< Char > > String
The string type used by the JSON document.
Definition json_value.h:52
Type
The different types of a JSON instance.
Definition json_value.h:69
ParsePhase
The parsing phase of a JSON document.
Definition json_value.h:60
ParseContext
The context type for parse callbacks.
Definition json_value.h:92

An optional callback that can be passed to parsing functions to obtain metadata during the parsing process

Member Enumeration Documentation

◆ ParseContext

enum class sourcemeta::core::JSON::ParseContext : std::uint8_t
strong

The context type for parse callbacks.

Enumerator
Root 

The root value of the document.

Property 

An object property value.

Index 

An array element value.

◆ ParsePhase

enum class sourcemeta::core::JSON::ParsePhase : std::uint8_t
strong

The parsing phase of a JSON document.

Enumerator
Pre 

The phase before a value is parsed.

Post 

The phase after a value is parsed.

◆ Type

enum class sourcemeta::core::JSON::Type : std::uint8_t
strong

The different types of a JSON instance.

Enumerator
Null 

The JSON null type.

Boolean 

The JSON boolean type.

Integer 

The JSON integer type.

Real 

The JSON real number type.

String 

The JSON string type.

Array 

The JSON array type.

Object 

The JSON object type.

Decimal 

The JSON decimal type.

Constructor & Destructor Documentation

◆ JSON() [1/12]

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

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

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

◆ JSON() [2/12]

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

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

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

◆ JSON() [3/12]

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

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

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

◆ JSON() [4/12]

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

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

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

◆ JSON() [5/12]

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

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

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

◆ JSON() [6/12]

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

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

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

◆ JSON() [7/12]

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

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

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

◆ JSON() [8/12]

sourcemeta::core::JSON::JSON ( const String & value)
explicit

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

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

◆ JSON() [9/12]

sourcemeta::core::JSON::JSON ( String && value)
explicit

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

#include <sourcemeta/core/json.h>
const sourcemeta::core::JSON my_string{std::move(value)};

◆ JSON() [10/12]

sourcemeta::core::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/core/json.h>
const sourcemeta::core::JSON my_string{"foo"};

◆ JSON() [11/12]

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

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

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

◆ JSON() [12/12]

sourcemeta::core::JSON::JSON ( std::initializer_list< typename Object::pair_value_type > values)
explicit

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

#include <sourcemeta/core/json.h>
#include <cassert>
const sourcemeta::core::JSON my_object{
{"baz", sourcemeta::core::JSON{1}}};
assert(my_object.is_object());
SOURCEMETA_FORCEINLINE auto is_object() const noexcept -> bool
Definition json_value.h:576

Member Function Documentation

◆ array_member_contains() [1/2]

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::array_member_contains ( const StringView key,
const StringView value ) const -> bool
inlinenodiscard

This method checks whether an array-valued object member contains a string. For example:

#include <sourcemeta/core/json.h>
#include <cassert>
const sourcemeta::core::JSON document =
sourcemeta::core::parse_json(R"JSON({ "tags": [ "a", "b" ] })JSON");
assert(document.array_member_contains("tags", "b"));
SOURCEMETA_FORCEINLINE auto array_member_contains(const StringView key, const typename Object::hash_type hash, const StringView value) const -> bool
Definition json_value.h:1693
SOURCEMETA_CORE_JSON_EXPORT auto parse_json(std::basic_istream< JSON::Char, JSON::CharTraits > &stream) -> JSON

◆ array_member_contains() [2/2]

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::array_member_contains ( const StringView key,
const typename Object::hash_type hash,
const StringView value ) const -> bool
inlinenodiscard

This method checks, given a pre-calculated hash, whether an array-valued object member contains a string, returning false when the member is absent or is not such an array. The instance must be an object. For example:

#include <sourcemeta/core/json.h>
#include <cassert>
const sourcemeta::core::JSON document =
sourcemeta::core::parse_json(R"JSON({ "tags": [ "a", "b" ] })JSON");
assert(document.array_member_contains("tags",
document.as_object().hash("tags"), "b"));
SOURCEMETA_FORCEINLINE auto as_object() noexcept -> Object &
Definition json_value.h:791

◆ array_size()

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::array_size ( ) const -> std::size_t
inlinenodiscard

If the input JSON instance is an array, return its number of elements.

For example:

#include <sourcemeta/core/json.h>
#include <cassert>
const sourcemeta::core::JSON my_array =
assert(my_array.array_size() == 2);
SOURCEMETA_FORCEINLINE auto array_size() const -> std::size_t
Definition json_value.h:1246

◆ as_array() [1/2]

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::as_array ( ) const -> const Array &
inlinenodiscardnoexcept

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

#include <sourcemeta/core/json.h>
#include <algorithm>
#include <iostream>
const sourcemeta::core::JSON document =
std::for_each(document.as_array().cbegin(),
document.as_array().cend(),
[](const auto &element) {
std::cout << "Element: "
<< element.to_integer()
<< "\n";
});
SOURCEMETA_FORCEINLINE auto as_array() const noexcept -> const Array &
Definition json_value.h:745

◆ as_array() [2/2]

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::as_array ( ) -> Array &
inlinenodiscardnoexcept

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

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

◆ as_integer()

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::as_integer ( ) const -> Integer
inlinenodiscard

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. A value whose magnitude does not fit in a 64-bit integer throws std::out_of_range. For example:

#include <sourcemeta/core/json.h>
#include <cassert>
const sourcemeta::core::JSON document{5.3};
assert(document.as_integer() == 5);
SOURCEMETA_FORCEINLINE auto as_integer() const -> Integer
Definition json_value.h:856

◆ as_object() [1/2]

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::as_object ( ) const -> const Object &
inlinenodiscardnoexcept

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

#include <sourcemeta/core/json.h>
#include <algorithm>
#include <iostream>
document.assign("foo", sourcemeta::core::JSON{1});
document.assign("bar", sourcemeta::core::JSON{2});
document.assign("baz", sourcemeta::core::JSON{3});
for (auto &[key, value] : document.as_object()) {
}
static auto make_object() -> JSON
auto assign(const String &key, const JSON &value) -> void

◆ as_object() [2/2]

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::as_object ( ) -> Object &
inlinenodiscardnoexcept

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

#include <sourcemeta/core/json.h>
#include <algorithm>
#include <iostream>
document.assign("foo", sourcemeta::core::JSON{1});
document.assign("bar", sourcemeta::core::JSON{2});
document.assign("baz", sourcemeta::core::JSON{3});
std::for_each(document.as_object().cbegin(),
document.as_object().cend(),
[](const auto &pair) {
std::cout << "Value: "
<< pair.second.to_integer()
<< "\n";
});

◆ as_real()

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::as_real ( ) const -> Real
inlinenodiscard

Get the JSON numeric document as a real number if it is not one already. A decimal whose magnitude does not fit in a double throws std::out_of_range, matching the behaviour of converting that decimal directly. For example:

#include <sourcemeta/core/json.h>
#include <cassert>
const sourcemeta::core::JSON document{5};
assert(document.as_real() == 5.0);
SOURCEMETA_FORCEINLINE auto as_real() const -> Real
Definition json_value.h:833

◆ assign() [1/4]

auto sourcemeta::core::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/core/json.h>
#include <cassert>
sourcemeta::core::parse_json("{ \"foo\": true }");
const sourcemeta::core::JSON value{false};
document.assign("bar", value);
assert(document.defines("foo"));
assert(document.defines("bar"));
SOURCEMETA_FORCEINLINE auto defines(const String &key) const -> bool
Definition json_value.h:1548

◆ assign() [2/4]

auto sourcemeta::core::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/core/json.h>
#include <cassert>
sourcemeta::core::parse_json("{ \"foo\": true }");
document.assign("bar", sourcemeta::core::JSON{false});
assert(document.defines("foo"));
assert(document.defines("bar"));

◆ assign() [3/4]

template<typename T>
auto sourcemeta::core::JSON::assign ( T key,
const JSON & value ) -> void
inline

This method sets or updates an object key by string view. The key is copied into a stored string exactly once.

◆ assign() [4/4]

template<typename T>
auto sourcemeta::core::JSON::assign ( T key,
JSON && value ) -> void
inline

This method sets or updates an object key by string view. The key is copied into a stored string exactly once.

◆ assign_assume_new() [1/3]

auto sourcemeta::core::JSON::assign_assume_new ( const String & key,
JSON && value ) -> void

This method sets an object key, assuming the key does not already exist. If the key already exists, behavior is undefined. This variant is faster than assign when building objects with keys known to be unique. For example:

#include <sourcemeta/core/json.h>
#include <cassert>
assert(document.defines("foo"));
assert(document.at("foo").to_integer() == 1);
auto assign_assume_new(const String &key, JSON &&value) -> void
SOURCEMETA_FORCEINLINE auto at(const typename Array::size_type index) const -> const JSON &
Definition json_value.h:904

◆ assign_assume_new() [2/3]

auto sourcemeta::core::JSON::assign_assume_new ( String && key,
JSON && value ) -> void

This method sets an object key, assuming the key does not already exist. If the key already exists, behavior is undefined. This variant is faster than assign when building objects with keys known to be unique, and allows moving the key. For example:

#include <sourcemeta/core/json.h>
#include <cassert>
std::string key{"foo"};
document.assign_assume_new(std::move(key), sourcemeta::core::JSON{1});
assert(document.defines("foo"));
assert(document.at("foo").to_integer() == 1);

◆ assign_assume_new() [3/3]

auto sourcemeta::core::JSON::assign_assume_new ( String && key,
JSON && value,
Object::hash_type hash ) -> JSON &

This method sets an object key with a pre-computed hash, returning the inserted value

◆ assign_if_missing() [1/4]

auto sourcemeta::core::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/core/json.h>
#include <cassert>
sourcemeta::core::parse_json("{ \"foo\": true }");
const sourcemeta::core::JSON value_1{1};
const sourcemeta::core::JSON value_2{2};
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 assign_if_missing(const String &key, const JSON &value) -> void

◆ assign_if_missing() [2/4]

auto sourcemeta::core::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/core/json.h>
#include <cassert>
sourcemeta::core::parse_json("{ \"foo\": true }");
assert(document.defines("foo"));
assert(document.at("foo").is_boolean());
assert(document.defines("bar"));
assert(document.at("bar").is_integer());

◆ assign_if_missing() [3/4]

template<typename T>
auto sourcemeta::core::JSON::assign_if_missing ( T key,
const JSON & value ) -> void
inline

This method sets an object key by string view if it is not already defined.

◆ assign_if_missing() [4/4]

template<typename T>
auto sourcemeta::core::JSON::assign_if_missing ( T key,
JSON && value ) -> void
inline

This method sets an object key by string view if it is not already defined.

◆ assign_if_nonempty() [1/4]

auto sourcemeta::core::JSON::assign_if_nonempty ( const StringView key,
const std::span< const StringView > values ) -> void
inline

This method assigns an array-of-strings object member unless the array is empty, in which case the member is left absent, assuming the key is new. For example:

#include <sourcemeta/core/json.h>
#include <array>
#include <cassert>
const std::array<std::string_view, 2> values{{"a", "b"}};
document.assign_if_nonempty("foo", values);
assert(document.at("foo").size() == 2);
SOURCEMETA_FORCEINLINE auto assign_if_nonempty(const StringView key, const typename Object::hash_type hash, const StringView value) -> void
Definition json_value.h:2049

◆ assign_if_nonempty() [2/4]

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::assign_if_nonempty ( const StringView key,
const StringView value ) -> void
inline

This method assigns a string object member unless the value is empty, in which case the member is left absent, assuming the key is new. For example:

#include <sourcemeta/core/json.h>
#include <cassert>
document.assign_if_nonempty("foo", "bar");
assert(document.at("foo").to_string() == "bar");

◆ assign_if_nonempty() [3/4]

auto sourcemeta::core::JSON::assign_if_nonempty ( const StringView key,
const typename Object::hash_type hash,
const std::span< const StringView > values ) -> void
inline

This method assigns an array-of-strings object member with a pre-computed hash unless the array is empty, in which case the member is left absent, assuming the key is new. For example:

#include <sourcemeta/core/json.h>
#include <array>
#include <cassert>
const std::array<std::string_view, 2> values{{"a", "b"}};
document.assign_if_nonempty("foo", document.as_object().hash("foo"),
values);
assert(document.at("foo").size() == 2);

◆ assign_if_nonempty() [4/4]

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::assign_if_nonempty ( const StringView key,
const typename Object::hash_type hash,
const StringView value ) -> void
inline

This method assigns a string object member with a pre-computed hash unless the value is empty, in which case the member is left absent, assuming the key is new. For example:

#include <sourcemeta/core/json.h>
#include <cassert>
document.assign_if_nonempty("foo", document.as_object().hash("foo"),
"bar");
assert(document.at("foo").to_string() == "bar");

◆ at() [1/8]

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::at ( const String & key) -> JSON &
inlinenodiscard

This method retrieves an object element.

For example:

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

◆ at() [2/8]

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::at ( const String & key) const -> const JSON &
inlinenodiscard

This method retrieves an object element.

For example:

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

◆ at() [3/8]

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::at ( const String & key,
const typename Object::hash_type hash ) -> JSON &
inlinenodiscard

This method retrieves an object element given a pre-calculated property hash.

For example:

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

◆ at() [4/8]

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::at ( const String & key,
const typename Object::hash_type hash ) const -> const JSON &
inlinenodiscard

This method retrieves an object element given a pre-calculated property hash.

For example:

#include <sourcemeta/core/json.h>
#include <cassert>
const sourcemeta::core::JSON my_object =
sourcemeta::core::parse_json("{ \"foo\": 1, \"bar\": 2 }");
assert(my_object.at("bar",
my_object.as_object().hash("bar")).to_integer() == 2);

◆ at() [5/8]

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::at ( const typename Array::size_type index) -> JSON &
inlinenodiscard

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/core/json.h>
#include <cassert>
assert(my_array.at(1).to_integer() == 2);
sourcemeta::core::parse_json("{ \"1\": "foo" }");
assert(my_array.at(1).to_string() == "foo");

◆ at() [6/8]

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::at ( const typename Array::size_type index) const -> const JSON &
inlinenodiscard

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/core/json.h>
#include <cassert>
const sourcemeta::core::JSON my_array =
assert(my_array.at(1).to_integer() == 2);
const sourcemeta::core::JSON my_object =
sourcemeta::core::parse_json("{ \"1\": "foo" }");
assert(my_array.at(1).to_string() == "foo");

◆ at() [7/8]

template<typename T>
SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::at ( T key,
const typename Object::hash_type hash ) -> JSON &
inlinenodiscard

This method retrieves an object element by string view key given a pre-calculated property hash

◆ at() [8/8]

template<typename T>
SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::at ( T key,
const typename Object::hash_type hash ) const -> const JSON &
inlinenodiscard

This method retrieves an object element by string view key given a pre-calculated property hash

◆ at_or() [1/4]

auto sourcemeta::core::JSON::at_or ( const String & key,
const JSON & otherwise ) const -> const JSON &
nodiscard

This method retrieves an object property or a user provided value if such property is not defined.

For example:

#include <sourcemeta/core/json.h>
#include <cassert>
sourcemeta::core::parse_json("{ \"foo\": 1, \"bar\": 2 }");
const sourcemeta::core::JSON default_value{3};
assert(my_object.at_or("baz", default_value).to_integer() == 3);
auto at_or(const String &key, const JSON &otherwise) const -> const JSON &

◆ at_or() [2/4]

auto sourcemeta::core::JSON::at_or ( const String & key,
const typename Object::hash_type hash,
const JSON & otherwise ) const -> const JSON &
nodiscard

This method retrieves an object property given a pre-calculated property hash, or a user provided value if such property is not defined.

For example:

#include <sourcemeta/core/json.h>
#include <cassert>
sourcemeta::core::parse_json("{ \"foo\": 1, \"bar\": 2 }");
const sourcemeta::core::JSON default_value{3};
assert(my_object.at_or("foo",
my_object.as_object().hash("foo"),
default_value).to_integer() == 1);

◆ at_or() [3/4]

auto sourcemeta::core::JSON::at_or ( const String & key,
const typename Object::hash_type hash,
JSON && otherwise ) const -> const JSON &=delete
nodiscarddelete

This overload avoids misuses of returning a const reference parameter as a constant reference.

◆ at_or() [4/4]

auto sourcemeta::core::JSON::at_or ( const String & key,
JSON && otherwise ) const -> const JSON &=delete
nodiscarddelete

This overload avoids misuses of returning a const reference parameter as a constant reference.

◆ back() [1/2]

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::back ( ) -> JSON &
inlinenodiscard

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/core/json.h>
#include <cassert>
assert(document.back().to_integer() == 3);
SOURCEMETA_FORCEINLINE auto back() -> JSON &
Definition json_value.h:1158

◆ back() [2/2]

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::back ( ) const -> const JSON &
inlinenodiscard

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/core/json.h>
#include <cassert>
const sourcemeta::core::JSON document =
assert(document.back().to_integer() == 3);

◆ byte_size()

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::byte_size ( ) const -> std::size_t
inlinenodiscard

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

#include <sourcemeta/core/json.h>
#include <cassert>
const sourcemeta::core::JSON my_string{
sourcemeta::core::parse_json("\"\\uD83D\\uDCA9\"")};
assert(my_string.size() == 1);
assert(my_string.byte_size() == 4);
SOURCEMETA_FORCEINLINE auto byte_size() const -> std::size_t
Definition json_value.h:1282
static auto size(const String &value) noexcept -> std::size_t

◆ clear()

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

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

#include <sourcemeta/core/json.h>
#include <cassert>
sourcemeta::core::parse_json("{ \"foo\": true }");
my_object.clear();
my_array.clear();
assert(my_object.empty());
assert(my_array.empty());
auto clear() -> void
SOURCEMETA_FORCEINLINE auto empty() const -> bool
Definition json_value.h:1354

◆ clear_except() [1/2]

template<typename Iterator>
auto sourcemeta::core::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/core/json.h>
#include <cassert>
#include <string>
#include <vector>
document.assign("foo", sourcemeta::core::JSON{true});
document.assign("bar", sourcemeta::core::JSON{false});
document.assign("baz", sourcemeta::core::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:2285

◆ clear_except() [2/2]

auto sourcemeta::core::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/core/json.h>
#include <cassert>
document.assign("foo", sourcemeta::core::JSON{true});
document.assign("bar", sourcemeta::core::JSON{false});
document.assign("baz", sourcemeta::core::JSON{true});
document.clear_except({ "foo" });
assert(document.defines("foo"));
assert(!document.defines("bar"));
assert(!document.defines("baz"));

◆ contains() [1/2]

auto sourcemeta::core::JSON::contains ( const JSON & element) const -> bool
nodiscard

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

#include <sourcemeta/core/json.h>
#include <cassert>
const sourcemeta::core::JSON document =
assert(document.contains(sourcemeta::core::JSON{2}));
assert(!document.contains(sourcemeta::core::JSON{4}));
auto contains(const JSON &element) const -> bool

◆ contains() [2/2]

auto sourcemeta::core::JSON::contains ( const StringView element) const -> bool
nodiscard

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

#include <sourcemeta/core/json.h>
#include <cassert>
const sourcemeta::core::JSON document =
sourcemeta::core::parse_json(R"JSON([ "foo", "bar", "baz" ])JSON");
assert(document.contains("bar"));
assert(!document.contains("qux"));

◆ defines() [1/5]

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::defines ( const String & key) const -> bool
inlinenodiscard

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

#include <sourcemeta/core/json.h>
#include <cassert>
const sourcemeta::core::JSON document =
sourcemeta::core::parse_json("{ \"foo\": 1 }");
assert(document.defines("foo"));
assert(!document.defines("bar"));

◆ defines() [2/5]

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::defines ( const String & key,
const typename Object::hash_type hash ) const -> bool
inlinenodiscard

This method checks whether an input JSON object defines a specific key given a pre-calculated property hash. For example:

#include <sourcemeta/core/json.h>
#include <cassert>
const sourcemeta::core::JSON document =
sourcemeta::core::parse_json("{ \"foo\": 1 }");
assert(document.defines("foo",
document.as_object().hash("foo")));
assert(document.defines("bar",
document.as_object().hash("bar")));

◆ defines() [3/5]

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::defines ( const typename Array::size_type index) const -> bool
inlinenodiscard

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

#include <sourcemeta/core/json.h>
#include <cassert>
const sourcemeta::core::JSON document =
assert(document.defines(0));
assert(!document.defines(1));

◆ defines() [4/5]

template<typename T>
SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::defines ( T key) const -> bool
inlinenodiscard

This method checks whether an input JSON object defines a specific string view key

◆ defines() [5/5]

template<typename T>
SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::defines ( T key,
const typename Object::hash_type hash ) const -> bool
inlinenodiscard

This method checks whether an input JSON object defines a specific string view key given a pre-calculated property hash

◆ defines_any() [1/2]

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

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

#include <sourcemeta/core/json.h>
#include <cassert>
#include <string>
#include <vector>
const sourcemeta::core::JSON document =
sourcemeta::core::parse_json("{ \"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:1629

◆ defines_any() [2/2]

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

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

#include <sourcemeta/core/json.h>
#include <cassert>
const sourcemeta::core::JSON document =
sourcemeta::core::parse_json("{ \"foo\": true, \"bar\": false }");
assert(document.defines_any({ "foo", "qux" }));

◆ divisible_by()

auto sourcemeta::core::JSON::divisible_by ( const JSON & divisor) const -> bool
nodiscard

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

#include <sourcemeta/core/json.h>
#include <cassert>
const sourcemeta::core::JSON dividend{6};
const sourcemeta::core::JSON divisor{1.5};
assert(dividend.divisible_by(divisor));
auto divisible_by(const JSON &divisor) const -> bool

◆ empty()

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::empty ( ) const -> bool
inlinenodiscard

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

For example:

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

◆ erase() [1/3]

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

This method deletes an object key. For example:

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

◆ erase() [2/3]

auto sourcemeta::core::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/core/json.h>
#include <cassert>
#include <iterator>
array.erase(std::next(array.begin()), array.end());
assert(array.size(), 1);
assert(array.at(0), 1);

◆ erase() [3/3]

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

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

#include <sourcemeta/core/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_if()

auto sourcemeta::core::JSON::erase_if ( const std::function< bool(const JSON &)> & predicate) -> void

This method deletes a set of array elements given a predicate. For example:

#include <sourcemeta/core/json.h>
#include <cassert>
array.erase_if(array,
[](const auto &item) { return item.to_integer() % 2 == 0; });
assert(array.size(), 2);
assert(array.at(0), 1);
assert(array.at(1), 3);
auto erase_if(const std::function< bool(const JSON &)> &predicate) -> void

◆ erase_keys() [1/2]

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

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

#include <sourcemeta/core/json.h>
#include <cassert>
#include <string>
#include <vector>
document.assign("foo", sourcemeta::core::JSON{true});
document.assign("bar", sourcemeta::core::JSON{false});
document.assign("baz", sourcemeta::core::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:2167

◆ erase_keys() [2/2]

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

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

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

◆ estimated_byte_size()

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

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/core/json.h>
#include <cassert>
sourcemeta::core::parse_json("{ \"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

◆ fast_hash()

auto sourcemeta::core::JSON::fast_hash ( ) const -> std::uint64_t
nodiscard

Produce a simple hash for the JSON value. Note the hash is fast to produce but might have a higher chance of collisions. For example:

#include <sourcemeta/core/json.h>
#include <cassert>
const sourcemeta::core::JSON value_1 =
sourcemeta::core::parse_json("{ \"foo\": 1 }");
const sourcemeta::core::JSON value_2 =
sourcemeta::core::parse_json("{ \"foo\": 1 }");
assert(value_1.fast_hash() == value_2.fast_hash());
auto fast_hash() const -> std::uint64_t

◆ front() [1/2]

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::front ( ) -> JSON &
inlinenodiscard

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/core/json.h>
#include <cassert>
assert(document.front().to_integer() == 1);
SOURCEMETA_FORCEINLINE auto front() -> JSON &
Definition json_value.h:1121

◆ front() [2/2]

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::front ( ) const -> const JSON &
inlinenodiscard

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/core/json.h>
#include <cassert>
const sourcemeta::core::JSON document =
assert(document.front().to_integer() == 1);

◆ includes() [1/2]

auto sourcemeta::core::JSON::includes ( const String & input) const -> bool
nodiscard

This method checks if a JSON string includes a given substring. For example:

#include <sourcemeta/core/json.h>
#include <cassert>
const sourcemeta::core::JSON document{"foo bar baz"};
assert(document.includes("bar"));
assert(!document.includes("qux"));
auto includes(const String &input) const -> bool

◆ includes() [2/2]

auto sourcemeta::core::JSON::includes ( const String::value_type input) const -> bool
nodiscard

This method checks if a JSON string includes a given character. For example:

#include <sourcemeta/core/json.h>
#include <cassert>
const sourcemeta::core::JSON document{"foo"};
assert(document.includes('f'));
assert(!document.includes('b'));

◆ into() [1/2]

auto sourcemeta::core::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/core/json.h>
#include <cassert>
sourcemeta::core::parse_json("{ \"foo\": true }");
const sourcemeta::core::JSON value{2};
document.at("foo").into(value);
assert(document.at("foo").is_integer());

◆ into() [2/2]

auto sourcemeta::core::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/core/json.h>
#include <cassert>
sourcemeta::core::parse_json("{ \"foo\": true }");
document.at("foo").into(sourcemeta::core::JSON{2});
assert(document.at("foo").is_integer());

◆ into_array()

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

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

#include <sourcemeta/core/json.h>
#include <cassert>
sourcemeta::core::JSON document{true};
assert(document.is_boolean());
document.into_array();
assert(document.is_array());
assert(document.empty());
auto into_array() -> void
SOURCEMETA_FORCEINLINE auto is_boolean() const noexcept -> bool
Definition json_value.h:433
SOURCEMETA_FORCEINLINE auto is_array() const noexcept -> bool
Definition json_value.h:561

◆ into_object()

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

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

#include <sourcemeta/core/json.h>
#include <cassert>
sourcemeta::core::JSON document{true};
assert(document.is_boolean());
document.into_object();
assert(document.is_object());
assert(document.empty());
auto into_object() -> void

◆ is_array()

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::is_array ( ) const -> bool
inlinenodiscardnoexcept

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

#include <sourcemeta/core/json.h>
#include <cassert>
document=sourcemeta::core::parse_json("[ 1, 2, 3 ]");
assert(document.is_array());

◆ is_array_of_strings()

auto sourcemeta::core::JSON::is_array_of_strings ( ) const -> bool
inlinenodiscard

This method checks whether this value is an array whose every element is a string. For example:

#include <sourcemeta/core/json.h>
#include <cassert>
const sourcemeta::core::JSON document =
sourcemeta::core::parse_json(R"JSON([ "a", "b" ])JSON");
assert(document.is_array_of_strings());
auto is_array_of_strings() const -> bool
Definition json_value.h:1729

◆ is_boolean()

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::is_boolean ( ) const -> bool
inlinenodiscardnoexcept

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

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

◆ is_decimal()

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::is_decimal ( ) const -> bool
inlinenodiscardnoexcept

Check if the input JSON document is an arbitrary precision decimal value. For example:

#include <sourcemeta/core/json.h>
#include <cassert>
const sourcemeta::core::Decimal value{1234567890};
const sourcemeta::core::JSON document{value};
assert(document.is_decimal());
SOURCEMETA_FORCEINLINE auto is_decimal() const noexcept -> bool
Definition json_value.h:592
Definition numeric_decimal.h:21

◆ is_integer()

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::is_integer ( ) const -> bool
inlinenodiscardnoexcept

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

#include <sourcemeta/core/json.h>
#include <cassert>
const sourcemeta::core::JSON document{5};
assert(document.is_integer());
SOURCEMETA_FORCEINLINE auto is_integer() const noexcept -> bool
Definition json_value.h:461

◆ is_integral()

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::is_integral ( ) const -> bool
inlinenodiscardnoexcept

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

#include <sourcemeta/core/json.h>
#include <cassert>
const sourcemeta::core::JSON document{5.0};
assert(document.is_integral());
SOURCEMETA_FORCEINLINE auto is_integral() const noexcept -> bool
Definition json_value.h:490

◆ is_null()

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::is_null ( ) const -> bool
inlinenodiscardnoexcept

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

#include <sourcemeta/core/json.h>
#include <cassert>
const sourcemeta::core::JSON document{nullptr};
assert(document.is_null());
SOURCEMETA_FORCEINLINE auto is_null() const noexcept -> bool
Definition json_value.h:447

◆ is_number()

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::is_number ( ) const -> bool
inlinenodiscardnoexcept

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

#include <sourcemeta/core/json.h>
#include <cassert>
const sourcemeta::core::JSON real{3.14};
const sourcemeta::core::JSON integer{5};
assert(real.is_number());
assert(integer.is_number());
SOURCEMETA_FORCEINLINE auto is_number() const noexcept -> bool
Definition json_value.h:518

◆ is_object()

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::is_object ( ) const -> bool
inlinenodiscardnoexcept

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

#include <sourcemeta/core/json.h>
#include <cassert>
document=sourcemeta::core::parse_json("{ \"foo\": 1 }");
assert(document.is_object());

◆ is_positive()

auto sourcemeta::core::JSON::is_positive ( ) const -> bool
nodiscardnoexcept

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/core/json.h>
#include <cassert>
const sourcemeta::core::JSON real{3.14};
const sourcemeta::core::JSON integer{-5};
assert(real.is_positive());
assert(!integer.is_positive());
auto is_positive() const noexcept -> bool

◆ is_real()

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::is_real ( ) const -> bool
inlinenodiscardnoexcept

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

#include <sourcemeta/core/json.h>
#include <cassert>
const sourcemeta::core::JSON document{3.14};
assert(document.is_real());
SOURCEMETA_FORCEINLINE auto is_real() const noexcept -> bool
Definition json_value.h:475

◆ is_string()

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::is_string ( ) const -> bool
inlinenodiscardnoexcept

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

#include <sourcemeta/core/json.h>
#include <cassert>
const sourcemeta::core::JSON document{"foo"};
assert(document.is_string());
SOURCEMETA_FORCEINLINE auto is_string() const noexcept -> bool
Definition json_value.h:546

◆ is_trimmed()

auto sourcemeta::core::JSON::is_trimmed ( ) const -> bool
nodiscardnoexcept

Check if the string has no leading or trailing whitespace. For example:

#include <sourcemeta/core/json.h>
#include <cassert>
const sourcemeta::core::JSON trimmed{"Hello World"};
assert(trimmed.is_trimmed());
const sourcemeta::core::JSON untrimmed{" Hello World "};
assert(!untrimmed.is_trimmed());
auto is_trimmed() const noexcept -> bool

◆ make_array() [1/2]

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

This function creates an empty JSON array. For example:

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

This function is particularly handy for programatically constructing arrays.

◆ make_array() [2/2]

auto sourcemeta::core::JSON::make_array ( std::initializer_list< JSON > values) -> JSON
static

This function creates a JSON array out of a list of other JSON documents. For example:

#include <sourcemeta/core/json.h>
#include <cassert>
const auto document = sourcemeta::core::JSON::make_array({
assert(document.is_array());
assert(document.size() == 3);

Note that a list of this kind always copies each of its documents, as the language offers no way of moving out of one. Prefer starting from an empty array and moving each document into it when the cost of those copies matters.

◆ make_object()

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

This function creates an empty JSON object. For example:

#include <sourcemeta/core/json.h>
#include <cassert>
const sourcemeta::core::JSON document =
assert(document.is_object());
assert(document.empty());

This function is particularly handy for programatically constructing objects.

◆ merge()

auto sourcemeta::core::JSON::merge ( const JSON::Object & other) -> void

This method assigns every property of another object into the current object. Overriding existing properties if they are already defined. For example:

#include <sourcemeta/core/json.h>
#include <cassert>
document.assign("foo", sourcemeta::core::JSON{1});
document.assign("bar", sourcemeta::core::JSON{2});
other.assign("bar", sourcemeta::core::JSON{1});
other.assign("baz", sourcemeta::core::JSON{2});
document.merge(other.as_object());
assert(document.size() == 3);
assert(document.at("foo").to_integer() == 1);
assert(document.at("bar").to_integer() == 1);
assert(document.at("baz").to_integer() == 2);
auto merge(const JSON::Object &other) -> void

◆ object_size()

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::object_size ( ) const -> std::size_t
inlinenodiscard

If the input JSON instance is an object, return its number of pairs.

For example:

#include <sourcemeta/core/json.h>
#include <cassert>
const sourcemeta::core::JSON my_object =
sourcemeta::core::parse_json("{ \"foo\": 1 }");
assert(my_object.object_size() == 1);
SOURCEMETA_FORCEINLINE auto object_size() const -> std::size_t
Definition json_value.h:1264

◆ push_back() [1/2]

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

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

#include <sourcemeta/core/json.h>
#include <cassert>
const sourcemeta::core::JSON value{4};
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::core::JSON::push_back ( JSON && value) -> void

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

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

◆ push_back_if_unique() [1/2]

auto sourcemeta::core::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/core/json.h>
#include <cassert>
const sourcemeta::core::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::core::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/core/json.h>
#include <cassert>
#include <utility>
sourcemeta::core::JSON new_element{3};
const auto result{document.push_back_if_unique(std::move(new_element)};
assert(result.first.get().to_integer() == 3);
assert(!result.second);

◆ rename()

auto sourcemeta::core::JSON::rename ( const JSON::String & key,
JSON::String && to ) -> void

This method moves a JSON property value from one property name to another, potentially deleting the destination property name if it already exists. For example:

#include <sourcemeta/core/json.h>
#include <cassert>
sourcemeta::core::parse_json("{ \"foo\": true }");
document.rename("foo", "bar");
assert(!document.defines("foo"));
assert(document.defines("bar"));
assert(document.at("bar").is_boolean());
assert(document.at("bar").to_boolean());
auto rename(const JSON::String &key, JSON::String &&to) -> void

◆ reorder()

auto sourcemeta::core::JSON::reorder ( const KeyComparison & compare) -> void

Reorder the properties of an object by sorting keys according to a comparator function. The object is modified in-place. For example:

#include <sourcemeta/core/json.h>
#include <cassert>
document.assign("zebra", sourcemeta::core::JSON{1});
document.assign("apple", sourcemeta::core::JSON{2});
document.assign("banana", sourcemeta::core::JSON{3});
document.reorder([](const auto &left, const auto &right) {
return left < right;
});
auto iterator = document.as_object().cbegin();
assert(iterator->first == "apple");
++iterator;
assert(iterator->first == "banana");
++iterator;
assert(iterator->first == "zebra");
auto reorder(const KeyComparison &compare) -> void

◆ size() [1/2]

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::size ( ) const -> std::size_t
inlinenodiscard

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/core/json.h>
#include <cassert>
const sourcemeta::core::JSON my_object =
sourcemeta::core::parse_json("{ \"foo\": 1 }");
const sourcemeta::core::JSON my_array =
const sourcemeta::core::JSON my_string{"foo"};
assert(my_object.size() == 1);
assert(my_array.size() == 2);
assert(my_string.size() == 3);

◆ size() [2/2]

auto sourcemeta::core::JSON::size ( const String & value) -> std::size_t
staticnoexcept

This function calculates the logical size of a string according to the JSON specification. For example:

#include <sourcemeta/core/json.h>
#include <cassert>
const sourcemeta::core::JSON::String value{"foo"};
assert(sourcemeta::core::JSON::size(value) == 3);

◆ string_size()

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::string_size ( ) const -> std::size_t
inlinenodiscard

If the input JSON instance is a string, return its logical length.

For example:

#include <sourcemeta/core/json.h>
#include <cassert>
const sourcemeta::core::JSON my_string{"foo"};
assert(my_string.string_size() == 3);
SOURCEMETA_FORCEINLINE auto string_size() const -> std::size_t
Definition json_value.h:1228

◆ to_boolean()

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::to_boolean ( ) const -> bool
inlinenodiscardnoexcept

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/core/json.h>
#include <cassert>
const sourcemeta::core::JSON document{true};
assert(document.is_boolean());
assert(document.to_boolean());
SOURCEMETA_FORCEINLINE auto to_boolean() const noexcept -> bool
Definition json_value.h:626

◆ to_decimal()

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::to_decimal ( ) const -> const Decimal &
inlinenodiscardnoexcept

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

#include <sourcemeta/core/json.h>
#include <cassert>
const sourcemeta::core::Decimal value{1234567890};
const sourcemeta::core::JSON document{value};
assert(document.is_decimal());
assert(document.to_decimal().to_int64() == 1234567890);
SOURCEMETA_FORCEINLINE auto to_decimal() const noexcept -> const Decimal &
Definition json_value.h:682

◆ to_integer()

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::to_integer ( ) const -> Integer
inlinenodiscardnoexcept

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/core/json.h>
#include <cassert>
const sourcemeta::core::JSON document{5};
assert(document.is_integer());
assert(document.to_integer() == 5);
SOURCEMETA_FORCEINLINE auto to_integer() const noexcept -> Integer
Definition json_value.h:644

◆ to_real()

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::to_real ( ) const -> Real
inlinenodiscardnoexcept

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/core/json.h>
#include <cassert>
const sourcemeta::core::JSON document{3.14};
assert(document.is_real());
assert(document.to_real() == 3.14);
SOURCEMETA_FORCEINLINE auto to_real() const noexcept -> Real
Definition json_value.h:662

◆ to_string()

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::to_string ( ) const -> const String &
inlinenodiscardnoexcept

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/core/json.h>
#include <cassert>
const sourcemeta::core::JSON document{"foo"};
assert(document.is_string());
assert(document.to_string() == "foo");
SOURCEMETA_FORCEINLINE auto to_string() const noexcept -> const String &
Definition json_value.h:702

◆ to_stringstream()

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

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/core/json.h>
#include <cassert>
const sourcemeta::core::JSON document{"foo"};
assert(document.is_string());
auto stream{document.to_stringstream()};
assert(stream.get() == 'f');
auto to_stringstream() const -> std::basic_istringstream< Char, CharTraits, Allocator< Char > >

◆ trim() [1/2]

auto sourcemeta::core::JSON::trim ( ) -> const JSON::String &

Trim the string in-place. For example:

#include <sourcemeta/core/json.h>
#include <cassert>
sourcemeta::core::JSON document{" \r\t Hello World\n\v \f"};
document.trim();
assert(document.to_string() == "Hello World");
auto trim() const -> JSON::String

◆ trim() [2/2]

auto sourcemeta::core::JSON::trim ( ) const -> JSON::String
nodiscard

Return a trimmed version of the string. For example:

#include <sourcemeta/core/json.h>
#include <cassert>
const sourcemeta::core::JSON document{" \r\t Hello World\n\v \f"};
assert(document.trim() == "Hello World");

◆ try_assign_before()

auto sourcemeta::core::JSON::try_assign_before ( const String & key,
const JSON & value,
const String & other ) -> void

This method sets or updates an object key. However, it will try to insert the key before the given one if possible.

#include <sourcemeta/core/json.h>
#include <cassert>
sourcemeta::core::parse_json("{ \"foo\": true }");
const sourcemeta::core::JSON value{false};
document.try_assign_before("bar", value, "foo");
assert(document.as_object().cbegin()->first == "bar");
auto try_assign_before(const String &key, const JSON &value, const String &other) -> void

◆ try_at() [1/8]

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::try_at ( const String & key) -> JSON *
inlinenodiscard

This method tries to retrieve a mutable object element by key. For example:

#include <sourcemeta/core/json.h>
#include <cassert>
sourcemeta::core::parse_json("{ \"foo\": 1 }");
auto result{document.try_at("foo")};
assert(result);
result->into(sourcemeta::core::JSON{2});
assert(document.at("foo").to_integer() == 2);
SOURCEMETA_FORCEINLINE auto try_at(const String &key) const -> const JSON *
Definition json_value.h:1379

◆ try_at() [2/8]

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::try_at ( const String & key) const -> const JSON *
inlinenodiscard

This method checks whether an input JSON object defines a specific key and returns the value if it does. For example:

#include <sourcemeta/core/json.h>
#include <cassert>
const sourcemeta::core::JSON document =
sourcemeta::core::parse_json("{ \"foo\": 1 }");
EXPECT_TRUE(document.is_object());
const auto result = document.try_at("foo");
EXPECT_TRUE(result);
EXPECT_EQ(result->to_integer(), 1);

◆ try_at() [3/8]

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::try_at ( const String & key,
const typename Object::hash_type hash ) -> JSON *
inlinenodiscard

This method tries to retrieve a mutable object element given a pre-calculated property hash. For example:

#include <sourcemeta/core/json.h>
#include <cassert>
sourcemeta::core::parse_json("{ \"foo\": 1 }");
auto result{document.try_at("foo",
document.as_object().hash("foo"))};
assert(result);
result->into(sourcemeta::core::JSON{2});
assert(document.at("foo").to_integer() == 2);

◆ try_at() [4/8]

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::try_at ( const String & key,
const typename Object::hash_type hash ) const -> const JSON *
inlinenodiscard

This method checks, given a pre-calculated hash, whether an input JSON object defines a specific key and returns the value if it does. For example:

#include <sourcemeta/core/json.h>
#include <cassert>
const sourcemeta::core::JSON document =
sourcemeta::core::parse_json("{ \"foo\": 1 }");
EXPECT_TRUE(document.is_object());
const auto result = document.try_at("foo",
document.as_object().hash("foo"));
EXPECT_TRUE(result);
EXPECT_EQ(result->to_integer(), 1);

◆ try_at() [5/8]

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::try_at ( const String & key,
const typename Object::hash_type hash,
typename Object::size_type & start ) const -> const JSON *
inlinenodiscard

Try to get a property, scanning from a caller-provided start offset. On hit, advances start past the found index. When looking up multiple keys in insertion order, each lookup hits on the first probe, making the total work O(N) instead of O(N^2). For example:

#include <sourcemeta/core/json.h>
#include <cassert>
const sourcemeta::core::JSON document =
sourcemeta::core::parse_json("{ \"foo\": 1, \"bar\": 2 }");
const auto &object{document.as_object()};
typename decltype(object)::size_type start{0};
const auto hash_foo{object.hash("foo")};
const auto *foo{document.try_at("foo", hash_foo, start)};
assert(foo);
assert(foo->to_integer() == 1);
const auto hash_bar{object.hash("bar")};
const auto *bar{document.try_at("bar", hash_bar, start)};
assert(bar);
assert(bar->to_integer() == 2);

◆ try_at() [6/8]

template<typename T>
SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::try_at ( T key,
const typename Object::hash_type hash ) -> JSON *
inlinenodiscard

This method tries to retrieve a mutable object element by string view key given a pre-calculated property hash

◆ try_at() [7/8]

template<typename T>
SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::try_at ( T key,
const typename Object::hash_type hash ) const -> const JSON *
inlinenodiscard

This method tries to retrieve an object element by string view key given a pre-calculated property hash

◆ try_at() [8/8]

template<typename T>
SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::try_at ( T key,
const typename Object::hash_type hash,
typename Object::size_type & start ) const -> const JSON *
inlinenodiscard

This method tries to retrieve an object element by string view key, scanning from a caller-provided start offset

◆ type()

SOURCEMETA_FORCEINLINE auto sourcemeta::core::JSON::type ( ) const -> Type
inlinenodiscardnoexcept

Get the type of the JSON document. For example:

#include <sourcemeta/core/json.h>
#include <cassert>
const sourcemeta::core::JSON document{true};
@ Boolean
The JSON boolean type.
Definition json_value.h:73

◆ unique()

auto sourcemeta::core::JSON::unique ( ) const -> bool
nodiscard

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

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

◆ unique_keys()

auto sourcemeta::core::JSON::unique_keys ( ) const -> bool
nodiscard

This method checks if a JSON object does not name any key more than once. The parser preserves repeated members rather than collapsing them, so this detects a document whose object named a key twice. For example:

#include <sourcemeta/core/json.h>
#include <cassert>
const auto document{sourcemeta::core::parse_json(R"({ "foo": 1 })")};
assert(document.unique_keys());

Function Documentation

◆ from_json() [1/3]

template<typename T>
auto sourcemeta::core::from_json ( const JSON & value) -> std::optional< T >

If the value has a .from_json() static method, always prefer that

◆ from_json() [2/3]

template<json_auto_map_like T>
auto sourcemeta::core::from_json ( const JSON & value,
const std::function< std::optional< typename T::mapped_type >(const JSON &)> & callback ) -> std::optional< T >

Convert a JSON object into a map-like value using a custom callback

◆ from_json() [3/3]

template<json_auto_list_like T>
auto sourcemeta::core::from_json ( const JSON & value,
const std::function< std::optional< typename T::value_type >(const JSON &)> & callback ) -> std::optional< T >

Convert a JSON array into a list-like value using a custom callback

◆ make_set()

SOURCEMETA_FORCEINLINE auto sourcemeta::core::make_set ( std::initializer_list< JSON::Type > types) -> JSON::TypeSet
inline

Create a JSON type set from an initializer list of types. For example:

#include <sourcemeta/core/json.h>
const auto types = sourcemeta::core::make_set(
@ Array
The JSON array type.
Definition json_value.h:81
@ Object
The JSON object type.
Definition json_value.h:83
SOURCEMETA_FORCEINLINE auto make_set(std::initializer_list< JSON::Type > types) -> JSON::TypeSet
Definition json.h:335

◆ parse_json() [1/8]

SOURCEMETA_CORE_JSON_EXPORT auto sourcemeta::core::parse_json ( const std::basic_string_view< 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/core/json.h>
#include <cassert>
const sourcemeta::core::JSON document =
assert(document.is_array());

If parsing fails, sourcemeta::core::JSONParseError will be thrown.

A string escape that denotes a lone surrogate, such as "\uDEAD", is rejected. RFC 8259 Section 8.2 considers such input grammar-conforming, but a lone surrogate cannot be represented in valid UTF-8, and every string this parser produces is valid UTF-8. Rejecting it is a deliberate exercise of the RFC 8259 Section 9 allowance that an implementation may set limits on the character contents of strings.

◆ parse_json() [2/8]

SOURCEMETA_CORE_JSON_EXPORT auto sourcemeta::core::parse_json ( const std::basic_string_view< JSON::Char, JSON::CharTraits > input,
JSON & output,
const JSON::ParseCallback & callback ) -> void

Parse a JSON document from a JSON string into an existing JSON value, invoking the given callback during parsing. The result is constructed directly into the given reference rather than returned by value to ensure that references passed through the parse callback (such as object property names) remain valid after parsing completes.

If parsing fails, sourcemeta::core::JSONParseError will be thrown.

◆ parse_json() [3/8]

SOURCEMETA_CORE_JSON_EXPORT auto sourcemeta::core::parse_json ( const std::basic_string_view< 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/core/json.h>
#include <cassert>
std::uint64_t line{1};
std::uint64_t column{0};
const sourcemeta::core::JSON document =
sourcemeta::core::parse_json("[ 1, 2, 3 ]", line, column);
assert(document.is_array());

◆ parse_json() [4/8]

SOURCEMETA_CORE_JSON_EXPORT auto sourcemeta::core::parse_json ( const std::basic_string_view< JSON::Char, JSON::CharTraits > input,
std::uint64_t & line,
std::uint64_t & column,
JSON & output,
const JSON::ParseCallback & callback ) -> void

Parse a JSON document from a JSON string into an existing JSON value, passing your own line and column read/write position indicators and invoking the given callback during parsing. The result is constructed directly into the given reference rather than returned by value to ensure that references passed through the parse callback (such as object property names) remain valid after parsing completes.

◆ parse_json() [5/8]

SOURCEMETA_CORE_JSON_EXPORT auto sourcemeta::core::parse_json ( 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/core/json.h>
#include <cassert>
#include <sstream>
std::istringstream stream{"[ 1, 2, 3 ]"};
const sourcemeta::core::JSON document =
assert(document.is_array());

If parsing fails, sourcemeta::core::JSONParseError will be thrown.

A string escape that denotes a lone surrogate, such as "\uDEAD", is rejected. RFC 8259 Section 8.2 considers such input grammar-conforming, but a lone surrogate cannot be represented in valid UTF-8, and every string this parser produces is valid UTF-8. Rejecting it is a deliberate exercise of the RFC 8259 Section 9 allowance that an implementation may set limits on the character contents of strings.

◆ parse_json() [6/8]

SOURCEMETA_CORE_JSON_EXPORT auto sourcemeta::core::parse_json ( std::basic_istream< JSON::Char, JSON::CharTraits > & stream,
JSON & output,
const JSON::ParseCallback & callback ) -> void

Parse a JSON document from a C++ standard input stream into an existing JSON value, invoking the given callback during parsing. The result is constructed directly into the given reference rather than returned by value to ensure that references passed through the parse callback (such as object property names) remain valid after parsing completes.

If parsing fails, sourcemeta::core::JSONParseError will be thrown.

◆ parse_json() [7/8]

SOURCEMETA_CORE_JSON_EXPORT auto sourcemeta::core::parse_json ( 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/core/json.h>
#include <cassert>
#include <sstream>
std::istringstream stream{"[ 1, 2, 3 ]"};
std::uint64_t line{1};
std::uint64_t column{0};
const sourcemeta::core::JSON document =
sourcemeta::core::parse_json(stream, line, column);
assert(document.is_array());

◆ parse_json() [8/8]

SOURCEMETA_CORE_JSON_EXPORT auto sourcemeta::core::parse_json ( std::basic_istream< JSON::Char, JSON::CharTraits > & stream,
std::uint64_t & line,
std::uint64_t & column,
JSON & output,
const JSON::ParseCallback & callback ) -> void

Parse a JSON document from a C++ standard input stream into an existing JSON value, passing your own line and column read/write position indicators and invoking the given callback during parsing. The result is constructed directly into the given reference rather than returned by value to ensure that references passed through the parse callback (such as object property names) remain valid after parsing completes.

◆ prettify()

SOURCEMETA_CORE_JSON_EXPORT auto sourcemeta::core::prettify ( const JSON & document,
std::basic_ostream< JSON::Char, JSON::CharTraits > & stream,
const std::size_t spaces = 2 ) -> void

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

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

◆ read_json() [1/2]

SOURCEMETA_CORE_JSON_EXPORT auto sourcemeta::core::read_json ( const std::filesystem::path & path) -> JSON

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

#include <sourcemeta/core/json.h>
#include <cassert>
#include <iostream>
const sourcemeta::core::JSON document =
sourcemeta::core::read_json("/tmp/foo.json");
sourcemeta::core::stringify(document, std::cout);
std::cout << std::endl;
SOURCEMETA_CORE_DIFF_EXPORT auto stringify(const Diff &document, std::ostream &stream, const Diff::Format format, const Diff::FormatOptions &options={}) -> void
SOURCEMETA_CORE_JSON_EXPORT auto read_json(const std::filesystem::path &path) -> JSON

If parsing fails, sourcemeta::core::JSONFileParseError will be thrown.

◆ read_json() [2/2]

SOURCEMETA_CORE_JSON_EXPORT auto sourcemeta::core::read_json ( const std::filesystem::path & path,
JSON & output,
const JSON::ParseCallback & callback ) -> void

A convenience function to parse a JSON document from a file into an existing JSON value, invoking the given callback during parsing. The result is constructed directly into the given reference rather than returned by value to ensure that references passed through the parse callback (such as object property names) remain valid after parsing completes.

If parsing fails, sourcemeta::core::JSONFileParseError will be thrown.

◆ stringify()

SOURCEMETA_CORE_JSON_EXPORT auto sourcemeta::core::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/core/json.h>
#include <iostream>
#include <sstream>
const sourcemeta::core::JSON document =
std::ostringstream stream;
sourcemeta::core::stringify(document, stream);
std::cout << stream.str() << std::endl;

◆ to_json() [1/7]

template<typename T>
auto sourcemeta::core::to_json ( const std::optional< T > & value) -> JSON

Convert an optional value into JSON, using null when empty

◆ to_json() [2/7]

template<typename L, typename R>
auto sourcemeta::core::to_json ( const std::pair< L, R > & value) -> JSON

Convert a pair into a JSON array of two elements

◆ to_json() [3/7]

template<typename T>
auto sourcemeta::core::to_json ( const T & value) -> JSON

If the value has a .to_json() method, always prefer that

◆ to_json() [4/7]

template<json_auto_list_like T, std::invocable< const typename T::value_type & > F>
auto sourcemeta::core::to_json ( const T & value,
const F & callback ) -> JSON

Convert a list-like value into a JSON array using a custom callback

◆ to_json() [5/7]

template<typename T>
auto sourcemeta::core::to_json ( const T value) -> JSON

Convert a file time point into JSON

◆ to_json() [6/7]

template<json_auto_list_like T>
auto sourcemeta::core::to_json ( typename T::const_iterator begin,
typename T::const_iterator end ) -> JSON

Convert a range of list-like elements into a JSON array

◆ to_json() [7/7]

template<json_auto_list_like T, std::invocable< const typename T::value_type & > F>
auto sourcemeta::core::to_json ( typename T::const_iterator begin,
typename T::const_iterator end,
const F & callback ) -> JSON

Convert a range of list-like elements into a JSON array using a custom callback

◆ try_parse_json()

SOURCEMETA_CORE_JSON_EXPORT auto sourcemeta::core::try_parse_json ( const std::basic_string_view< JSON::Char, JSON::CharTraits > input) -> std::optional< JSON >

Create a JSON document from a JSON string, returning no value instead of throwing when the input is not valid JSON. For example:

#include <sourcemeta/core/json.h>
#include <cassert>
const auto document{sourcemeta::core::try_parse_json("[ 1, 2, 3 ]")};
assert(document.has_value());
assert(document.value().is_array());
assert(!sourcemeta::core::try_parse_json("[ 1, 2,").has_value());
SOURCEMETA_CORE_JSON_EXPORT auto try_parse_json(const std::basic_string_view< JSON::Char, JSON::CharTraits > input) -> std::optional< JSON >