1#include <xrpl/json/json_value.h>
3#include <xrpl/basics/Number.h>
4#include <xrpl/beast/core/LexicalCast.h>
5#include <xrpl/beast/utility/instrumentation.h>
6#include <xrpl/json/detail/json_assert.h>
7#include <xrpl/json/json_forwards.h>
8#include <xrpl/json/json_writer.h>
47 length = (value !=
nullptr) ? (
unsigned int)strlen(value) : 0;
49 char* newString =
static_cast<char*
>(malloc(length + 1));
51 memcpy(newString, value, length);
52 newString[length] = 0;
64static ValueAllocator*&
68 return kValueAllocator;
98 ,
index_(static_cast<int>(allocate))
126 if ((
cstr_ !=
nullptr) && (other.
cstr_ !=
nullptr))
135 if ((
cstr_ !=
nullptr) && (other.
cstr_ !=
nullptr))
202 UNREACHABLE(
"json::Value::Value(ValueType) : invalid type");
241 value_.stringVal =
const_cast<char*
>(value.cStr());
281 UNREACHABLE(
"json::Value::Value(Value const&) : invalid type");
305 if (
value_.mapVal !=
nullptr)
311 UNREACHABLE(
"json::Value::~Value : invalid type");
328 other.allocated_ = 0;
334 Value tmp(std::move(other));
350 other.allocated_ = temp2;
369 return (i == ui) ? 0 : 1;
375 if (
auto signum =
static_cast<int>(x.
type_) -
static_cast<int>(y.
type_))
420 UNREACHABLE(
"json::operator<(Value, Value) : invalid type");
468 UNREACHABLE(
"json::operator==(Value, Value) : invalid type");
491 return (
value_.stringVal !=
nullptr) ?
value_.stringVal :
"";
494 return value_.boolVal ?
"true" :
"false";
507 JSON_ASSERT_MESSAGE(
false,
"Type is not convertible to string");
511 UNREACHABLE(
"json::Value::asString : invalid type");
531 value_.uintVal < (
unsigned)
kMaxInt,
"integer out of signed integer range");
537 "Real out of signed integer range");
541 return value_.boolVal ? 1 : 0;
544 char const*
const str{(
value_.stringVal !=
nullptr) ?
value_.stringVal :
""};
550 JSON_ASSERT_MESSAGE(
false,
"Type is not convertible to int");
554 UNREACHABLE(
"json::Value::asInt : invalid type");
584 -1 *
value_.realVal <=
kMaxUInt,
"Real out of unsigned integer range");
587 JSON_ASSERT_MESSAGE(
value_.realVal <=
kMaxUInt,
"Real out of unsigned integer range");
592 return value_.boolVal ? 1 : 0;
595 char const*
const str{(
value_.stringVal !=
nullptr) ?
value_.stringVal :
""};
599 JSON_ASSERT_MESSAGE(-1 * temp <=
kMaxUInt,
"String out of unsigned integer range");
602 JSON_ASSERT_MESSAGE(temp <=
kMaxUInt,
"String out of unsigned integer range");
608 JSON_ASSERT_MESSAGE(
false,
"Type is not convertible to int");
612 UNREACHABLE(
"json::Value::asAbsInt : invalid type");
629 value_.intVal >= 0,
"Negative integer can not be converted to unsigned integer");
638 "Real out of unsigned integer range");
642 return value_.boolVal ? 1 : 0;
645 char const*
const str{(
value_.stringVal !=
nullptr) ?
value_.stringVal :
""};
651 JSON_ASSERT_MESSAGE(
false,
"Type is not convertible to uint");
655 UNREACHABLE(
"json::Value::asUInt : invalid type");
680 return value_.boolVal ? 1.0 : 0.0;
685 JSON_ASSERT_MESSAGE(
false,
"Type is not convertible to double");
689 UNREACHABLE(
"json::Value::asDouble : invalid type");
706 return value_.intVal != 0;
709 return value_.realVal != 0.0;
715 return (
value_.stringVal !=
nullptr) &&
value_.stringVal[0] != 0;
719 return !
value_.mapVal->empty();
723 UNREACHABLE(
"json::Value::asBool : invalid type");
767 ((
value_.stringVal ==
nullptr) ||
value_.stringVal[0] == 0));
779 UNREACHABLE(
"json::Value::isConvertible : invalid type");
801 if (!
value_.mapVal->empty())
803 ObjectValues::const_iterator itLast =
value_.mapVal->end();
805 return (*itLast).first.index() + 1;
815 UNREACHABLE(
"json::Value::size : invalid type");
831 return (s !=
nullptr) && (s[0] != 0);
842 "json::Value::clear : valid type");
861 "json::Value::operator[](UInt) : valid type");
867 ObjectValues::iterator it =
value_.mapVal->lower_bound(key);
869 if (it !=
value_.mapVal->end() && (*it).first == key)
872 ObjectValues::value_type
const defaultValue(key,
kNull);
873 it =
value_.mapVal->insert(it, defaultValue);
882 "json::Value::operator[](UInt) const : valid type");
888 ObjectValues::const_iterator
const it =
value_.mapVal->find(key);
890 if (it ==
value_.mapVal->end())
907 "json::Value::resolveReference : valid type");
916 ObjectValues::iterator it =
value_.mapVal->lower_bound(actualKey);
918 if (it !=
value_.mapVal->end() && (*it).first == actualKey)
921 ObjectValues::value_type
const defaultValue(actualKey,
kNull);
922 it =
value_.mapVal->insert(it, defaultValue);
923 Value& value = (*it).second;
930 Value const* value = &((*this)[index]);
931 return value == &
kNull ? defaultValue : *value;
937 return index <
size();
945 "json::Value::operator[](const char*) const : valid type");
951 ObjectValues::const_iterator
const it =
value_.mapVal->find(actualKey);
953 if (it ==
value_.mapVal->end())
962 return (*
this)[key.
c_str()];
968 return (*
this)[key.
c_str()];
980 return (*
this)[key.
cStr()];
986 return (*
this)[
size()] = value;
992 return (*
this)[
size()] = std::move(value);
998 Value const* value = &((*this)[key]);
999 return value == &
kNull ? defaultValue : *value;
1005 return get(key.
c_str(), defaultValue);
1013 "json::Value::removeMember : valid type");
1019 ObjectValues::iterator
const it =
value_.mapVal->find(actualKey);
1021 if (it ==
value_.mapVal->end())
1024 Value old(it->second);
1025 value_.mapVal->erase(it);
1041 Value const* value = &((*this)[key]);
1042 return value != &
kNull;
1062 "json::Value::getMemberNames : valid type");
1069 ObjectValues::const_iterator it =
value_.mapVal->begin();
1070 ObjectValues::const_iterator
const itEnd =
value_.mapVal->end();
1072 for (; it != itEnd; ++it)
1154 return writer.
write(*
this);
1164 if (
value_.mapVal !=
nullptr)
1182 if (
value_.mapVal !=
nullptr)
1200 if (
value_.mapVal !=
nullptr)
1217 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)
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()