19#include <initializer_list>
34class object_node_handle;
52 constexpr value_storage() :
75 public std::logic_error
78 explicit kind_error(
const std::string& description);
115 typedef std::size_t size_type;
116 typedef std::ptrdiff_t difference_type;
119 template <
typename T,
typename TArrayView>
123 using iterator_category = std::random_access_iterator_tag;
124 using value_type = T;
125 using difference_type = std::ptrdiff_t;
127 using reference = T&;
138 template <
typename U,
typename UArrayView>
140 typename std::enable_if<std::is_convertible<U*, T*>::value>::type* = 0
142 _owner(source._owner),
143 _index(source._index)
172 template <
typename U,
typename UArrayView>
176 return _owner == other._owner && _index == other._index;
179 template <
typename U,
typename UArrayView>
183 return !operator==(other);
189 return _owner->operator[](_index);
193 T* operator->()
const
195 return &_owner->operator[](_index);
229 return difference_type(_index) - difference_type(other._index);
235 return _index < rhs._index;
241 return _index <= rhs._index;
247 return _index > rhs._index;
253 return _index >= rhs._index;
257 T& operator[](size_type n)
const
259 return _owner->operator[](_index + n);
262 template <
typename U,
typename UArrayView>
281 typedef detail::basic_view<array_iterator, const_array_iterator>
array_view;
282 typedef detail::basic_view<const_array_iterator> const_array_view;
283 typedef detail::basic_owning_view<value, array_iterator, const_array_iterator> owning_array_view;
288 template <
typename T,
typename TIterator>
292 using iterator_category = std::bidirectional_iterator_tag;
293 using value_type = T;
294 using difference_type = std::ptrdiff_t;
296 using reference = T&;
307 template <
typename U,
typename UIterator>
309 typename std::enable_if<std::is_convertible<U*, T*>::value>::type* = 0
316 _impl = source._impl;
320 template <
typename U,
typename UIterator>
321 typename std::enable_if<std::is_convertible<U*, T*>::value, basic_object_iterator&>::type
322 operator=(
const basic_object_iterator<U, UIterator>& source)
324 return operator=(basic_object_iterator(source));
327 basic_object_iterator& operator++()
333 basic_object_iterator operator++(
int)
335 basic_object_iterator clone(*
this);
340 basic_object_iterator& operator--()
346 basic_object_iterator operator--(
int)
348 basic_object_iterator clone(*
this);
353 template <
typename U,
typename UIterator>
355 bool operator ==(
const basic_object_iterator<U, UIterator>& other)
const
357 return _impl == other._impl;
360 template <
typename U,
typename UIterator>
362 bool operator !=(
const basic_object_iterator<U, UIterator>& other)
const
364 return _impl != other._impl;
368 T& operator *()
const
374 T* operator ->()
const
382 template <
typename UIterator>
383 explicit basic_object_iterator(
const UIterator& iter) :
418 typedef detail::basic_view<object_iterator, const_object_iterator>
object_view;
419 typedef detail::basic_view<const_object_iterator> const_object_view;
420 typedef detail::basic_owning_view<value, object_iterator, const_object_iterator> owning_object_view;
487 #define JSONV_VALUE_INTEGER_ALTERNATIVE_CTOR_PROTO_GENERATOR(type_) \
490 #undef JSONV_VALUE_INTEGER_ALTERNATIVE_CTOR_PROTO_GENERATOR
515 const
std::
string& as_string() const;
519 bool is_string() const;
526 std::string_view as_string_view() const &;
536 std::wstring as_wstring() const;
543 int64_t as_integer() const;
547 bool is_integer() const;
555 double as_decimal() const;
559 bool is_decimal() const;
566 bool as_boolean() const;
570 bool is_boolean() const;
574 bool is_array() const;
578 bool is_object() const;
582 bool is_null() const;
712 bool operator< (
const value& other)
const;
714 bool operator> (
const value& other)
const;
716 bool operator<=(
const value& other)
const;
718 bool operator>=(
const value& other)
const;
759 owning_array_view as_array() &&;
770 const
value& operator[](size_type idx) const;
811 template <
typename TForwardIterator>
816 for (difference_type offset = orig_offset ; first != last; ++first, ++offset)
817 insert(begin_array() + offset, *first);
818 return begin_array() + orig_offset;
829 template <
typename TForwardIterator>
830 void assign(TForwardIterator first, TForwardIterator last)
832 resize(std::distance(first, last),
value());
833 auto iter = begin_array();
834 while (first != last)
845 void assign(std::initializer_list<value> items);
903 owning_object_view as_object() &&;
939 size_type count(const
std::
string& key) const;
941 size_type count(const
std::wstring& key) const;
986 template <typename TForwardIterator>
987 void insert(TForwardIterator first, TForwardIterator last)
996 for ( ; first != last; ++first)
1026 void insert(std::initializer_list<std::pair<std::string, value>> items);
1027 void insert(std::initializer_list<std::pair<std::wstring, value>> items);
1046 std::pair<object_iterator, bool>
emplace(
const std::wstring& key,
value val);
1090 size_type
erase(
const std::string& key);
1091 size_type
erase(
const std::wstring& key);
1142 size_type size() const;
1182 detail::value_storage _data;
1213template <typename TForwardIterator>
1214value array(TForwardIterator first, TForwardIterator last)
1229template <
typename TForwardIterator>
1265 bool empty() const noexcept {
return !_has_value; }
1269 explicit operator bool() const noexcept {
return _has_value; }
1282 enum class purposeful_construction
1285 explicit object_node_handle(purposeful_construction, key_type, mapped_type)
noexcept;
1291 mutable key_type _key;
1292 mutable mapped_type _value;
1312 std::size_t operator()(
const jsonv::value& val)
const noexcept;
Copyright (c) 2014-2020 by Travis Gockel.
Thrown from various value methods when attempting to perform an operation which is not valid for the ...
A node handle used when a value is kind::object to access elements of the object in potentially destr...
object_node_handle & operator=(object_node_handle &&src) noexcept
Release the element this handle owns, if any, and take over the one owned by src, if any.
key_type & key() const
Returns a non-const reference to the key_type member of the element.
mapped_type & mapped() const
Returns a non-const reference to the mapped_type member of the element.
std::string key_type
The key type of the object.
object_node_handle(object_node_handle &&src) noexcept
Take over the element owned by src, if any. src is left empty.
Represents an exact path in some JSON structure.
Represents a single JSON value, which can be any one of a potential kind, each behaving slightly diff...
void assign(TForwardIterator first, TForwardIterator last)
Assign the contents of range [first, last) to this array.
const value & at_path(std::string_view p) const
Get the value specified by the path p.
value(const std::string_view &value)
Create a kind::string with the given value.
void insert(std::initializer_list< std::pair< std::wstring, value > > items)
Insert items into this object.
constexpr value()
Default-construct this to null.
value(std::nullptr_t)=delete
The nullptr overload will fail to compile – use jsonv::null if you want a kind::null.
std::pair< object_iterator, bool > emplace(const std::wstring &key, value val)
Construct an element from key and val and insert it into this object.
array_iterator erase(const_array_iterator first, const_array_iterator last)
Erase the range [first, last) from this array.
value(float value)
Create a kind::decimal with the given value.
value & at_path(const path &p)
Get the value specified by the path p.
const value & at_path(size_type p) const
Get the value specified by the path p.
value & path(const path &p)
Get or create the value specified by the path p.
friend std::ostream & operator<<(std::ostream &stream, const value &val)
Output this value to a stream, through an ostream_encoder.
const_object_iterator end_object() const
Get an iterator to the one past the end of this object.
basic_object_iterator< object_value_type, std::map< std::string, value >::iterator > object_iterator
The object_iterator is applicable when kind is kind::object.
std::pair< object_iterator, bool > try_emplace(const std::wstring &key, value val)
Insert val with the given key into this object, but only if no element with an equivalent key is alre...
value & at_path(std::string_view p)
Get the value specified by the path p.
void assign(size_type count, const value &val)
Assign count elements to this array with val.
object_iterator erase(const_object_iterator position)
Erase the item at the given position.
value & at_path(size_type p)
Get the value specified by the path p.
const value & at_path(const path &p) const
Get the value specified by the path p.
size_type count_path(const path &p) const
Similar to count, but walks the given path p to determine its presence.
bool operator!=(const value &other) const
Compares two JSON values for inequality.
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.
array_view as_array() &
View this instance as an array.
object_iterator end_object()
Get an iterator to the one past the end of this object.
const value & operator[](int idx) const
Get the value in this array at the given idx.
int compare(const value &other) const
Used to build a strict-ordering of JSON values.
object_node_handle extract(const_object_iterator position)
Unlinks the node that contains the element pointed to by position and returns a node handle that owns...
size_type count_path(std::string_view p) const
Similar to count, but walks the given path p to determine its presence.
void push_back(const value &item)
Push item to the back of this array.
const_object_iterator begin_object() const
Get an iterator to the first key-value pair in this object.
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.
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 ...
size_type count_path(size_type p) const
Similar to count, but walks the given path p to determine its presence.
value(const wchar_t *value)
Create a kind::string with the given value.
const_array_iterator begin_array() const
Get an iterator to the beginning of this array.
object_insert_return_type insert(object_node_handle &&handle)
Insert the contents of handle.
std::pair< object_iterator, bool > try_emplace(const std::string &key, value val)
Insert val with the given key into this object, but only if no element with an equivalent key is alre...
object_iterator insert(const_object_iterator hint, object_node_handle &&handle)
If handle is an empty node handle, does nothing and returns end_object.
array_iterator erase(const_array_iterator position)
Erase the item at this array's position.
std::pair< object_iterator, bool > insert_or_assign(const std::wstring &key, value val)
Insert val with the given key into this object if no element with an equivalent key is present; other...
bool operator==(const value &other) const
Compares two JSON values for equality.
array_iterator insert(const_array_iterator position, TForwardIterator first, TForwardIterator last)
Insert the range defined by [first, last) at position in this array.
void push_back(value &&item)
Push item to the back of this array.
void resize(size_type count, const value &val=value())
Resize the length of this array to count items.
value & at(size_type idx)
Get the value in this array at the given idx.
std::pair< object_iterator, bool > emplace(std::string key, value val)
Construct an element from key and val and insert it into this object.
value(const std::wstring &value)
Create a kind::string with the given value.
const value & at(size_type idx) const
Get the value in this array at the given idx.
const_array_iterator end_array() const
Get an iterator to the end of this array.
value(bool value)
Create a kind::boolean with the given value.
basic_array_iterator< value, value > array_iterator
The array_iterator is applicable when kind is kind::array.
~value() noexcept
Destruction will never throw.
value(const value &source)
Copy the contents of source into a new instance.
std::pair< object_iterator, bool > insert_or_assign(const std::string &key, value val)
Insert val with the given key into this object if no element with an equivalent key is present; other...
std::pair< const std::string, value > object_value_type
The type of value stored when kind is kind::object.
size_type erase(const std::wstring &key)
Erase the item with the given key.
const_object_iterator position
The position of the inserted node or node with the duplicate key.
value(const std::string &value)
Create a kind::string with the given value.
void swap(value &other) noexcept
Swap the value this instance represents with other.
object_iterator begin_object()
Get an iterator to the first key-value pair in this object.
value & operator[](int idx)
Get the value in this array at the given idx.
void insert(std::initializer_list< std::pair< std::string, value > > items)
Insert items into this object.
bool inserted
Did the insert operation perform an insert? A value of false indicates there was a key already presen...
object_view as_object() &
View this instance as an object.
bool empty() const noexcept
Is the underlying structure empty?
friend std::string to_string(const value &)
Get a string representation of the given value.
value(const char *value)
Create a kind::string with the given value.
void pop_back()
Pop an item off the back of this array.
object_iterator erase(const_object_iterator first, const_object_iterator last)
Erase the range defined by [first, last).
array_iterator insert(const_array_iterator position, value item)
Insert an item into position on this array.
const_object_view as_object() const &
View this instance as an object.
size_type erase(const std::string &key)
Erase the item with the given key.
value & path(size_type p)
Get or create the value specified by the path p.
object_node_handle extract(const std::wstring &key)
If the container has an element with the given key, unlinks the node that contains that element from ...
array_iterator begin_array()
Get an iterator to the beginning of this array.
array_iterator end_array()
Get an iterator to the end of this array.
value(double value)
Create a kind::decimal with the given value.
void assign(std::initializer_list< value > items)
Assign the given items to this array.
value & path(std::string_view p)
Get or create the value specified by the path p.
value(int64_t value)
Create a kind::integer with the given value.
void reserve(size_type count)
Reserve at least count elements in the array.
const_array_view as_array() const &
View this instance as an array.
Type returned from insert operations when this has kind::object.
Copyright (c) 2014-2020 by Travis Gockel.
#define JSONV_NODISCARD
Warn if the caller discards the result of this function.
#define JSONV_INTEGER_ALTERNATES_LIST(item)
An item list of types to also consider as an integer.
#define JSONV_PUBLIC
This function or class is part of the public API for JSON Voorhees.
@ insert
Insert the correct key/value pair as part of serialization.
kind
Describes the kind of data a value holds.
JSONV_PUBLIC std::string to_string(const ast_node_type &type)
Get what operator<< writes for type as a std::string.
JSONV_PUBLIC value object()
Create an empty object.
JSONV_PUBLIC const value null
An instance with kind::null.
JSONV_PUBLIC value array()
Create an empty array value.
Copyright (c) 2019-2020 by Travis Gockel.
The base type for iterating over array values.
The base iterator type for iterating over object types.
basic_object_iterator(const basic_object_iterator< U, UIterator > &source, typename std::enable_if< std::is_convertible< U *, T * >::value >::type *=0)
This allows assignment from an object_iterator to a const_object_iterator.