11 #ifndef __JSONV_VALUE_HPP_INCLUDED__ 12 #define __JSONV_VALUE_HPP_INCLUDED__ 20 #include <initializer_list> 27 #include <type_traits> 35 class object_node_handle;
69 enum class kind : unsigned char
93 public std::logic_error
96 explicit kind_error(
const std::string& description);
134 typedef std::size_t size_type;
135 typedef std::ptrdiff_t difference_type;
138 template <
typename T,
typename TArrayView>
140 public std::iterator<std::random_access_iterator_tag, T>
153 template <
typename U,
typename UArrayView>
155 typename std::enable_if<std::is_convertible<U*, T*>::value>::type* = 0
157 _owner(source._owner),
158 _index(source._index)
187 template <
typename U,
typename UArrayView>
190 return _owner == other._owner && _index == other._index;
193 template <
typename U,
typename UArrayView>
196 return !operator==(other);
201 return _owner->operator[](_index);
204 T* operator->()
const 206 return &_owner->operator[](_index);
237 return difference_type(_index) - difference_type(other._index);
242 return _index < rhs._index;
247 return _index <= rhs._index;
252 return _index > rhs._index;
257 return _index >= rhs._index;
260 T& operator[](size_type n)
const 262 return _owner->operator[](_index + n);
265 template <
typename U,
typename UArrayView>
291 template <
typename T,
typename TIterator>
293 public std::iterator<std::bidirectional_iterator_tag, T>
305 template <
typename U,
typename UIterator>
307 typename std::enable_if<std::is_convertible<U*, T*>::value>::type* = 0
314 _impl = source._impl;
318 template <
typename U,
typename UIterator>
351 template <
typename U,
typename UIterator>
354 return _impl == other._impl;
357 template <
typename U,
typename UIterator>
360 return _impl != other._impl;
363 T& operator *()
const 368 T* operator ->()
const 376 template <
typename UIterator>
437 value(std::nullptr_t) =
delete;
452 value(
const char* value);
456 value(
const std::wstring& value);
463 value(
const wchar_t* value);
466 value(int64_t value);
477 #define JSONV_VALUE_INTEGER_ALTERNATIVE_CTOR_PROTO_GENERATOR(type_) \ 488 value& operator=(
const value& source);
491 value(value&& source) noexcept;
497 value& operator=(value&& source) noexcept;
503 const std::string& as_string()
const;
506 bool is_string()
const;
519 std::wstring as_wstring()
const;
525 int64_t as_integer()
const;
528 bool is_integer()
const;
535 double as_decimal()
const;
538 bool is_decimal()
const;
544 bool as_boolean()
const;
547 bool is_boolean()
const;
550 bool is_array()
const;
553 bool is_object()
const;
556 bool is_null()
const;
575 value& at_path(
const path& p);
577 value& at_path(size_type p);
578 const value& at_path(
const path& p)
const;
580 const value& at_path(size_type p)
const;
589 size_type count_path(
const path& p)
const;
591 size_type count_path(size_type p)
const;
608 value& path(size_type p);
611 void swap(value& other) noexcept;
628 bool operator==(
const value& other)
const;
632 bool operator!=(
const value& other)
const;
649 int compare(
const value& other)
const;
651 bool operator< (
const value& other)
const;
652 bool operator> (
const value& other)
const;
653 bool operator<=(
const value& other)
const;
654 bool operator>=(
const value& other)
const;
657 friend std::ostream&
operator<<(std::ostream& stream,
const value& val);
660 friend std::string
to_string(
const value&);
666 array_iterator begin_array();
667 const_array_iterator begin_array()
const;
673 array_iterator end_array();
674 const_array_iterator end_array()
const;
680 array_view as_array() &;
681 const_array_view as_array()
const &;
682 owning_array_view as_array() &&;
689 value& operator[](size_type idx);
690 const value& operator[](size_type idx)
const;
691 inline value& operator[](
int idx) {
return operator[](size_type(idx)); }
692 inline const value& operator[](
int idx)
const {
return operator[](size_type(idx)); }
699 value& at(size_type idx);
700 const value& at(size_type idx)
const;
706 void push_back(value item);
719 void push_front(value item);
732 array_iterator
insert(const_array_iterator position, value item);
738 template <
typename TForwardIterator>
739 array_iterator
insert(const_array_iterator position, TForwardIterator first, TForwardIterator last)
741 difference_type orig_offset = std::distance(const_array_iterator(begin_array()), position);
743 for (difference_type offset = orig_offset ; first != last; ++first, ++offset)
744 insert(begin_array() + offset, *first);
745 return begin_array() + orig_offset;
752 void assign(size_type count,
const value& val);
758 template <
typename TForwardIterator>
759 void assign(TForwardIterator first, TForwardIterator last)
761 resize(std::distance(first, last), value());
762 auto iter = begin_array();
763 while (first != last)
775 void assign(std::initializer_list<value> items);
782 void resize(size_type count,
const value& val = value());
788 array_iterator erase(const_array_iterator position);
794 array_iterator erase(const_array_iterator first, const_array_iterator last);
800 object_iterator begin_object();
801 const_object_iterator begin_object()
const;
807 object_iterator end_object();
808 const_object_iterator end_object()
const;
814 object_view as_object() &;
815 const_object_view as_object()
const &;
816 owning_object_view as_object() &&;
822 value& operator[](
const std::string& key);
823 value& operator[](std::string&& key);
824 value& operator[](
const std::wstring& key);
831 value& at(
const std::string& key);
832 value& at(
const std::wstring& key);
833 const value& at(
const std::string& key)
const;
834 const value& at(
const std::wstring& key)
const;
840 size_type count(
const std::string& key)
const;
841 size_type count(
const std::wstring& key)
const;
847 object_iterator find(
const std::string& key);
848 object_iterator find(
const std::wstring& key);
849 const_object_iterator find(
const std::string& key)
const;
850 const_object_iterator find(
const std::wstring& key)
const;
857 std::pair<object_iterator, bool>
insert(std::pair<std::string, value> pair);
858 std::pair<object_iterator, bool>
insert(std::pair<std::wstring, value> pair);
859 object_iterator
insert(const_object_iterator hint, std::pair<std::string, value> pair);
860 object_iterator
insert(const_object_iterator hint, std::pair<std::wstring, value> pair);
865 template <
typename TForwardIterator>
866 void insert(TForwardIterator first, TForwardIterator last)
868 for ( ; first != last; ++first)
895 void insert(std::initializer_list<std::pair<std::string, value>> items);
896 void insert(std::initializer_list<std::pair<std::wstring, value>> items);
904 size_type erase(
const std::string& key);
905 size_type erase(
const std::wstring& key);
910 object_iterator erase(const_object_iterator position);
915 object_iterator erase(const_object_iterator first, const_object_iterator last);
942 bool empty()
const noexcept;
953 size_type size()
const;
967 value
map(
const std::function<value (
const value&)>& func)
const&;
982 value
map(
const std::function<value (value)>& func) &&;
1022 template <
typename TForwardIterator>
1023 value array(TForwardIterator first, TForwardIterator last)
1025 value arr = array();
1034 JSONV_PUBLIC value object(std::initializer_list<std::pair<std::string, value>> source);
1035 JSONV_PUBLIC value object(std::initializer_list<std::pair<std::wstring, value>> source);
1038 template <
typename TForwardIterator>
1039 value object(TForwardIterator first, TForwardIterator last)
1041 value obj = object();
1070 bool empty() const noexcept {
return !_has_value; }
1073 explicit operator bool() const noexcept {
return _has_value; }
1086 enum class purposeful_construction
1116 std::size_t operator()(
const jsonv::value& val)
const noexcept;
JSONV_PUBLIC std::ostream & operator<<(std::ostream &, const kind &)
Print out the name of the kind.
basic_object_iterator< object_value_type, std::map< std::string, value >::iterator > object_iterator
The object_iterator is applicable when kind is kind::object.
#define JSONV_INTEGER_ALTERNATES_LIST(item)
An item list of types to also consider as an integer.
std::string key_type
The key type of the object.
Thrown from various value methods when attempting to perform an operation which is not valid for the ...
bool empty() const noexcept
void assign(TForwardIterator first, TForwardIterator last)
Assign the contents of range [first, last) to this array.
array_iterator insert(const_array_iterator position, value item)
Insert an item into position on this array.
The base iterator type for iterating over object types.
Copyright (c) 2014-2019 by Travis Gockel.
The base type for iterating over array values.
A form of basic_view that owns the object it is iterating over.
bool inserted
Did the insert operation perform an insert? A value of false indicates there was a key already presen...
JSONV_PUBLIC const value null
An instance with kind::null.
Represents an exact path in some JSON structure.
void assign(size_type count, const value &val)
Assign count elements to this array with val.
void insert(TForwardIterator first, TForwardIterator last)
Insert range defined by [first, last) into this object.
JSONV_PUBLIC value map(const std::function< value(const value &)> &func, const value &input)
Run a function over the values in the input.
JSONV_PUBLIC value array()
Create an empty array value.
jsonv::kind kind() const
Get this value's kind.
Insert the correct key/value pair as part of serialization.
kind
Describes the kind of data a value holds.
JSONV_PUBLIC void swap(value &a, value &b) noexcept
Swap the values a and b.
JSONV_PUBLIC std::string to_string(const value &)
Get a string representation of the given value.
Type returned from insert operations when this has kind::object.
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...
Pulls in an implementation of string_view.
int compare(const value &a, const value &b, const TCompareTraits &traits)
Compare the values a and b using the comparison traits.
JSONV_PUBLIC value object()
Create an empty object.
Copyright (c) 2014 by Travis Gockel.
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.
basic_array_iterator< value, value > array_iterator
The array_iterator is applicable when kind is kind::array.
const_object_iterator position
The position of the inserted node or node with the duplicate key.
A view template used for array and object views of a value.
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.
constexpr value()
Default-construct this to null.
T extract(const value &from, const formats &fmts)
Extract a C++ value from from using the provided fmts.
std::pair< const std::string, value > object_value_type
The type of value stored when kind is kind::object.
#define JSONV_PUBLIC
This function or class is part of the public API for JsonVoorhees.
JSONV_STRING_VIEW_TYPE string_view
A non-owning reference to a string.
Represents a single JSON value, which can be any one of a potential kind, each behaving slightly diff...
array_iterator insert(const_array_iterator position, TForwardIterator first, TForwardIterator last)
Insert the range defined by [first, last) at position in this array.
A node handle used when a value is kind::object to access elements of the object in potentially destr...