20#include <xrpl/basics/StringUtilities.h>
21#include <xrpl/basics/contract.h>
22#include <xrpl/basics/safe_cast.h>
23#include <xrpl/beast/core/LexicalCast.h>
24#include <xrpl/beast/utility/instrumentation.h>
25#include <xrpl/protocol/ErrorCodes.h>
26#include <xrpl/protocol/LedgerFormats.h>
27#include <xrpl/protocol/SField.h>
28#include <xrpl/protocol/STAccount.h>
29#include <xrpl/protocol/STAmount.h>
30#include <xrpl/protocol/STArray.h>
31#include <xrpl/protocol/STBitString.h>
32#include <xrpl/protocol/STBlob.h>
33#include <xrpl/protocol/STInteger.h>
34#include <xrpl/protocol/STIssue.h>
35#include <xrpl/protocol/STParsedJSON.h>
36#include <xrpl/protocol/STPathSet.h>
37#include <xrpl/protocol/STVector256.h>
38#include <xrpl/protocol/STXChainBridge.h>
39#include <xrpl/protocol/TER.h>
40#include <xrpl/protocol/TxFormats.h>
41#include <xrpl/protocol/UintTypes.h>
42#include <xrpl/protocol/XChainAttestations.h>
43#include <xrpl/protocol/detail/STVar.h>
50namespace STParsedJSONDetail {
51template <
typename U,
typename S>
57 Throw<std::runtime_error>(
"Value out of range");
58 return static_cast<U
>(value);
61template <
typename U1,
typename U2>
67 Throw<std::runtime_error>(
"Value out of range");
68 return static_cast<U1
>(value);
77 return object +
"." + field;
85 "Field '" +
make_name(
object, field) +
"' is not a JSON object.");
106 "Field '" +
make_name(
object, field) +
"' is unknown.");
114 "Field '" +
make_name(
object, field) +
"' is out of range.");
122 "Field '" +
make_name(
object, field) +
"' has bad type.");
130 "Field '" +
make_name(
object, field) +
"' has invalid data.");
144 "Field '" +
make_name(
object, field) +
"' must be a JSON array.");
152 "Field '" +
make_name(
object, field) +
"' must be a string.");
160 "Field '" +
object +
"' exceeds nesting depth limit.");
169 "]' must be an object with a single key/object value.");
177 "Object '" + sField.
getName() +
178 "' contents did not meet requirements for that type.");
187 " is not an object. Arrays may only contain objects.");
210 switch (field.fieldType)
215 constexpr auto minValue =
217 constexpr auto maxValue =
223 if (!strValue.
empty() &&
224 ((strValue[0] <
'0') || (strValue[0] >
'9')))
226 if (field == sfTransactionResult)
230 if (!ter ||
TERtoInt(*ter) < minValue ||
237 ret = detail::make_stvar<STUInt8>(
243 error =
bad_type(json_name, fieldName);
249 ret = detail::make_stvar<STUInt8>(
251 beast::lexicalCastThrow<std::uint8_t>(strValue));
254 else if (value.
isInt())
256 if (value.
asInt() < minValue || value.
asInt() > maxValue)
262 ret = detail::make_stvar<STUInt8>(
267 if (value.
asUInt() > maxValue)
273 ret = detail::make_stvar<STUInt8>(
278 error =
bad_type(json_name, fieldName);
296 if (!strValue.
empty() &&
297 ((strValue[0] <
'0') || (strValue[0] >
'9')))
299 if (field == sfTransactionType)
301 ret = detail::make_stvar<STUInt16>(
308 name = &sfTransaction;
310 else if (field == sfLedgerEntryType)
312 ret = detail::make_stvar<STUInt16>(
319 name = &sfLedgerEntry;
329 ret = detail::make_stvar<STUInt16>(
331 beast::lexicalCastThrow<std::uint16_t>(strValue));
334 else if (value.
isInt())
336 ret = detail::make_stvar<STUInt16>(
337 field, to_unsigned<std::uint16_t>(value.
asInt()));
341 ret = detail::make_stvar<STUInt16>(
342 field, to_unsigned<std::uint16_t>(value.
asUInt()));
346 error =
bad_type(json_name, fieldName);
363 ret = detail::make_stvar<STUInt32>(
365 beast::lexicalCastThrow<std::uint32_t>(
368 else if (value.
isInt())
370 ret = detail::make_stvar<STUInt32>(
371 field, to_unsigned<std::uint32_t>(value.
asInt()));
375 ret = detail::make_stvar<STUInt32>(
376 field, safe_cast<std::uint32_t>(value.
asUInt()));
380 error =
bad_type(json_name, fieldName);
401 bool const useBase10 =
407 str.data() + str.size(),
409 useBase10 ? 10 : 16);
411 if (ec !=
std::errc() || (p != str.data() + str.size()))
412 Throw<std::invalid_argument>(
"invalid data");
414 ret = detail::make_stvar<STUInt64>(field, val);
416 else if (value.
isInt())
418 ret = detail::make_stvar<STUInt64>(
419 field, to_unsigned<std::uint64_t>(value.
asInt()));
423 ret = detail::make_stvar<STUInt64>(
424 field, safe_cast<std::uint64_t>(value.
asUInt()));
428 error =
bad_type(json_name, fieldName);
443 error =
bad_type(json_name, fieldName);
460 ret = detail::make_stvar<STUInt128>(field, num);
467 error =
bad_type(json_name, fieldName);
484 ret = detail::make_stvar<STUInt192>(field, num);
491 error =
bad_type(json_name, fieldName);
508 ret = detail::make_stvar<STUInt160>(field, num);
515 error =
bad_type(json_name, fieldName);
532 ret = detail::make_stvar<STUInt256>(field, num);
539 error =
bad_type(json_name, fieldName);
547 ret = detail::make_stvar<STBlob>(
548 field, vBlob->data(), vBlob->size());
552 Throw<std::invalid_argument>(
"invalid data");
591 Throw<std::invalid_argument>(
"invalid data");
594 ret = detail::make_stvar<STVector256>(std::move(tail));
619 if (!value[i].isArrayOrNull())
622 ss << fieldName <<
"[" << i <<
"]";
630 ss << fieldName <<
"[" << i <<
"][" << j <<
"]";
632 json_name +
"." + ss.
str());
648 bool hasCurrency =
false;
655 if (!account.isString())
664 if (!uAccount.
parseHex(account.asString()))
667 parseBase58<AccountID>(account.asString());
714 parseBase58<AccountID>(issuer.
asString());
726 uAccount, uCurrency, uIssuer, hasCurrency);
731 ret = detail::make_stvar<STPathSet>(std::move(tail));
744 error =
bad_type(json_name, fieldName);
752 if (
AccountID account; account.parseHex(strValue))
753 return detail::make_stvar<STAccount>(field, account);
755 if (
auto result = parseBase58<AccountID>(strValue))
756 return detail::make_stvar<STAccount>(field, *result);
772 ret = detail::make_stvar<STIssue>(
issueFromJson(field, value));
781 case STI_XCHAIN_BRIDGE:
784 ret = detail::make_stvar<STXChainBridge>(
797 ret = detail::make_stvar<STCurrency>(
808 error =
bad_type(json_name, fieldName);
862 switch (field.fieldType)
866 case STI_TRANSACTION:
867 case STI_LEDGERENTRY:
878 json_name +
"." + fieldName,
885 data.emplace_back(std::move(*ret));
900 json_name +
"." + fieldName,
905 if (!array.has_value())
907 data.emplace_back(std::move(*array));
920 parseLeaf(json_name, fieldName, &inName, value, error);
925 data.emplace_back(std::move(*leaf));
933 data.applyTemplateFromSField(inName);
975 bool const isObjectOrNull(json[i].isObjectOrNull());
976 bool const singleKey(isObjectOrNull ? json[i].size() == 1 :
true);
978 if (!isObjectOrNull || !singleKey)
988 std::string const objectName(json[i].getMemberNames()[0]);
998 Json::Value const objectFields(json[i][objectName]);
1001 ss << json_name <<
"." <<
"[" << i <<
"]." << objectName;
1004 ss.
str(), objectFields, nameField, depth + 1, error);
1007 std::string errMsg = error[
"error_message"].asString();
1008 error[
"error_message"] =
1009 "Error at '" + ss.
str() +
"'. " + errMsg;
1010 return std::nullopt;
1013 if (ret->getFName().fieldType != STI_OBJECT)
1015 ss <<
"Field type: " << ret->getFName().fieldType <<
" ";
1017 return std::nullopt;
1023 return detail::make_stvar<STArray>(std::move(tail));
1028 return std::nullopt;
1040 using namespace STParsedJSONDetail;
1050 using namespace STParsedJSONDetail;
1056 auto p =
dynamic_cast<STArray*
>(&arr->get());
1060 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::string const & getName() const
static const SField & 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 const int maxDepth
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 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)
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.