|
A library for working with phylogenetic and population genetic data.
v0.32.0
|
|
Go to the documentation of this file. 1 #ifndef GENESIS_UTILS_FORMATS_JSON_DOCUMENT_H_
2 #define GENESIS_UTILS_FORMATS_JSON_DOCUMENT_H_
182 ValueUnion() =
default;
191 array = create<ArrayType>();
195 object = create<ObjectType>();
199 string = create<StringType>(
"");
219 throw std::runtime_error(
"Invalid Json Value Type." );
339 template<
typename T,
typename std::enable_if<
340 not (std::is_same<T, int>::value) and
341 std::is_same<T, NumberSignedType>::value,
363 template<
typename T,
typename std::enable_if<
364 not (std::is_same<T, int>::value) and
365 std::is_same<T, NumberUnsignedType>::value,
368 : type_(
ValueType::kNumberUnsigned), value_(val)
386 if( not std::isfinite(val) ) {
388 value_ = ValueUnion();
402 std::initializer_list<JsonDocument> init,
403 bool type_deduction =
true,
413 value_.array = create<ArrayType>( n, val );
418 class CompatibleArrayTypeT,
typename std::enable_if<
419 not std::is_same<CompatibleArrayTypeT, typename JsonDocument::iterator>::value and
420 not std::is_same<CompatibleArrayTypeT, typename JsonDocument::const_iterator>::value and
423 not std::is_same<CompatibleArrayTypeT, typename ArrayType::iterator>::value and
424 not std::is_same<CompatibleArrayTypeT, typename ArrayType::const_iterator>::value and
425 std::is_constructible<JsonDocument, typename CompatibleArrayTypeT::value_type>::value,
int>
::type = 0
432 value_.array = create<ArrayType>(
begin(val),
end(val));
442 template<
class CompatibleObjectType,
443 typename std::enable_if<
444 std::is_constructible<
445 typename ObjectType::key_type,
typename CompatibleObjectType::key_type
447 std::is_constructible<
448 JsonDocument,
typename CompatibleObjectType::mapped_type
458 value_.object = create<ObjectType>(
begin(val),
end(val) );
468 template<
class CompatibleStringType,
typename std::enable_if<
469 std::is_constructible<StringType, CompatibleStringType>::value,
int>
::type = 0
509 std::initializer_list<JsonDocument> init = std::initializer_list<JsonDocument>()
518 std::initializer_list<JsonDocument> init = std::initializer_list<JsonDocument>()
527 std::string
const& init
539 result.value_ = value;
550 result.value_ = value;
561 result.value_ = value;
572 result.value_ = value;
728 return value_.number_float;
730 return value_.number_signed;
732 return value_.number_unsigned;
734 throw std::domain_error(
"Cannot use get_number<T>() with " +
type_name() +
"." );
748 JsonDocument const&
at(
typename ObjectType::key_type
const& key )
const;
766 iterator find(
typename JsonDocument::ObjectType::key_type key );
862 void push_back(
typename ObjectType::value_type
const& val );
864 template<
class... Args>
869 throw std::domain_error(
"Cannot use emplace_back() with " +
type_name());
880 value_.array->emplace_back( std::forward<Args>(args)... );
931 return not (lhs == rhs);
941 return not v.is_null();
951 return not v.is_null();
977 return not (rhs < lhs);
988 return not (lhs <= rhs);
999 return not (lhs < rhs);
1031 template<
typename T,
typename... Args>
1032 static T* create( Args&& ... args )
1035 std::allocator<T> alloc;
1036 auto deleter = [&]( T * obj )
1038 alloc.deallocate( obj, 1 );
1040 std::unique_ptr<T, decltype(deleter)> obj( alloc.allocate(1), deleter );
1041 alloc.construct( obj.get(), std::forward<Args>(args)... );
1042 assert( obj !=
nullptr );
1043 return obj.release();
1050 template<
typename T>
1051 static void destroy( T* ptr )
1053 std::allocator<T> alloc;
1055 alloc.deallocate(ptr, 1);
1060 void assert_invariant()
const
1074 ValueUnion value_ = {};
1081 #endif // include guard
JsonDocument(size_t n, JsonDocument const &val)
Construct an array with n many copies of val.
friend bool operator<(const_reference lhs, const_reference rhs)
Compare less than.
static JsonDocument boolean(BooleanType value)
Explicitly create a boolean.
JsonDocument(const NumberUnsignedType val)
Create an unsigned integer number (explicit).
bool operator>(Optional< T > const &x, Optional< U > const &y)
const_iterator cbegin() const
Return a const iterator to the first element.
size_type count(typename ObjectType::key_type key) const
Return the number of occurrences of a key in a JSON object.
friend bool operator==(const_reference lhs, const_reference rhs)
Compare equal.
ObjectType & get_object()
JsonIterator< JsonDocument const > const_iterator
static JsonDocument string(std::string const &init)
Explicitly create a string.
JsonDocument(typename StringType::value_type const *v)
Create a string, explicitly, using a char pointer.
NumberFloatType & get_number_float()
bool operator>=(Optional< T > const &x, Optional< U > const &y)
friend bool operator<=(const_reference lhs, const_reference rhs)
Compare less than or equal.
JsonDocument(std::nullptr_t=nullptr)
Create a null object.
iterator end()
Return an iterator to one past the last element.
iterator find(typename JsonDocument::ObjectType::key_type key)
Find an element in a JSON object.
JsonIterator< JsonDocument > iterator
friend bool operator!=(const_reference lhs, const_reference rhs)
Compare not equal.
Store a Json value of any kind.
std::ptrdiff_t difference_type
static JsonDocument number_unsigned(NumberUnsignedType value)
Explicitly create an unsigned number.
std::string type_name() const
JsonDocument(const NumberSignedType val)
Create a signed integer number (explicit).
std::vector< JsonDocument > ArrayType
bool is_null() const
Return true iff the JSON value is null.
ValueType type() const
Return the type of the JSON value.
iterator begin()
Return an iterator to the first element.
friend bool operator==(const_reference v, std::nullptr_t)
Compare equal.
bool is_number() const
Return true iff the JSON value is a number, i.e., a float or signed or unsigned integer.
NumberUnsignedType & get_number_unsigned()
bool is_number_unsigned() const
Return true iff the JSON value is an unsigned integer number.
~JsonDocument()
Destructor.
void emplace_back(Args &&... args)
JsonDocument(ValueType type)
Create an empty value of the given type.
friend bool operator>=(const_reference lhs, const_reference rhs)
Compare greater than or equal.
JsonDocument const & const_reference
bool operator!=(Color const &lhs, Color const &rhs)
static JsonDocument number_float(NumberFloatType value)
Explicitly create a float number.
JsonDocument(const NumberFloatType val)
Create a floating-point number (explicit).
void push_back(JsonDocument &&val)
Add a Json value to an array.
bool is_boolean() const
Return true iff the JSON value is a boolean.
JsonDocument & at(size_t index)
bool is_number_float() const
Return true iff the JSON value is a float number.
bool is_array() const
Return true iff the JSON value is an array.
static JsonDocument array(std::initializer_list< JsonDocument > init=std::initializer_list< JsonDocument >())
Explicitly create an array from an initializer list.
JsonDocument & operator[](size_t index)
JsonDocument(StringType const &v)
Create a string, explicitly.
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
bool is_number_signed() const
Return true iff the JSON value is a signed integer number.
friend bool operator!=(std::nullptr_t, const_reference v)
Compare not equal.
void clear()
Clears the content of a JSON value and resets it to the default value as if JsonDocument(ValueType) w...
friend bool operator==(std::nullptr_t, const_reference v)
Compare equal.
bool operator<=(Optional< T > const &x, Optional< U > const &y)
bool is_string() const
Return true iff the JSON value is a string.
JsonDocument const * const_pointer
const_iterator cend() const
Return a const iterator to one past the last element.
bool operator<(Optional< T > const &x, Optional< U > const &y)
JsonDocument(ArrayType const &v)
Create an array, explicitly.
std::uint64_t NumberUnsignedType
bool operator==(Color const &lhs, Color const &rhs)
JsonDocument & operator=(JsonDocument other)
Copy assignment.
static JsonDocument number_signed(NumberSignedType value)
Explicitly create a signed number.
StringType & get_string()
friend bool operator!=(const_reference v, std::nullptr_t)
Compare not equal.
BooleanType & get_boolean()
bool is_number_integer() const
Return true iff the JSON value is a signed or unsigned integer number.
bool is_primitive() const
Return true iff the JSON type is primitive (string, number, boolean, or null).
std::map< std::string, JsonDocument > ObjectType
JsonDocument(const CompatibleStringType &val)
Create a string (implicit).
JsonDocument(const CompatibleArrayTypeT &val)
NumberSignedType & get_number_signed()
JsonDocument(CompatibleObjectType const &val)
Create an object (implicit).
static JsonDocument object(std::initializer_list< JsonDocument > init=std::initializer_list< JsonDocument >())
Explicitly create an object from an initializer list.
std::int64_t NumberSignedType
friend bool operator>(const_reference lhs, const_reference rhs)
Compare greater than.
bool is_object() const
Return true iff the JSON value is an object.
JsonDocument(int const val)
Create a signed integer number from an enum type (explicit).
JsonDocument(BooleanType v)
Create a boolean, explicitly.
JsonDocument(ObjectType const &v)
Create an object, explicitly.
bool is_structured() const
Return true iff the JSON type is structured (array or object).
Template for a random access iterator for the JsonDocument class.