1#include <xrpl/basics/StringUtilities.h>
2#include <xrpl/basics/base_uint.h>
3#include <xrpl/basics/contract.h>
4#include <xrpl/basics/safe_cast.h>
5#include <xrpl/beast/core/LexicalCast.h>
6#include <xrpl/json/json_forwards.h>
7#include <xrpl/json/json_value.h>
8#include <xrpl/protocol/AccountID.h>
9#include <xrpl/protocol/ErrorCodes.h>
10#include <xrpl/protocol/LedgerFormats.h>
11#include <xrpl/protocol/Permissions.h>
12#include <xrpl/protocol/SField.h>
13#include <xrpl/protocol/STAccount.h>
14#include <xrpl/protocol/STAmount.h>
15#include <xrpl/protocol/STArray.h>
16#include <xrpl/protocol/STBitString.h>
17#include <xrpl/protocol/STBlob.h>
18#include <xrpl/protocol/STCurrency.h>
19#include <xrpl/protocol/STInteger.h>
20#include <xrpl/protocol/STIssue.h>
21#include <xrpl/protocol/STNumber.h>
22#include <xrpl/protocol/STParsedJSON.h>
23#include <xrpl/protocol/STPathSet.h>
24#include <xrpl/protocol/STVector256.h>
25#include <xrpl/protocol/STXChainBridge.h>
26#include <xrpl/protocol/TER.h>
27#include <xrpl/protocol/TxFormats.h>
28#include <xrpl/protocol/UintTypes.h>
29#include <xrpl/protocol/detail/STVar.h>
46namespace STParsedJSONDetail {
47template <
typename U,
typename S>
52 Throw<std::runtime_error>(
"Value out of range");
53 return static_cast<U
>(value);
56template <
typename U1,
typename U2>
61 Throw<std::runtime_error>(
"Value out of range");
62 return static_cast<U1
>(value);
72 return object +
"." + field;
146 "Field '" +
object +
"[" +
std::to_string(index) +
"]' must be an object with a single key/object value.");
162 " is not an object. Arrays may only contain objects.");
166template <
class STResult,
class Integer>
182 ret = detail::make_stvar<STResult>(
183 field, safe_cast<typename STResult::value_type>(beast::lexicalCastThrow<Integer>(value.
asString())));
185 else if (value.
isInt())
187 ret = detail::make_stvar<STResult>(field, to_unsigned<typename STResult::value_type>(value.
asInt()));
191 ret = detail::make_stvar<STResult>(field, to_unsigned<typename STResult::value_type>(value.
asUInt()));
195 error =
bad_type(json_name, fieldName);
208template <
class STResult,
class Integer = std::u
int16_t>
226 if (!strValue.
empty() && ((strValue[0] <
'0') || (strValue[0] >
'9')))
228 if (field == sfTransactionType)
230 ret = detail::make_stvar<STResult>(
232 safe_cast<typename STResult::value_type>(
236 name = &sfTransaction;
238 else if (field == sfLedgerEntryType)
240 ret = detail::make_stvar<STResult>(
242 safe_cast<typename STResult::value_type>(
246 name = &sfLedgerEntry;
256 return parseUnsigned<STResult, Integer>(field, json_name, fieldName, name, value, error);
267template <
class STResult,
class Integer = std::u
int32_t>
283 if (field == sfPermissionValue)
287 if (granularPermission)
289 ret = detail::make_stvar<STResult>(field, *granularPermission);
299 ret = detail::make_stvar<STResult>(
301 safe_cast<typename STResult::value_type>(beast::lexicalCastThrow<Integer>(value.
asString())));
305 return parseUnsigned<STResult, Integer>(field, json_name, fieldName, name, value, error);
339 switch (field.fieldType)
350 if (!strValue.
empty() && ((strValue[0] <
'0') || (strValue[0] >
'9')))
352 if (field == sfTransactionResult)
366 error =
bad_type(json_name, fieldName);
372 ret = detail::make_stvar<STUInt8>(field, beast::lexicalCastThrow<std::uint8_t>(strValue));
375 else if (value.
isInt())
377 if (value.
asInt() < minValue || value.
asInt() > maxValue)
383 ret = detail::make_stvar<STUInt8>(field,
static_cast<std::uint8_t>(value.
asInt()));
387 if (value.
asUInt() > maxValue)
397 error =
bad_type(json_name, fieldName);
409 ret = parseUint16<STUInt16>(field, json_name, fieldName, name, value, error);
416 ret = parseUint32<STUInt32>(field, json_name, fieldName, name, value, error);
434 auto [p, ec] =
std::from_chars(str.data(), str.data() + str.size(), val, useBase10 ? 10 : 16);
436 if (ec !=
std::errc() || (p != str.data() + str.size()))
437 Throw<std::invalid_argument>(
"invalid data");
439 ret = detail::make_stvar<STUInt64>(field, val);
441 else if (value.
isInt())
443 ret = detail::make_stvar<STUInt64>(field, to_unsigned<std::uint64_t>(value.
asInt()));
447 ret = detail::make_stvar<STUInt64>(field, safe_cast<std::uint64_t>(value.
asUInt()));
451 error =
bad_type(json_name, fieldName);
466 error =
bad_type(json_name, fieldName);
483 ret = detail::make_stvar<STUInt128>(field, num);
490 error =
bad_type(json_name, fieldName);
507 ret = detail::make_stvar<STUInt160>(field, num);
514 error =
bad_type(json_name, fieldName);
531 ret = detail::make_stvar<STUInt192>(field, num);
538 error =
bad_type(json_name, fieldName);
555 ret = detail::make_stvar<STUInt256>(field, num);
564 ret = detail::make_stvar<STInt32>(field, beast::lexicalCastThrow<std::int32_t>(value.
asString()));
566 else if (value.
isInt())
572 ret = detail::make_stvar<STInt32>(field, value.
asInt());
576 auto const uintValue = value.
asUInt();
582 ret = detail::make_stvar<STInt32>(field,
static_cast<std::int32_t>(uintValue));
586 error =
bad_type(json_name, fieldName);
601 error =
bad_type(json_name, fieldName);
609 ret = detail::make_stvar<STBlob>(field, vBlob->data(), vBlob->size());
613 Throw<std::invalid_argument>(
"invalid data");
664 Throw<std::invalid_argument>(
"invalid data");
667 ret = detail::make_stvar<STVector256>(std::move(tail));
692 if (!value[i].isArrayOrNull())
695 ss << fieldName <<
"[" << i <<
"]";
703 ss << fieldName <<
"[" << i <<
"][" << j <<
"]";
720 bool hasCurrency =
false;
724 if (!account && !currency && !issuer)
733 if (!account.isString())
741 if (!uAccount.
parseHex(account.asString()))
743 auto const a = parseBase58<AccountID>(account.asString());
785 auto const a = parseBase58<AccountID>(issuer.
asString());
795 p.
emplace_back(uAccount, uCurrency, uIssuer, hasCurrency);
800 ret = detail::make_stvar<STPathSet>(std::move(tail));
813 error =
bad_type(json_name, fieldName);
821 if (
AccountID account; account.parseHex(strValue))
822 return detail::make_stvar<STAccount>(field, account);
824 if (
auto result = parseBase58<AccountID>(strValue))
825 return detail::make_stvar<STAccount>(field, *result);
841 ret = detail::make_stvar<STIssue>(
issueFromJson(field, value));
850 case STI_XCHAIN_BRIDGE:
853 ret = detail::make_stvar<STXChainBridge>(
STXChainBridge(field, value));
875 error =
bad_type(json_name, fieldName);
919 switch (field.fieldType)
923 case STI_TRANSACTION:
924 case STI_LEDGERENTRY:
934 auto ret =
parseObject(json_name +
"." + fieldName, value, field, depth + 1, error);
937 data.emplace_back(std::move(*ret));
951 auto array =
parseArray(json_name +
"." + fieldName, value, field, depth + 1, error);
952 if (!array.has_value())
954 data.emplace_back(std::move(*array));
966 auto leaf =
parseLeaf(json_name, fieldName, &inName, value, error);
971 data.emplace_back(std::move(*leaf));
979 data.applyTemplateFromSField(inName);
1016 bool const isObjectOrNull(json[i].isObjectOrNull());
1017 bool const singleKey(isObjectOrNull ? json[i].size() == 1 :
true);
1019 if (!isObjectOrNull || !singleKey)
1029 std::string const objectName(json[i].getMemberNames()[0]);
1039 Json::Value const objectFields(json[i][objectName]);
1042 ss << json_name <<
"." <<
"[" << i <<
"]." << objectName;
1044 auto ret =
parseObject(ss.
str(), objectFields, nameField, depth + 1, error);
1047 std::string errMsg = error[
"error_message"].asString();
1048 error[
"error_message"] =
"Error at '" + ss.
str() +
"'. " + errMsg;
1052 if (ret->getFName().fieldType != STI_OBJECT)
1054 ss <<
"Field type: " << ret->getFName().fieldType <<
" ";
1062 return detail::make_stvar<STArray>(std::move(tail));
1077 using namespace STParsedJSONDetail;
bool isObjectOrNull() const
Members getMemberNames() const
Return a list of the member names.
bool isValidIndex(UInt index) const
Return true if index < size().
std::string asString() const
Returns the unquoted string value.
bool isArrayOrNull() const
std::optional< std::uint32_t > getGranularValue(std::string const &name) const
static Permission const & getInstance()
static SField const & getField(int fieldCode)
std::string const & getName() const
void push_back(STObject const &object)
Json::Value error
On failure, an appropriate set of error values.
STParsedJSONObject()=delete
void push_back(STPath const &e)
void emplace_back(Args &&... args)
void push_back(uint256 const &v)
Integers of any length that is a multiple of 32-bits.
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Json::Value make_error(error_code_i code)
Returns a new json object that reflects the error code.
constexpr std::enable_if_t< std::is_unsigned< U >::value &&std::is_signed< S >::value, U > to_unsigned(S value)
static Json::Value invalid_data(std::string const &object, std::string const &field)
static Json::Value unknown_field(std::string const &object, std::string const &field)
static std::optional< STObject > parseObject(std::string const &json_name, Json::Value const &json, SField const &inName, int depth, Json::Value &error)
static std::string make_name(std::string const &object, std::string const &field)
static int const maxDepth
static Json::Value out_of_range(std::string const &object, std::string const &field)
static Json::Value bad_type(std::string const &object, std::string const &field)
static std::optional< detail::STVar > parseUnsigned(SField const &field, std::string const &json_name, std::string const &fieldName, SField const *name, Json::Value const &value, Json::Value &error)
static Json::Value too_deep(std::string const &object)
static std::optional< detail::STVar > parseUint32(SField const &field, std::string const &json_name, std::string const &fieldName, SField const *name, Json::Value const &value, Json::Value &error)
static Json::Value not_an_array(std::string const &object)
static Json::Value array_expected(std::string const &object, std::string const &field)
static std::optional< detail::STVar > parseArray(std::string const &json_name, Json::Value const &json, SField const &inName, int depth, Json::Value &error)
static std::optional< detail::STVar > parseLeaf(std::string const &json_name, std::string const &fieldName, SField const *name, Json::Value const &value, Json::Value &error)
static std::optional< detail::STVar > parseUint16(SField const &field, std::string const &json_name, std::string const &fieldName, SField const *name, Json::Value const &value, Json::Value &error)
static Json::Value not_an_object(std::string const &object, std::string const &field)
static Json::Value string_expected(std::string const &object, std::string const &field)
static Json::Value singleton_expected(std::string const &object, unsigned int index)
static Json::Value non_object_in_array(std::string const &item, Json::UInt index)
static Json::Value template_mismatch(SField const &sField)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
STCurrency currencyFromJson(SField const &name, Json::Value const &v)
STAmount amountFromJson(SField const &name, Json::Value const &v)
std::optional< Blob > strUnHex(std::size_t strSize, Iterator begin, Iterator end)
Issue issueFromJson(Json::Value const &v)
constexpr TERUnderlyingType TERtoInt(TELcodes v)
STNumber numberFromJson(SField const &field, Json::Value const &value)
std::optional< TER > transCode(std::string const &token)
bool to_currency(Currency &, std::string const &)
Tries to convert a string to a Currency, returns true on success.