1#include <xrpl/json/json_value.h>
3#include <xrpl/basics/Number.h>
4#include <xrpl/basics/contract.h>
5#include <xrpl/beast/core/LexicalCast.h>
6#include <xrpl/beast/utility/instrumentation.h>
7#include <xrpl/json/detail/json_assert.h>
8#include <xrpl/json/json_errors.h>
9#include <xrpl/json/json_forwards.h>
10#include <xrpl/json/json_writer.h>
50 length = (value !=
nullptr) ? (
unsigned int)strlen(value) : 0;
53 char* newString =
static_cast<char*
>(malloc(length + 1));
55 memcpy(newString, value, length);
56 newString[length] = 0;
71static ValueAllocator*&
75 return kValueAllocator;
105 ,
index_(static_cast<int>(allocate))
112 other.
cstr_ != nullptr
137 if ((
cstr_ !=
nullptr) && (other.
cstr_ !=
nullptr))
146 if ((
cstr_ !=
nullptr) && (other.
cstr_ !=
nullptr))
203 value_.stringVal =
nullptr;
217 UNREACHABLE(
"json::Value::Value(ValueType) : invalid type");
255 value.data(),
static_cast<unsigned int>(value.length()));
261 value_.stringVal =
const_cast<char*
>(value.cStr());
289 value_.stringVal =
nullptr;
301 UNREACHABLE(
"json::Value::Value(Value const&) : invalid type");
330 UNREACHABLE(
"json::Value::~Value : invalid type");
347 other.allocated_ = 0;
353 Value tmp(std::move(other));
369 other.allocated_ = temp2;
388 return (i == ui) ? 0 : 1;
394 if (
auto signum =
static_cast<int>(x.
type_) -
static_cast<int>(y.
type_))
439 UNREACHABLE(
"json::operator<(Value, Value) : invalid type");
487 UNREACHABLE(
"json::operator==(Value, Value) : invalid type");
510 return (
value_.stringVal !=
nullptr) ?
value_.stringVal :
"";
513 return value_.boolVal ?
"true" :
"false";
526 JSON_ASSERT_MESSAGE(
false,
"Type is not convertible to string");
530 UNREACHABLE(
"json::Value::asString : invalid type");
550 value_.uintVal < (
unsigned)
kMaxInt,
"integer out of signed integer range");
556 "Real out of signed integer range");
560 return value_.boolVal ? 1 : 0;
563 char const*
const str{(
value_.stringVal !=
nullptr) ?
value_.stringVal :
""};
569 JSON_ASSERT_MESSAGE(
false,
"Type is not convertible to int");
573 UNREACHABLE(
"json::Value::asInt : invalid type");
603 -1 *
value_.realVal <=
kMaxUInt,
"Real out of unsigned integer range");
606 JSON_ASSERT_MESSAGE(
value_.realVal <=
kMaxUInt,
"Real out of unsigned integer range");
611 return value_.boolVal ? 1 : 0;
614 char const*
const str{(
value_.stringVal !=
nullptr) ?
value_.stringVal :
""};
618 JSON_ASSERT_MESSAGE(-1 * temp <=
kMaxUInt,
"String out of unsigned integer range");
621 JSON_ASSERT_MESSAGE(temp <=
kMaxUInt,
"String out of unsigned integer range");
627 JSON_ASSERT_MESSAGE(
false,
"Type is not convertible to int");
631 UNREACHABLE(
"json::Value::asAbsInt : invalid type");
648 value_.intVal >= 0,
"Negative integer can not be converted to unsigned integer");
657 "Real out of unsigned integer range");
661 return value_.boolVal ? 1 : 0;
664 char const*
const str{(
value_.stringVal !=
nullptr) ?
value_.stringVal :
""};
670 JSON_ASSERT_MESSAGE(
false,
"Type is not convertible to uint");
674 UNREACHABLE(
"json::Value::asUInt : invalid type");
699 return value_.boolVal ? 1.0 : 0.0;
704 JSON_ASSERT_MESSAGE(
false,
"Type is not convertible to double");
708 UNREACHABLE(
"json::Value::asDouble : invalid type");
724 return value_.intVal != 0;
727 return value_.uintVal != 0;
730 return value_.realVal != 0.0;
736 return (
value_.stringVal !=
nullptr) &&
value_.stringVal[0] != 0;
740 return !
value_.mapVal->empty();
744 UNREACHABLE(
"json::Value::asBool : invalid type");
788 ((
value_.stringVal ==
nullptr) ||
value_.stringVal[0] == 0));
800 UNREACHABLE(
"json::Value::isConvertible : invalid type");
824 if (!
value_.mapVal->empty())
826 auto itLast =
value_.mapVal->end();
828 return (*itLast).first.index() + 1;
838 UNREACHABLE(
"json::Value::size : invalid type");
854 return (s !=
nullptr) && (s[0] != 0);
865 "json::Value::clear : valid type");
884 "json::Value::operator[](UInt) : valid type");
890 auto it =
value_.mapVal->lower_bound(key);
892 if (it !=
value_.mapVal->end() && (*it).first == key)
895 ObjectValues::value_type
const defaultValue(key,
kNull);
896 it =
value_.mapVal->insert(it, defaultValue);
905 "json::Value::operator[](UInt) const : valid type");
911 auto const it =
value_.mapVal->find(key);
913 if (it ==
value_.mapVal->end())
930 "json::Value::resolveReference : valid type");
939 auto it =
value_.mapVal->lower_bound(actualKey);
941 if (it !=
value_.mapVal->end() && (*it).first == actualKey)
944 ObjectValues::value_type
const defaultValue(actualKey,
kNull);
945 it =
value_.mapVal->insert(it, defaultValue);
946 Value& value = (*it).second;
953 Value const* value = &(*this)[index];
954 return value == &
kNull ? defaultValue : *value;
960 return index <
size();
968 "json::Value::operator[](const char*) const : valid type");
974 auto const it =
value_.mapVal->find(actualKey);
976 if (it ==
value_.mapVal->end())
985 return (*
this)[key.
c_str()];
991 return (*
this)[key.
c_str()];
1003 return (*
this)[key.
cStr()];
1009 return (*
this)[
size()] = value;
1015 return (*
this)[
size()] = std::move(value);
1021 Value const* value = &(*this)[key];
1022 return value == &
kNull ? defaultValue : *value;
1028 return get(key.
c_str(), defaultValue);
1036 "json::Value::removeMember : valid type");
1042 auto const it =
value_.mapVal->find(actualKey);
1044 if (it ==
value_.mapVal->end())
1047 Value old(it->second);
1048 value_.mapVal->erase(it);
1064 Value const* value = &(*this)[key];
1065 return value != &
kNull;
1085 "json::Value::getMemberNames : valid type");
1092 auto it =
value_.mapVal->begin();
1093 auto const itEnd =
value_.mapVal->end();
1095 for (; it != itEnd; ++it)
1177 return writer.
write(*
this);
1187 if (
value_.mapVal !=
nullptr)
1205 if (
value_.mapVal !=
nullptr)
1223 if (
value_.mapVal !=
nullptr)
1240 if (
value_.mapVal !=
nullptr)
void releaseMemberName(char *memberName) override
~DefaultValueAllocator() override=default
char * duplicateStringValue(char const *value, unsigned int length=kUnknown) override
void releaseStringValue(char *value) override
char * makeMemberName(char const *memberName) override
Lightweight wrapper to tag static string.
constexpr char const * cStr() const
Writes a Value in JSON format in a human friendly way.
std::string write(Value const &root) override
Serialize a Value in JSON format.
Experimental do not use: Allocator to customize member name and string value memory management done b...
virtual void releaseMemberName(char *memberName)=0
virtual void releaseStringValue(char *value)=0
virtual char * duplicateStringValue(char const *value, unsigned int length=kUnknown)=0
static constexpr auto kUnknown
bool isStaticString() const
bool operator<(CZString const &other) const
char const * cStr() const
bool operator==(CZString const &other) const
static constexpr Int kMaxInt
const_iterator begin() const
Value removeMember(char const *key)
Remove and return the named member.
bool isNull() const
isNull() tests to see if this field is null.
Value get(UInt index, Value const &defaultValue) const
If the array contains at least index+1 elements, returns the element value, otherwise returns default...
ValueConstIterator const_iterator
std::vector< std::string > Members
union json::Value::ValueHolder value_
bool isValidIndex(UInt index) const
Return true if index < size().
Value & resolveReference(char const *key, bool isStatic)
std::string toStyledString() const
UInt asAbsUInt() const
Correct absolute value from int or unsigned int.
Value & append(Value const &value)
Append value to array at the end.
UInt size() const
Number of values in array or object.
Members getMemberNames() const
Return a list of the member names.
static constexpr Int kMinInt
bool isArrayOrNull() const
const_iterator end() const
Value & operator=(Value const &other)
bool isConvertibleTo(ValueType other) const
std::string asString() const
Returns the unquoted string value.
void swap(Value &other) noexcept
Swap values.
static constexpr UInt kMaxUInt
Value & operator[](UInt index)
Access an array element (zero based index ).
Value(ValueType type=ValueType::Null)
Create a default Value of the given type.
char const * asCString() const
std::map< CZString, Value > ObjectValues
bool isObjectOrNull() const
bool isMember(char const *key) const
Return true if the object has a member named key.
void clear()
Remove all object members and array elements.
Number is a floating point type that can represent a wide range of values.
T emplace_back(T... args)
constexpr Out lexicalCastThrow(In in)
Convert from one type to another, throw on error.
JSON (JavaScript Object Notation).
static ValueAllocator *& valueAllocator()
std::string to_string(Value const &)
Writes a json::Value to an std::string.
bool operator==(StaticString x, StaticString y)
ValueType
Type of the value held by a Value object.
@ UInt
unsigned integer value
@ Int
signed integer value
@ String
UTF-8 string value.
@ Array
array value (ordered list)
@ Object
object value (collection of name/value pairs).
static int integerCmp(Int i, UInt ui)
bool operator<(Value const &, Value const &)
static struct json::DummyValueAllocatorInitializer gDummyValueAllocatorInitializer
DummyValueAllocatorInitializer()