1#include <xrpl/beast/core/LexicalCast.h>
2#include <xrpl/beast/utility/instrumentation.h>
3#include <xrpl/json/detail/json_assert.h>
4#include <xrpl/json/json_forwards.h>
5#include <xrpl/json/json_value.h>
6#include <xrpl/json/json_writer.h>
43 length = (value !=
nullptr) ? (
unsigned int)strlen(value) : 0;
45 char* newString =
static_cast<char*
>(malloc(length + 1));
47 memcpy(newString, value, length);
48 newString[length] = 0;
60static ValueAllocator*&
93 : cstr_(allocate == duplicate ?
valueAllocator()->makeMemberName(cstr) : cstr), index_(allocate)
99 other.index_ != noDuplication && other.cstr_ != 0
102 , index_([&]() -> int {
105 return other.
index_ == noDuplication ? noDuplication : duplicate;
112 if ((cstr_ !=
nullptr) && index_ == duplicate)
119 if ((cstr_ !=
nullptr) && (other.
cstr_ !=
nullptr))
120 return strcmp(cstr_, other.
cstr_) < 0;
122 return index_ < other.
index_;
128 if ((cstr_ !=
nullptr) && (other.
cstr_ !=
nullptr))
129 return strcmp(cstr_, other.
cstr_) == 0;
131 return index_ == other.
index_;
149 return index_ == noDuplication;
195 UNREACHABLE(
"Json::Value::Value(ValueType) : invalid type");
274 UNREACHABLE(
"Json::Value::Value(Value const&) : invalid type");
304 UNREACHABLE(
"Json::Value::~Value : invalid type");
318 : value_(other.value_), type_(other.type_), allocated_(other.allocated_)
321 other.allocated_ = 0;
327 Value tmp(std::move(other));
341 int const temp2 = allocated_;
342 allocated_ = other.allocated_;
343 other.allocated_ = temp2;
362 return (i == ui) ? 0 : 1;
413 UNREACHABLE(
"Json::operator<(Value, Value) : invalid type");
461 UNREACHABLE(
"Json::operator==(Value, Value) : invalid type");
500 JSON_ASSERT_MESSAGE(
false,
"Type is not convertible to string");
504 UNREACHABLE(
"Json::Value::asString : invalid type");
530 "Real out of signed integer range");
538 return beast::lexicalCastThrow<int>(str);
543 JSON_ASSERT_MESSAGE(
false,
"Type is not convertible to int");
547 UNREACHABLE(
"Json::Value::asInt : invalid type");
589 auto const temp = beast::lexicalCastThrow<std::int64_t>(str);
592 JSON_ASSERT_MESSAGE(-1 * temp <=
maxUInt,
"String out of unsigned integer range");
595 JSON_ASSERT_MESSAGE(temp <=
maxUInt,
"String out of unsigned integer range");
601 JSON_ASSERT_MESSAGE(
false,
"Type is not convertible to int");
605 UNREACHABLE(
"Json::Value::asAbsInt : invalid type");
622 value_.
int_ >= 0,
"Negative integer can not be converted to unsigned integer");
631 "Real out of unsigned integer range");
639 return beast::lexicalCastThrow<unsigned int>(str);
644 JSON_ASSERT_MESSAGE(
false,
"Type is not convertible to uint");
648 UNREACHABLE(
"Json::Value::asUInt : invalid type");
678 JSON_ASSERT_MESSAGE(
false,
"Type is not convertible to double");
682 UNREACHABLE(
"Json::Value::asDouble : invalid type");
716 UNREACHABLE(
"Json::Value::asBool : invalid type");
766 UNREACHABLE(
"Json::Value::isConvertible : invalid type");
792 return (*itLast).first.index() + 1;
802 UNREACHABLE(
"Json::Value::size : invalid type");
817 auto s = asCString();
818 return (s !=
nullptr) && (s[0] != 0);
821 return !(isArray() || isObject()) || (size() != 0u);
829 "Json::Value::clear : valid type");
858 ObjectValues::value_type
const defaultValue(key,
null);
868 "Json::Value::operator[](UInt) const : valid type");
903 ObjectValues::value_type
const defaultValue(actualKey,
null);
905 Value& value = (*it).second;
912 Value const* value = &((*this)[index]);
913 return value == &
null ? defaultValue : *value;
919 return index <
size();
927 "Json::Value::operator[](const char*) const : valid type");
933 ObjectValues::const_iterator
const it =
value_.
map_->
find(actualKey);
944 return (*
this)[key.
c_str()];
950 return (*
this)[key.
c_str()];
962 return (*
this)[key.
c_str()];
968 return (*
this)[
size()] = value;
974 return (*
this)[
size()] = std::move(value);
980 Value const* value = &((*this)[key]);
981 return value == &
null ? defaultValue : *value;
987 return get(key.
c_str(), defaultValue);
1005 Value old(it->second);
1022 Value const* value = &((*this)[key]);
1023 return value != &
null;
1050 ObjectValues::const_iterator
const itEnd =
value_.
map_->
end();
1052 for (; it != itEnd; ++it)
1134 return writer.
write(*
this);
char * duplicateStringValue(char const *value, unsigned int length=unknown) override
virtual ~DefaultValueAllocator()=default
void releaseMemberName(char *memberName) override
char * makeMemberName(char const *memberName) override
void releaseStringValue(char *value) override
Lightweight wrapper to tag static string.
constexpr char const * c_str() 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 releaseStringValue(char *value)=0
virtual char * duplicateStringValue(char const *value, unsigned int length=unknown)=0
virtual void releaseMemberName(char *memberName)=0
const iterator for object and array value.
Iterator for object and array value.
bool isStaticString() const
char const * c_str() const
bool operator==(CZString const &other) const
bool operator<(CZString const &other) const
const_iterator begin() const
Value & append(Value const &value)
Append value to array at the end.
UInt size() const
Number of values in array or object.
std::string toStyledString() const
const_iterator end() const
bool isObjectOrNull() const
void clear()
Remove all object members and array elements.
char const * asCString() const
union Json::Value::ValueHolder value_
ValueConstIterator const_iterator
UInt asAbsUInt() const
Correct absolute value from int or unsigned int.
Members getMemberNames() const
Return a list of the member names.
std::vector< std::string > Members
Value & operator=(Value const &other)
Value removeMember(char const *key)
Remove and return the named member.
void swap(Value &other) noexcept
Swap values.
bool isValidIndex(UInt index) const
Return true if index < size().
std::string asString() const
Returns the unquoted string value.
static constexpr Int maxInt
bool isNull() const
isNull() tests to see if this field is null.
bool isMember(char const *key) const
Return true if the object has a member named key.
bool isArrayOrNull() const
Value get(UInt index, Value const &defaultValue) const
If the array contains at least index+1 elements, returns the element value, otherwise returns default...
Value(ValueType type=nullValue)
Create a default Value of the given type.
static constexpr Int minInt
std::map< CZString, Value > ObjectValues
Value & resolveReference(char const *key, bool isStatic)
bool isConvertibleTo(ValueType other) const
Value & operator[](UInt index)
Access an array element (zero based index ).
static constexpr UInt maxUInt
Number is a floating point type that can represent a wide range of values.
JSON (JavaScript Object Notation).
std::string to_string(Value const &)
Writes a Json::Value to an std::string.
static struct Json::DummyValueAllocatorInitializer dummyValueAllocatorInitializer
ValueType
Type of the value held by a Value object.
@ stringValue
UTF-8 string value.
@ arrayValue
array value (ordered list)
@ intValue
signed integer value
@ objectValue
object value (collection of name/value pairs).
@ uintValue
unsigned integer value
bool operator<(Value const &, Value const &)
bool operator==(StaticString x, StaticString y)
static ValueAllocator *& valueAllocator()
static int integerCmp(Int i, UInt ui)
DummyValueAllocatorInitializer()