20#include <xrpl/basics/StringUtilities.h>
21#include <xrpl/basics/base_uint.h>
22#include <xrpl/basics/contract.h>
23#include <xrpl/basics/safe_cast.h>
24#include <xrpl/beast/core/LexicalCast.h>
25#include <xrpl/json/json_forwards.h>
26#include <xrpl/json/json_value.h>
27#include <xrpl/protocol/AccountID.h>
28#include <xrpl/protocol/ErrorCodes.h>
29#include <xrpl/protocol/LedgerFormats.h>
30#include <xrpl/protocol/Permissions.h>
31#include <xrpl/protocol/SField.h>
32#include <xrpl/protocol/STAccount.h>
33#include <xrpl/protocol/STAmount.h>
34#include <xrpl/protocol/STArray.h>
35#include <xrpl/protocol/STBitString.h>
36#include <xrpl/protocol/STBlob.h>
37#include <xrpl/protocol/STCurrency.h>
38#include <xrpl/protocol/STInteger.h>
39#include <xrpl/protocol/STIssue.h>
40#include <xrpl/protocol/STNumber.h>
41#include <xrpl/protocol/STParsedJSON.h>
42#include <xrpl/protocol/STPathSet.h>
43#include <xrpl/protocol/STVector256.h>
44#include <xrpl/protocol/STXChainBridge.h>
45#include <xrpl/protocol/TER.h>
46#include <xrpl/protocol/TxFormats.h>
47#include <xrpl/protocol/UintTypes.h>
48#include <xrpl/protocol/detail/STVar.h>
65namespace STParsedJSONDetail {
66template <
typename U,
typename S>
72 Throw<std::runtime_error>(
"Value out of range");
73 return static_cast<U
>(value);
76template <
typename U1,
typename U2>
82 Throw<std::runtime_error>(
"Value out of range");
83 return static_cast<U1
>(value);
92 return object +
"." + field;
100 "Field '" +
make_name(
object, field) +
"' is not a JSON object.");
121 "Field '" +
make_name(
object, field) +
"' is unknown.");
129 "Field '" +
make_name(
object, field) +
"' is out of range.");
137 "Field '" +
make_name(
object, field) +
"' has bad type.");
145 "Field '" +
make_name(
object, field) +
"' has invalid data.");
159 "Field '" +
make_name(
object, field) +
"' must be a JSON array.");
167 "Field '" +
make_name(
object, field) +
"' must be a string.");
175 "Field '" +
object +
"' exceeds nesting depth limit.");
184 "]' must be an object with a single key/object value.");
192 "Object '" + sField.
getName() +
193 "' contents did not meet requirements for that type.");
202 " is not an object. Arrays may only contain objects.");
205template <
class STResult,
class Integer>
221 ret = detail::make_stvar<STResult>(
223 safe_cast<typename STResult::value_type>(
224 beast::lexicalCastThrow<Integer>(value.
asString())));
226 else if (value.
isInt())
228 ret = detail::make_stvar<STResult>(
230 to_unsigned<typename STResult::value_type>(value.
asInt()));
234 ret = detail::make_stvar<STResult>(
236 to_unsigned<typename STResult::value_type>(value.
asUInt()));
240 error =
bad_type(json_name, fieldName);
253template <
class STResult,
class Integer = std::u
int16_t>
271 if (!strValue.
empty() &&
272 ((strValue[0] <
'0') || (strValue[0] >
'9')))
274 if (field == sfTransactionType)
276 ret = detail::make_stvar<STResult>(
278 safe_cast<typename STResult::value_type>(
279 static_cast<Integer
>(
284 name = &sfTransaction;
286 else if (field == sfLedgerEntryType)
288 ret = detail::make_stvar<STResult>(
290 safe_cast<typename STResult::value_type>(
291 static_cast<Integer
>(
296 name = &sfLedgerEntry;
306 return parseUnsigned<STResult, Integer>(
307 field, json_name, fieldName, name, value, error);
318template <
class STResult,
class Integer = std::u
int32_t>
334 if (field == sfPermissionValue)
337 auto const granularPermission =
339 if (granularPermission)
341 ret = detail::make_stvar<STResult>(
342 field, *granularPermission);
348 ret = detail::make_stvar<STResult>(
355 ret = detail::make_stvar<STResult>(
357 safe_cast<typename STResult::value_type>(
358 beast::lexicalCastThrow<Integer>(value.
asString())));
362 return parseUnsigned<STResult, Integer>(
363 field, json_name, fieldName, name, value, error);
394 switch (field.fieldType)
399 constexpr auto minValue =
401 constexpr auto maxValue =
407 if (!strValue.
empty() &&
408 ((strValue[0] <
'0') || (strValue[0] >
'9')))
410 if (field == sfTransactionResult)
414 if (!ter ||
TERtoInt(*ter) < minValue ||
421 ret = detail::make_stvar<STUInt8>(
427 error =
bad_type(json_name, fieldName);
433 ret = detail::make_stvar<STUInt8>(
435 beast::lexicalCastThrow<std::uint8_t>(strValue));
438 else if (value.
isInt())
440 if (value.
asInt() < minValue || value.
asInt() > maxValue)
446 ret = detail::make_stvar<STUInt8>(
451 if (value.
asUInt() > maxValue)
457 ret = detail::make_stvar<STUInt8>(
462 error =
bad_type(json_name, fieldName);
474 ret = parseUint16<STUInt16>(
475 field, json_name, fieldName, name, value, error);
482 ret = parseUint32<STUInt32>(
483 field, json_name, fieldName, name, value, error);
498 bool const useBase10 =
504 str.data() + str.size(),
506 useBase10 ? 10 : 16);
508 if (ec !=
std::errc() || (p != str.data() + str.size()))
509 Throw<std::invalid_argument>(
"invalid data");
511 ret = detail::make_stvar<STUInt64>(field, val);
513 else if (value.
isInt())
515 ret = detail::make_stvar<STUInt64>(
516 field, to_unsigned<std::uint64_t>(value.
asInt()));
520 ret = detail::make_stvar<STUInt64>(
521 field, safe_cast<std::uint64_t>(value.
asUInt()));
525 error =
bad_type(json_name, fieldName);
540 error =
bad_type(json_name, fieldName);
557 ret = detail::make_stvar<STUInt128>(field, num);
564 error =
bad_type(json_name, fieldName);
581 ret = detail::make_stvar<STUInt192>(field, num);
588 error =
bad_type(json_name, fieldName);
605 ret = detail::make_stvar<STUInt160>(field, num);
612 error =
bad_type(json_name, fieldName);
629 ret = detail::make_stvar<STUInt256>(field, num);
636 error =
bad_type(json_name, fieldName);
644 ret = detail::make_stvar<STBlob>(
645 field, vBlob->data(), vBlob->size());
649 Throw<std::invalid_argument>(
"invalid data");
702 Throw<std::invalid_argument>(
"invalid data");
705 ret = detail::make_stvar<STVector256>(std::move(tail));
730 if (!value[i].isArrayOrNull())
733 ss << fieldName <<
"[" << i <<
"]";
741 ss << fieldName <<
"[" << i <<
"][" << j <<
"]";
743 json_name +
"." + ss.
str());
759 bool hasCurrency =
false;
766 if (!account.isString())
775 if (!uAccount.
parseHex(account.asString()))
778 parseBase58<AccountID>(account.asString());
825 parseBase58<AccountID>(issuer.
asString());
837 uAccount, uCurrency, uIssuer, hasCurrency);
842 ret = detail::make_stvar<STPathSet>(std::move(tail));
855 error =
bad_type(json_name, fieldName);
863 if (
AccountID account; account.parseHex(strValue))
864 return detail::make_stvar<STAccount>(field, account);
866 if (
auto result = parseBase58<AccountID>(strValue))
867 return detail::make_stvar<STAccount>(field, *result);
883 ret = detail::make_stvar<STIssue>(
issueFromJson(field, value));
892 case STI_XCHAIN_BRIDGE:
895 ret = detail::make_stvar<STXChainBridge>(
908 ret = detail::make_stvar<STCurrency>(
919 error =
bad_type(json_name, fieldName);
973 switch (field.fieldType)
977 case STI_TRANSACTION:
978 case STI_LEDGERENTRY:
989 json_name +
"." + fieldName,
996 data.emplace_back(std::move(*ret));
1011 json_name +
"." + fieldName,
1016 if (!array.has_value())
1018 data.emplace_back(std::move(*array));
1031 parseLeaf(json_name, fieldName, &inName, value, error);
1036 data.emplace_back(std::move(*leaf));
1044 data.applyTemplateFromSField(inName);
1086 bool const isObjectOrNull(json[i].isObjectOrNull());
1087 bool const singleKey(isObjectOrNull ? json[i].size() == 1 :
true);
1089 if (!isObjectOrNull || !singleKey)
1099 std::string const objectName(json[i].getMemberNames()[0]);
1109 Json::Value const objectFields(json[i][objectName]);
1112 ss << json_name <<
"."
1113 <<
"[" << i <<
"]." << objectName;
1116 ss.
str(), objectFields, nameField, depth + 1, error);
1119 std::string errMsg = error[
"error_message"].asString();
1120 error[
"error_message"] =
1121 "Error at '" + ss.
str() +
"'. " + errMsg;
1125 if (ret->getFName().fieldType != STI_OBJECT)
1127 ss <<
"Field type: " << ret->getFName().fieldType <<
" ";
1135 return detail::make_stvar<STArray>(std::move(tail));
1152 using namespace STParsedJSONDetail;
1162 using namespace STParsedJSONDetail;
1168 auto p =
dynamic_cast<STArray*
>(&arr->get());
1172 array = std::move(*p);
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()
std::string const & getName() const
static SField const & getField(int fieldCode)
void push_back(STObject const &object)
std::optional< STArray > array
The STArray if the parse was successful.
Json::Value error
On failure, an appropriate set of error values.
STParsedJSONArray()=delete
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.
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 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 Json::Value not_an_object(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 bad_type(std::string const &object, std::string const &field)
constexpr std::enable_if_t< std::is_unsigned< U >::value &&std::is_signed< S >::value, U > to_unsigned(S value)
static std::optional< detail::STVar > parseArray(std::string const &json_name, Json::Value const &json, SField const &inName, int depth, Json::Value &error)
static Json::Value array_expected(std::string const &object, std::string const &field)
static Json::Value singleton_expected(std::string const &object, unsigned int index)
static Json::Value not_an_array(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 invalid_data(std::string const &object, std::string const &field)
static Json::Value non_object_in_array(std::string const &item, Json::UInt index)
static std::optional< STObject > parseObject(std::string const &json_name, Json::Value const &json, SField const &inName, int depth, Json::Value &error)
static Json::Value too_deep(std::string const &object)
static Json::Value unknown_field(std::string const &object, std::string const &field)
static Json::Value template_mismatch(SField const &sField)
static Json::Value out_of_range(std::string const &object, std::string const &field)
static Json::Value string_expected(std::string const &object, std::string const &field)
static int const maxDepth
static std::string make_name(std::string const &object, std::string const &field)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
std::optional< Blob > strUnHex(std::size_t strSize, Iterator begin, Iterator end)
STCurrency currencyFromJson(SField const &name, Json::Value const &v)
STAmount amountFromJson(SField const &name, Json::Value const &v)
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.