JSON Voorhees
Killer JSON for C++
|
Represents a single JSON value, which can be any one of a potential kind
, each behaving slightly differently.
More...
#include <jsonv/value.hpp>
Classes | |
struct | basic_array_iterator |
The base type for iterating over array values. More... | |
struct | basic_object_iterator |
The base iterator type for iterating over object types. More... | |
struct | object_insert_return_type |
Type returned from insert operations when this has kind::object. More... | |
Public Types | |
typedef std::size_t | size_type |
typedef std::ptrdiff_t | difference_type |
typedef basic_array_iterator< value, value > | array_iterator |
The array_iterator is applicable when kind is kind::array . More... | |
typedef basic_array_iterator< const value, const value > | const_array_iterator |
typedef detail::basic_view< array_iterator, const_array_iterator > | array_view |
If kind is kind::array , an array_view allows you to access a value as a sequence container. More... | |
typedef detail::basic_view< const_array_iterator > | const_array_view |
typedef detail::basic_owning_view< value, array_iterator, const_array_iterator > | owning_array_view |
typedef std::pair< const std::string, value > | object_value_type |
The type of value stored when kind is kind::object . More... | |
typedef basic_object_iterator< object_value_type, std::map< std::string, value >::iterator > | object_iterator |
The object_iterator is applicable when kind is kind::object . More... | |
typedef basic_object_iterator< const object_value_type, std::map< std::string, value >::const_iterator > | const_object_iterator |
typedef detail::basic_view< object_iterator, const_object_iterator > | object_view |
If kind is kind::object , an object_view allows you to access a value as an associative container. More... | |
typedef detail::basic_view< const_object_iterator > | const_object_view |
typedef detail::basic_owning_view< value, object_iterator, const_object_iterator > | owning_object_view |
Public Member Functions | |
constexpr | value () |
Default-construct this to null. More... | |
value (std::nullptr_t)=delete | |
The nullptr overload will fail to compile – use null if you want a kind::null . More... | |
value (const value &source) | |
Copy the contents of source into a new instance. More... | |
value (const std::string &value) | |
Create a kind::string with the given value. More... | |
value (const string_view &value) | |
Create a kind::string with the given value. More... | |
value (const char *value) | |
Create a kind::string with the given value. More... | |
value (const std::wstring &value) | |
Create a kind::string with the given value. More... | |
value (const wchar_t *value) | |
Create a kind::string with the given value. More... | |
value (int64_t value) | |
Create a kind::integer with the given value. More... | |
value (double value) | |
Create a kind::decimal with the given value. More... | |
value (float value) | |
Create a kind::decimal with the given value. More... | |
value (bool value) | |
Create a kind::boolean with the given value. More... | |
~value () noexcept | |
Destruction will never throw. More... | |
value & | operator= (const value &source) |
Copy-assigns source to this. More... | |
value (value &&source) noexcept | |
Move-construct this instance, leaving source as a null value. More... | |
value & | operator= (value &&source) noexcept |
Move-assigns source to this, leaving source as a null value. More... | |
const std::string & | as_string () const |
Get this value as a string. More... | |
bool | is_string () const |
Tests if this kind is kind::string . More... | |
string_view | as_string_view () const & |
Get this value as a string_view . More... | |
std::wstring | as_wstring () const |
Get this value as a wide string. More... | |
int64_t | as_integer () const |
Get this value as an integer. More... | |
bool | is_integer () const |
Tests if this kind is kind::integer . More... | |
double | as_decimal () const |
Get this value as a decimal. More... | |
bool | is_decimal () const |
Tests if this kind is kind::integer or kind::decimal . More... | |
bool | as_boolean () const |
Get this value as a boolean. More... | |
bool | is_boolean () const |
Tests if this kind is kind::boolean . More... | |
bool | is_array () const |
Tests if this kind is kind::array . More... | |
bool | is_object () const |
Tests if this kind is kind::object . More... | |
bool | is_null () const |
Tests if this kind is kind::null . More... | |
void | clear () |
Resets this value to null. More... | |
jsonv::kind | kind () const |
Get this value's kind. More... | |
value & | at_path (const path &p) |
Get the value specified by the path p. More... | |
value & | at_path (string_view p) |
value & | at_path (size_type p) |
const value & | at_path (const path &p) const |
const value & | at_path (string_view p) const |
const value & | at_path (size_type p) const |
size_type | count_path (const path &p) const |
Similar to count , but walks the given path p to determine its presence. More... | |
size_type | count_path (string_view p) const |
size_type | count_path (size_type p) const |
value & | path (const path &p) |
Get or create the value specified by the path p. More... | |
value & | path (string_view p) |
value & | path (size_type p) |
void | swap (value &other) noexcept |
Swap the value this instance represents with other. More... | |
bool | operator== (const value &other) const |
Compares two JSON values for equality. More... | |
bool | operator!= (const value &other) const |
Compares two JSON values for inequality. More... | |
int | compare (const value &other) const |
Used to build a strict-ordering of JSON values. More... | |
bool | operator< (const value &other) const |
bool | operator> (const value &other) const |
bool | operator<= (const value &other) const |
bool | operator>= (const value &other) const |
array_iterator | begin_array () |
Get an iterator to the beginning of this array. More... | |
const_array_iterator | begin_array () const |
array_iterator | end_array () |
Get an iterator to the end of this array. More... | |
const_array_iterator | end_array () const |
array_view | as_array ()& |
View this instance as an array. More... | |
const_array_view | as_array () const & |
owning_array_view | as_array ()&& |
value & | operator[] (size_type idx) |
Get the value in this array at the given idx. More... | |
const value & | operator[] (size_type idx) const |
value & | operator[] (int idx) |
const value & | operator[] (int idx) const |
value & | at (size_type idx) |
Get the value in this array at the given idx. More... | |
const value & | at (size_type idx) const |
void | push_back (value item) |
Push item to the back of this array. More... | |
void | pop_back () |
Pop an item off the back of this array. More... | |
void | push_front (value item) |
Push item to the front of this array. More... | |
void | pop_front () |
Pop an item from the front of this array. More... | |
array_iterator | insert (const_array_iterator position, value item) |
Insert an item into position on this array. More... | |
template<typename TForwardIterator > | |
array_iterator | insert (const_array_iterator position, TForwardIterator first, TForwardIterator last) |
Insert the range defined by [first, last) at position in this array. More... | |
void | assign (size_type count, const value &val) |
Assign count elements to this array with val. More... | |
template<typename TForwardIterator > | |
void | assign (TForwardIterator first, TForwardIterator last) |
Assign the contents of range [first, last) to this array. More... | |
void | assign (std::initializer_list< value > items) |
Assign the given items to this array. More... | |
void | resize (size_type count, const value &val=value()) |
Resize the length of this array to count items. More... | |
array_iterator | erase (const_array_iterator position) |
Erase the item at this array's position. More... | |
array_iterator | erase (const_array_iterator first, const_array_iterator last) |
Erase the range [first, last) from this array. More... | |
object_iterator | begin_object () |
Get an iterator to the first key-value pair in this object. More... | |
const_object_iterator | begin_object () const |
object_iterator | end_object () |
Get an iterator to the one past the end of this object. More... | |
const_object_iterator | end_object () const |
object_view | as_object ()& |
View this instance as an object. More... | |
const_object_view | as_object () const & |
owning_object_view | as_object ()&& |
value & | operator[] (const std::string &key) |
Get the value associated with the given key of this object. More... | |
value & | operator[] (std::string &&key) |
value & | operator[] (const std::wstring &key) |
value & | at (const std::string &key) |
Get the value associated with the given key of this object. More... | |
value & | at (const std::wstring &key) |
const value & | at (const std::string &key) const |
const value & | at (const std::wstring &key) const |
size_type | count (const std::string &key) const |
Check if the given key exists in this object. More... | |
size_type | count (const std::wstring &key) const |
object_iterator | find (const std::string &key) |
Attempt to locate a key-value pair with the provided key in this object. More... | |
object_iterator | find (const std::wstring &key) |
const_object_iterator | find (const std::string &key) const |
const_object_iterator | find (const std::wstring &key) const |
bool | empty () const noexcept |
Is the underlying structure empty? More... | |
size_type | size () const |
Get the number of items in this value. More... | |
value | map (const std::function< value(const value &)> &func) const & |
Run a function over the values of this instance. More... | |
value | map (const std::function< value(value)> &func)&& |
Run a function over the values of this instance. More... | |
std::pair< object_iterator, bool > | insert (std::pair< std::string, value > pair) |
std::pair< object_iterator, bool > | insert (std::pair< std::wstring, value > pair) |
object_iterator | insert (const_object_iterator hint, std::pair< std::string, value > pair) |
object_iterator | insert (const_object_iterator hint, std::pair< std::wstring, value > pair) |
template<typename TForwardIterator > | |
void | insert (TForwardIterator first, TForwardIterator last) |
Insert range defined by [first, last) into this object. More... | |
object_insert_return_type | insert (object_node_handle &&handle) |
Insert the contents of handle. More... | |
object_iterator | insert (const_object_iterator hint, object_node_handle &&handle) |
If handle is an empty node handle, does nothing and returns end_object. More... | |
void | insert (std::initializer_list< std::pair< std::string, value >> items) |
Insert items into this object. More... | |
void | insert (std::initializer_list< std::pair< std::wstring, value >> items) |
size_type | erase (const std::string &key) |
size_type | erase (const std::wstring &key) |
object_iterator | erase (const_object_iterator position) |
Erase the item at the given position. More... | |
object_iterator | erase (const_object_iterator first, const_object_iterator last) |
Erase the range defined by [first, last). More... | |
object_node_handle | extract (const_object_iterator position) |
object_node_handle | extract (const std::string &key) |
If the container has an element with the given key, unlinks the node that contains that element from the container and returns a node handle that owns it. More... | |
object_node_handle | extract (const std::wstring &key) |
Friends | |
std::ostream & | operator<< (std::ostream &stream, const value &val) |
Output this value to a stream. More... | |
std::string | to_string (const value &) |
Get a string representation of the given value . More... | |
JSONV_PUBLIC value | array () |
Create an empty array value. More... | |
JSONV_PUBLIC value | object () |
Create an empty object. More... | |
Represents a single JSON value, which can be any one of a potential kind
, each behaving slightly differently.
Instances will vary their behavior based on their kind – functions will throw a kind_error
if the operation does not apply to the value's kind. For example, it does not make sense to call find
on an integer
.
kind::null
You cannot do anything with this...it is just null.kind::boolean
These values can be true
or false
.kind::integer
A numeric value which can be added, subtracted and all the other things you would expect.kind::decimal
Floating-point values should be considered "more general" than integers – you may request an integer value as a decimal, but you cannot request a decimal as an integer, even when doing so would not require rounding. The literal 20.0
will always have kind::decimal
.kind::string
A UTF-8 encoded string which is mostly accessed through the std::string
class. Some random functions work in the cases where it makes sense (for example: empty
and size
), but in general, string manipulation should be done after calling as_string
.kind::array
An array behaves like a std::deque
because it is ultimately backed by one. If you feel the documentation is lacking, read this: http://en.cppreference.com/w/cpp/container/deque.kind::object
An object behaves lake a std::map
because it is ultimately backed by one. If you feel the documentation is lacking, read this: http://en.cppreference.com/w/cpp/container/map. This library follows the recommendation in RFC 7159 to not allow for duplicate keys because most other libraries can not deal with it. It would also make the AST significantly more painful.struct jsonv::value::object_insert_return_type |
Type returned from insert
operations when this has kind::object.
It is generally compatible with the insert_return_type
of std::map
, with the notable lack of node
.
Class Members | ||
---|---|---|
bool | inserted |
Did the insert operation perform an insert? A value of false indicates there was a key already present with the same name. |
const_object_iterator | position | The position of the inserted node or node with the duplicate key. |
typedef basic_object_iterator<object_value_type, std::map<std::string, value>::iterator> jsonv::value::object_iterator |
typedef std::pair<const std::string, value> jsonv::value::object_value_type |
|
inline |
|
delete |
The nullptr overload will fail to compile – use null
if you want a kind::null
.
jsonv::value::value | ( | const value & | source | ) |
Copy the contents of source into a new instance.
jsonv::value::value | ( | const std::string & | value | ) |
Create a kind::string
with the given value.
jsonv::value::value | ( | const string_view & | value | ) |
Create a kind::string
with the given value.
jsonv::value::value | ( | const char * | value | ) |
Create a kind::string
with the given value.
value | The value to create with. This must be null-terminated. |
jsonv::value::value | ( | const std::wstring & | value | ) |
Create a kind::string
with the given value.
Keep in mind that it will be converted to and stored as a UTF-8 encoded string.
jsonv::value::value | ( | const wchar_t * | value | ) |
Create a kind::string
with the given value.
Keep in mind that it will be converted to and stored as a UTF-8 encoded string.
value | The value to create with. This must be null-terminated. |
jsonv::value::value | ( | int64_t | value | ) |
Create a kind::integer
with the given value.
jsonv::value::value | ( | double | value | ) |
Create a kind::decimal
with the given value.
jsonv::value::value | ( | float | value | ) |
Create a kind::decimal
with the given value.
jsonv::value::value | ( | bool | value | ) |
Create a kind::boolean
with the given value.
|
noexcept |
Destruction will never throw.
|
noexcept |
Move-construct this instance, leaving source as a null value.
array_view jsonv::value::as_array | ( | ) |
View this instance as an array.
kind_error | if the kind is not an array. |
bool jsonv::value::as_boolean | ( | ) | const |
Get this value as a boolean.
kind_error | if this value does not represent a boolean. |
double jsonv::value::as_decimal | ( | ) | const |
Get this value as a decimal.
If the value's underlying kind is actually an integer type, cast the integer to a double before returning. This ignores the potential loss of precision.
kind_error | if this value does not represent a decimal or integer. |
int64_t jsonv::value::as_integer | ( | ) | const |
Get this value as an integer.
kind_error | if this value does not represent an integer. |
object_view jsonv::value::as_object | ( | ) |
View this instance as an object.
kind_error | if the kind is not an object. |
const std::string& jsonv::value::as_string | ( | ) | const |
Get this value as a string.
kind_error | if this value does not represent a string. |
string_view jsonv::value::as_string_view | ( | ) | const |
Get this value as a string_view
.
It is your responsibility to ensure the value
instance remains valid.
kind_error | if this value does not represent a string. |
std::wstring jsonv::value::as_wstring | ( | ) | const |
Get this value as a wide string.
Keep in mind that this is slower than as_string
, as the internal storage is the char
base std::string
.
kind_error | if this value does not represent a string. |
void jsonv::value::assign | ( | size_type | count, |
const value & | val | ||
) |
Assign count elements to this array with val.
kind_error | if the kind is not an array. |
|
inline |
Assign the contents of range [first, last) to this array.
kind_error | if the kind is not an array. |
void jsonv::value::assign | ( | std::initializer_list< value > | items | ) |
Assign the given items to this array.
kind_error | if the kind is not an array. |
value& jsonv::value::at | ( | size_type | idx | ) |
Get the value in this array at the given idx.
kind_error | if the kind is not an array. |
std::out_of_range | if the provided idx is above size . |
value& jsonv::value::at | ( | const std::string & | key | ) |
Get the value associated with the given key of this object.
kind_error | if the kind is not an object. |
std::out_of_range | if the key is not in this object. |
Get the value specified by the path p.
std::out_of_range | if any path along the chain did not exist. |
kind_error | if the path traversal is not valid for the value (for example: if the path specifies an array index when the value is a string). |
parse_error | if a string_view was specified that did not have a valid specification (see path::create ). |
array_iterator jsonv::value::begin_array | ( | ) |
Get an iterator to the beginning of this array.
kind_error | if the kind is not an array. |
object_iterator jsonv::value::begin_object | ( | ) |
Get an iterator to the first key-value pair in this object.
kind_error | if the kind is not an object. |
void jsonv::value::clear | ( | ) |
Resets this value to null.
int jsonv::value::compare | ( | const value & | other | ) | const |
Used to build a strict-ordering of JSON values.
When comparing values of the same kind, the ordering should align with your intuition. When comparing values of different kinds, some arbitrary rules were created based on how "complicated" the author thought the type to be.
size_type jsonv::value::count | ( | const std::string & | key | ) | const |
Check if the given key exists in this object.
kind_error | if the kind is not an object. |
size_type jsonv::value::count_path | ( | const path & | p | ) | const |
Similar to count
, but walks the given path p to determine its presence.
1
if the path finds an element; 0
if there is no such path in the tree.parse_error | if a string_view was specified that did not have a valid specification (see path::create ). |
|
noexcept |
Is the underlying structure empty?
nothing |
array_iterator jsonv::value::end_array | ( | ) |
Get an iterator to the end of this array.
kind_error | if the kind is not an array. |
object_iterator jsonv::value::end_object | ( | ) |
Get an iterator to the one past the end of this object.
kind_error | if the kind is not an object. |
array_iterator jsonv::value::erase | ( | const_array_iterator | position | ) |
Erase the item at this array's position.
kind_error | if the kind is not an array. |
array_iterator jsonv::value::erase | ( | const_array_iterator | first, |
const_array_iterator | last | ||
) |
Erase the range [first, last) from this array.
kind_error | if the kind is not an array. |
size_type jsonv::value::erase | ( | const std::string & | key | ) |
Erase the item with the given key.
kind_error | if the kind is not an object. |
object_iterator jsonv::value::erase | ( | const_object_iterator | position | ) |
Erase the item at the given position.
kind_error | if the kind is not an object. |
object_iterator jsonv::value::erase | ( | const_object_iterator | first, |
const_object_iterator | last | ||
) |
Erase the range defined by [first, last).
kind_error | if the kind is not an object. |
object_node_handle jsonv::value::extract | ( | const_object_iterator | position | ) |
Unlinks the node that contains the element pointed to by position and returns a node handle that owns it.
kind_error | if the kind is not an object. |
object_node_handle jsonv::value::extract | ( | const std::string & | key | ) |
If the container has an element with the given key, unlinks the node that contains that element from the container and returns a node handle that owns it.
Otherwise, returns an empty node handle.
kind_error | if the kind is not an object. |
object_iterator jsonv::value::find | ( | const std::string & | key | ) |
Attempt to locate a key-value pair with the provided key in this object.
kind_error | if the kind is not an object. |
array_iterator jsonv::value::insert | ( | const_array_iterator | position, |
value | item | ||
) |
Insert an item into position on this array.
kind_error | if the kind is not an array. |
|
inline |
Insert the range defined by [first, last) at position in this array.
kind_error | if the kind is not an array. |
std::pair<object_iterator, bool> jsonv::value::insert | ( | std::pair< std::string, value > | pair | ) |
Insert pair into this object. If hint is provided, this insertion could be optimized.
first
refers to the newly-inserted element (or the element which shares the key). kind_error | if the kind is not an object. |
|
inline |
Insert range defined by [first, last) into this object.
kind_error | if the kind is not an object. |
object_insert_return_type jsonv::value::insert | ( | object_node_handle && | handle | ) |
Insert the contents of handle.
If handle is empty, this does nothing.
inserted
is false
and position
is end_object()
. If the insertion took place, inserted
is true
and position
points to the inserted element. If the insertion was attempted but failed, inserted
is false
and position
points to an element with a key equivalent to handle.key()
. kind_error | if the kind is not an object. |
object_iterator jsonv::value::insert | ( | const_object_iterator | hint, |
object_node_handle && | handle | ||
) |
If handle is an empty node handle, does nothing and returns end_object.
Otherwise, inserts the element owned by handle into the container, if the container doesn't already contain an element with a key equivalent to handle.key()
and returns the iterator pointing to the element with key equivalent to handle.key()
. If the insertion succeeds, handle is moved from, otherwise it retains ownership of the element. The element is inserted as close as possible to the position just prior to hint.
handle.key()
if handle was not empty. If handle was empty, end_object()
. kind_error | if the kind is not an object. |
void jsonv::value::insert | ( | std::initializer_list< std::pair< std::string, value >> | items | ) |
Insert items into this object.
kind_error | if the kind is not an object. |
bool jsonv::value::is_array | ( | ) | const |
Tests if this kind
is kind::array
.
bool jsonv::value::is_boolean | ( | ) | const |
Tests if this kind
is kind::boolean
.
bool jsonv::value::is_decimal | ( | ) | const |
Tests if this kind
is kind::integer
or kind::decimal
.
bool jsonv::value::is_integer | ( | ) | const |
Tests if this kind
is kind::integer
.
bool jsonv::value::is_null | ( | ) | const |
Tests if this kind
is kind::null
.
bool jsonv::value::is_object | ( | ) | const |
Tests if this kind
is kind::object
.
bool jsonv::value::is_string | ( | ) | const |
Tests if this kind
is kind::string
.
|
inline |
bool jsonv::value::operator!= | ( | const value & | other | ) | const |
Compares two JSON values for inequality.
The rules for inequality are the exact opposite of equality.
Copy-assigns source
to this.
If an exception is thrown during the copy, it is propagated out. This instance will remain unchanged.
Move-assigns source
to this, leaving source as a null value.
Unlike a copy, this will never throw.
bool jsonv::value::operator== | ( | const value & | other | ) | const |
Compares two JSON values for equality.
Two JSON values are equal if and only if all of the following conditions apply:
kind
.kind
is invalid (memory corruption), then two JSON values are not equal, even if they have been corrupted in the same way and even if they share this
(a corrupt object is not equal to itself).dict
and list
. value& jsonv::value::operator[] | ( | size_type | idx | ) |
Get the value in this array at the given idx.
The overloads which accept an int
are required to resolve the type ambiguity of the literal 0
between a size_type and a char*.
kind_error | if the kind is not an array. |
value& jsonv::value::operator[] | ( | const std::string & | key | ) |
Get the value associated with the given key of this object.
If the key does not exist, it will be created.
kind_error | if the kind is not an object. |
Get or create the value specified by the path p.
This is the moral equivalent to operator
[] for paths. If no value exists at the path, a new one is created as the default (null
) value. If any path along the way either does not exist or is null
, it is created for you, based on the implications of the specification p. Unlike at_path
, which will throw if accessing a non-existent key of an object
or going past the end of an array
, this will simply create that path and fill in the blanks with null
values.
kind_error | if the path traversal is not valid for the value (for example: if the path specifies an array index when the value is a string). |
parse_error | if a string_view was specified that did not have a valid specification (see path::create ). |
void jsonv::value::pop_back | ( | ) |
Pop an item off the back of this array.
kind_error | if the kind is not an array. |
std::logic_error | if the array is empty. |
void jsonv::value::pop_front | ( | ) |
Pop an item from the front of this array.
kind_error | if the kind is not an array. |
std::logic_error | if the array is empty. |
void jsonv::value::push_back | ( | value | item | ) |
Push item to the back of this array.
kind_error | if the kind is not an array. |
void jsonv::value::push_front | ( | value | item | ) |
Push item to the front of this array.
kind_error | if the kind is not an array. |
Resize the length of this array to count items.
If the resize creates new elements, fill those newly-created elements with val.
kind_error | if the kind is not an array. |
size_type jsonv::value::size | ( | ) | const |
Get the number of items in this value.
\0
values and counting multi-byte encodings as more than one value).kind_error | if the kind is not an object, array or string. |
|
noexcept |
Swap the value this instance represents with other.
|
friend |
Create an empty array value.
|
friend |
Create an empty object.
|
friend |
Output this value to a stream.
|
friend |
Get a string representation of the given value
.