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/SField.h>
31#include <xrpl/protocol/STAccount.h>
32#include <xrpl/protocol/STAmount.h>
33#include <xrpl/protocol/STArray.h>
34#include <xrpl/protocol/STBitString.h>
35#include <xrpl/protocol/STBlob.h>
36#include <xrpl/protocol/STCurrency.h>
37#include <xrpl/protocol/STInteger.h>
38#include <xrpl/protocol/STIssue.h>
39#include <xrpl/protocol/STParsedJSON.h>
40#include <xrpl/protocol/STPathSet.h>
41#include <xrpl/protocol/STVector256.h>
42#include <xrpl/protocol/STXChainBridge.h>
43#include <xrpl/protocol/TER.h>
44#include <xrpl/protocol/TxFormats.h>
45#include <xrpl/protocol/UintTypes.h>
46#include <xrpl/protocol/detail/STVar.h>
63namespace STParsedJSONDetail {
64template <
typename U,
typename S>
70 Throw<std::runtime_error>(
"Value out of range");
71 return static_cast<U
>(value);
74template <
typename U1,
typename U2>
80 Throw<std::runtime_error>(
"Value out of range");
81 return static_cast<U1
>(value);
90 return object +
"." + field;
98 "Field '" +
make_name(
object, field) +
"' is not a JSON object.");
119 "Field '" +
make_name(
object, field) +
"' is unknown.");
127 "Field '" +
make_name(
object, field) +
"' is out of range.");
135 "Field '" +
make_name(
object, field) +
"' has bad type.");
143 "Field '" +
make_name(
object, field) +
"' has invalid data.");
157 "Field '" +
make_name(
object, field) +
"' must be a JSON array.");
165 "Field '" +
make_name(
object, field) +
"' must be a string.");
173 "Field '" +
object +
"' exceeds nesting depth limit.");
182 "]' must be an object with a single key/object value.");
190 "Object '" + sField.
getName() +
191 "' contents did not meet requirements for that type.");
200 " is not an object. Arrays may only contain objects.");
223 switch (field.fieldType)
228 constexpr auto minValue =
230 constexpr auto maxValue =
236 if (!strValue.
empty() &&
237 ((strValue[0] <
'0') || (strValue[0] >
'9')))
239 if (field == sfTransactionResult)
243 if (!ter ||
TERtoInt(*ter) < minValue ||
250 ret = detail::make_stvar<STUInt8>(
256 error =
bad_type(json_name, fieldName);
262 ret = detail::make_stvar<STUInt8>(
264 beast::lexicalCastThrow<std::uint8_t>(strValue));
267 else if (value.
isInt())
269 if (value.
asInt() < minValue || value.
asInt() > maxValue)
275 ret = detail::make_stvar<STUInt8>(
280 if (value.
asUInt() > maxValue)
286 ret = detail::make_stvar<STUInt8>(
291 error =
bad_type(json_name, fieldName);
309 if (!strValue.
empty() &&
310 ((strValue[0] <
'0') || (strValue[0] >
'9')))
312 if (field == sfTransactionType)
314 ret = detail::make_stvar<STUInt16>(
321 name = &sfTransaction;
323 else if (field == sfLedgerEntryType)
325 ret = detail::make_stvar<STUInt16>(
332 name = &sfLedgerEntry;
342 ret = detail::make_stvar<STUInt16>(
344 beast::lexicalCastThrow<std::uint16_t>(strValue));
347 else if (value.
isInt())
349 ret = detail::make_stvar<STUInt16>(
350 field, to_unsigned<std::uint16_t>(value.
asInt()));
354 ret = detail::make_stvar<STUInt16>(
355 field, to_unsigned<std::uint16_t>(value.
asUInt()));
359 error =
bad_type(json_name, fieldName);
376 ret = detail::make_stvar<STUInt32>(
378 beast::lexicalCastThrow<std::uint32_t>(
381 else if (value.
isInt())
383 ret = detail::make_stvar<STUInt32>(
384 field, to_unsigned<std::uint32_t>(value.
asInt()));
388 ret = detail::make_stvar<STUInt32>(
389 field, safe_cast<std::uint32_t>(value.
asUInt()));
393 error =
bad_type(json_name, fieldName);
414 bool const useBase10 =
420 str.data() + str.size(),
422 useBase10 ? 10 : 16);
424 if (ec !=
std::errc() || (p != str.data() + str.size()))
425 Throw<std::invalid_argument>(
"invalid data");
427 ret = detail::make_stvar<STUInt64>(field, val);
429 else if (value.
isInt())
431 ret = detail::make_stvar<STUInt64>(
432 field, to_unsigned<std::uint64_t>(value.
asInt()));
436 ret = detail::make_stvar<STUInt64>(
437 field, safe_cast<std::uint64_t>(value.
asUInt()));
441 error =
bad_type(json_name, fieldName);
456 error =
bad_type(json_name, fieldName);
473 ret = detail::make_stvar<STUInt128>(field, num);
480 error =
bad_type(json_name, fieldName);
497 ret = detail::make_stvar<STUInt192>(field, num);
504 error =
bad_type(json_name, fieldName);
521 ret = detail::make_stvar<STUInt160>(field, num);
528 error =
bad_type(json_name, fieldName);
545 ret = detail::make_stvar<STUInt256>(field, num);
552 error =
bad_type(json_name, fieldName);
560 ret = detail::make_stvar<STBlob>(
561 field, vBlob->data(), vBlob->size());
565 Throw<std::invalid_argument>(
"invalid data");
604 Throw<std::invalid_argument>(
"invalid data");
607 ret = detail::make_stvar<STVector256>(std::move(tail));
632 if (!value[i].isArrayOrNull())
635 ss << fieldName <<
"[" << i <<
"]";
643 ss << fieldName <<
"[" << i <<
"][" << j <<
"]";
645 json_name +
"." + ss.
str());
661 bool hasCurrency =
false;
668 if (!account.isString())
677 if (!uAccount.
parseHex(account.asString()))
680 parseBase58<AccountID>(account.asString());
727 parseBase58<AccountID>(issuer.
asString());
739 uAccount, uCurrency, uIssuer, hasCurrency);
744 ret = detail::make_stvar<STPathSet>(std::move(tail));
757 error =
bad_type(json_name, fieldName);
765 if (
AccountID account; account.parseHex(strValue))
766 return detail::make_stvar<STAccount>(field, account);
768 if (
auto result = parseBase58<AccountID>(strValue))
769 return detail::make_stvar<STAccount>(field, *result);
785 ret = detail::make_stvar<STIssue>(
issueFromJson(field, value));
794 case STI_XCHAIN_BRIDGE:
797 ret = detail::make_stvar<STXChainBridge>(
810 ret = detail::make_stvar<STCurrency>(
821 error =
bad_type(json_name, fieldName);
875 switch (field.fieldType)
879 case STI_TRANSACTION:
880 case STI_LEDGERENTRY:
891 json_name +
"." + fieldName,
898 data.emplace_back(std::move(*ret));
913 json_name +
"." + fieldName,
918 if (!array.has_value())
920 data.emplace_back(std::move(*array));
933 parseLeaf(json_name, fieldName, &inName, value, error);
938 data.emplace_back(std::move(*leaf));
946 data.applyTemplateFromSField(inName);
988 bool const isObjectOrNull(json[i].isObjectOrNull());
989 bool const singleKey(isObjectOrNull ? json[i].size() == 1 :
true);
991 if (!isObjectOrNull || !singleKey)
1001 std::string const objectName(json[i].getMemberNames()[0]);
1008 return std::nullopt;
1011 Json::Value const objectFields(json[i][objectName]);
1014 ss << json_name <<
"." <<
"[" << i <<
"]." << objectName;
1017 ss.
str(), objectFields, nameField, depth + 1, error);
1020 std::string errMsg = error[
"error_message"].asString();
1021 error[
"error_message"] =
1022 "Error at '" + ss.
str() +
"'. " + errMsg;
1023 return std::nullopt;
1026 if (ret->getFName().fieldType != STI_OBJECT)
1028 ss <<
"Field type: " << ret->getFName().fieldType <<
" ";
1030 return std::nullopt;
1036 return detail::make_stvar<STArray>(std::move(tail));
1041 return std::nullopt;
1053 using namespace STParsedJSONDetail;
1063 using namespace STParsedJSONDetail;
1069 auto p =
dynamic_cast<STArray*
>(&arr->get());
1073 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.