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.");
225 switch (field.fieldType)
230 constexpr auto minValue =
232 constexpr auto maxValue =
238 if (!strValue.
empty() &&
239 ((strValue[0] <
'0') || (strValue[0] >
'9')))
241 if (field == sfTransactionResult)
245 if (!ter ||
TERtoInt(*ter) < minValue ||
252 ret = detail::make_stvar<STUInt8>(
258 error =
bad_type(json_name, fieldName);
264 ret = detail::make_stvar<STUInt8>(
266 beast::lexicalCastThrow<std::uint8_t>(strValue));
269 else if (value.
isInt())
271 if (value.
asInt() < minValue || value.
asInt() > maxValue)
277 ret = detail::make_stvar<STUInt8>(
282 if (value.
asUInt() > maxValue)
288 ret = detail::make_stvar<STUInt8>(
293 error =
bad_type(json_name, fieldName);
311 if (!strValue.
empty() &&
312 ((strValue[0] <
'0') || (strValue[0] >
'9')))
314 if (field == sfTransactionType)
316 ret = detail::make_stvar<STUInt16>(
323 name = &sfTransaction;
325 else if (field == sfLedgerEntryType)
327 ret = detail::make_stvar<STUInt16>(
334 name = &sfLedgerEntry;
344 ret = detail::make_stvar<STUInt16>(
346 beast::lexicalCastThrow<std::uint16_t>(strValue));
349 else if (value.
isInt())
351 ret = detail::make_stvar<STUInt16>(
352 field, to_unsigned<std::uint16_t>(value.
asInt()));
356 ret = detail::make_stvar<STUInt16>(
357 field, to_unsigned<std::uint16_t>(value.
asUInt()));
361 error =
bad_type(json_name, fieldName);
378 if (field == sfPermissionValue)
381 auto const granularPermission =
384 if (granularPermission)
386 ret = detail::make_stvar<STUInt32>(
387 field, *granularPermission);
394 ret = detail::make_stvar<STUInt32>(
402 ret = detail::make_stvar<STUInt32>(
404 beast::lexicalCastThrow<std::uint32_t>(
408 else if (value.
isInt())
410 ret = detail::make_stvar<STUInt32>(
411 field, to_unsigned<std::uint32_t>(value.
asInt()));
415 ret = detail::make_stvar<STUInt32>(
416 field, safe_cast<std::uint32_t>(value.
asUInt()));
420 error =
bad_type(json_name, fieldName);
441 bool const useBase10 =
447 str.data() + str.size(),
449 useBase10 ? 10 : 16);
451 if (ec !=
std::errc() || (p != str.data() + str.size()))
452 Throw<std::invalid_argument>(
"invalid data");
454 ret = detail::make_stvar<STUInt64>(field, val);
456 else if (value.
isInt())
458 ret = detail::make_stvar<STUInt64>(
459 field, to_unsigned<std::uint64_t>(value.
asInt()));
463 ret = detail::make_stvar<STUInt64>(
464 field, safe_cast<std::uint64_t>(value.
asUInt()));
468 error =
bad_type(json_name, fieldName);
483 error =
bad_type(json_name, fieldName);
500 ret = detail::make_stvar<STUInt128>(field, num);
507 error =
bad_type(json_name, fieldName);
524 ret = detail::make_stvar<STUInt192>(field, num);
531 error =
bad_type(json_name, fieldName);
548 ret = detail::make_stvar<STUInt160>(field, num);
555 error =
bad_type(json_name, fieldName);
572 ret = detail::make_stvar<STUInt256>(field, num);
579 error =
bad_type(json_name, fieldName);
587 ret = detail::make_stvar<STBlob>(
588 field, vBlob->data(), vBlob->size());
592 Throw<std::invalid_argument>(
"invalid data");
645 Throw<std::invalid_argument>(
"invalid data");
648 ret = detail::make_stvar<STVector256>(std::move(tail));
673 if (!value[i].isArrayOrNull())
676 ss << fieldName <<
"[" << i <<
"]";
684 ss << fieldName <<
"[" << i <<
"][" << j <<
"]";
686 json_name +
"." + ss.
str());
702 bool hasCurrency =
false;
709 if (!account.isString())
718 if (!uAccount.
parseHex(account.asString()))
721 parseBase58<AccountID>(account.asString());
768 parseBase58<AccountID>(issuer.
asString());
780 uAccount, uCurrency, uIssuer, hasCurrency);
785 ret = detail::make_stvar<STPathSet>(std::move(tail));
798 error =
bad_type(json_name, fieldName);
806 if (
AccountID account; account.parseHex(strValue))
807 return detail::make_stvar<STAccount>(field, account);
809 if (
auto result = parseBase58<AccountID>(strValue))
810 return detail::make_stvar<STAccount>(field, *result);
826 ret = detail::make_stvar<STIssue>(
issueFromJson(field, value));
835 case STI_XCHAIN_BRIDGE:
838 ret = detail::make_stvar<STXChainBridge>(
851 ret = detail::make_stvar<STCurrency>(
862 error =
bad_type(json_name, fieldName);
916 switch (field.fieldType)
920 case STI_TRANSACTION:
921 case STI_LEDGERENTRY:
932 json_name +
"." + fieldName,
939 data.emplace_back(std::move(*ret));
954 json_name +
"." + fieldName,
959 if (!array.has_value())
961 data.emplace_back(std::move(*array));
974 parseLeaf(json_name, fieldName, &inName, value, error);
979 data.emplace_back(std::move(*leaf));
987 data.applyTemplateFromSField(inName);
1000 return std::nullopt;
1014 return std::nullopt;
1020 return std::nullopt;
1029 bool const isObjectOrNull(json[i].isObjectOrNull());
1030 bool const singleKey(isObjectOrNull ? json[i].size() == 1 :
true);
1032 if (!isObjectOrNull || !singleKey)
1036 return std::nullopt;
1042 std::string const objectName(json[i].getMemberNames()[0]);
1049 return std::nullopt;
1052 Json::Value const objectFields(json[i][objectName]);
1055 ss << json_name <<
"."
1056 <<
"[" << i <<
"]." << objectName;
1059 ss.
str(), objectFields, nameField, depth + 1, error);
1062 std::string errMsg = error[
"error_message"].asString();
1063 error[
"error_message"] =
1064 "Error at '" + ss.
str() +
"'. " + errMsg;
1065 return std::nullopt;
1068 if (ret->getFName().fieldType != STI_OBJECT)
1070 ss <<
"Field type: " << ret->getFName().fieldType <<
" ";
1072 return std::nullopt;
1078 return detail::make_stvar<STArray>(std::move(tail));
1083 return std::nullopt;
1095 using namespace STParsedJSONDetail;
1105 using namespace STParsedJSONDetail;
1111 auto p =
dynamic_cast<STArray*
>(&arr->get());
1115 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 > 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 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 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.