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>
53 Throw<std::runtime_error>(
"Value out of range");
54 return static_cast<U
>(value);
57template <
typename U1,
typename U2>
63 Throw<std::runtime_error>(
"Value out of range");
64 return static_cast<U1
>(value);
74 return object +
"." + field;
82 "Field '" +
make_name(
object, field) +
"' is not a JSON object.");
103 "Field '" +
make_name(
object, field) +
"' is unknown.");
111 "Field '" +
make_name(
object, field) +
"' is out of range.");
119 "Field '" +
make_name(
object, field) +
"' has bad type.");
127 "Field '" +
make_name(
object, field) +
"' has invalid data.");
141 "Field '" +
make_name(
object, field) +
"' must be a JSON array.");
149 "Field '" +
make_name(
object, field) +
"' must be a string.");
157 "Field '" +
object +
"' exceeds nesting depth limit.");
166 "]' must be an object with a single key/object value.");
174 "Object '" + sField.
getName() +
175 "' contents did not meet requirements for that type.");
184 " is not an object. Arrays may only contain objects.");
188template <
class STResult,
class Integer>
204 ret = detail::make_stvar<STResult>(
206 safe_cast<typename STResult::value_type>(
207 beast::lexicalCastThrow<Integer>(value.
asString())));
209 else if (value.
isInt())
211 ret = detail::make_stvar<STResult>(
213 to_unsigned<typename STResult::value_type>(value.
asInt()));
217 ret = detail::make_stvar<STResult>(
219 to_unsigned<typename STResult::value_type>(value.
asUInt()));
223 error =
bad_type(json_name, fieldName);
236template <
class STResult,
class Integer = std::u
int16_t>
254 if (!strValue.
empty() &&
255 ((strValue[0] <
'0') || (strValue[0] >
'9')))
257 if (field == sfTransactionType)
259 ret = detail::make_stvar<STResult>(
261 safe_cast<typename STResult::value_type>(
262 static_cast<Integer
>(
267 name = &sfTransaction;
269 else if (field == sfLedgerEntryType)
271 ret = detail::make_stvar<STResult>(
273 safe_cast<typename STResult::value_type>(
274 static_cast<Integer
>(
279 name = &sfLedgerEntry;
289 return parseUnsigned<STResult, Integer>(
290 field, json_name, fieldName, name, value, error);
301template <
class STResult,
class Integer = std::u
int32_t>
317 if (field == sfPermissionValue)
320 auto const granularPermission =
322 if (granularPermission)
324 ret = detail::make_stvar<STResult>(
325 field, *granularPermission);
331 ret = detail::make_stvar<STResult>(
338 ret = detail::make_stvar<STResult>(
340 safe_cast<typename STResult::value_type>(
341 beast::lexicalCastThrow<Integer>(value.
asString())));
345 return parseUnsigned<STResult, Integer>(
346 field, json_name, fieldName, name, value, error);
380 switch (field.fieldType)
385 constexpr auto minValue =
387 constexpr auto maxValue =
393 if (!strValue.
empty() &&
394 ((strValue[0] <
'0') || (strValue[0] >
'9')))
396 if (field == sfTransactionResult)
400 if (!ter ||
TERtoInt(*ter) < minValue ||
407 ret = detail::make_stvar<STUInt8>(
413 error =
bad_type(json_name, fieldName);
419 ret = detail::make_stvar<STUInt8>(
421 beast::lexicalCastThrow<std::uint8_t>(strValue));
424 else if (value.
isInt())
426 if (value.
asInt() < minValue || value.
asInt() > maxValue)
432 ret = detail::make_stvar<STUInt8>(
437 if (value.
asUInt() > maxValue)
443 ret = detail::make_stvar<STUInt8>(
448 error =
bad_type(json_name, fieldName);
460 ret = parseUint16<STUInt16>(
461 field, json_name, fieldName, name, value, error);
468 ret = parseUint32<STUInt32>(
469 field, json_name, fieldName, name, value, error);
484 bool const useBase10 =
490 str.data() + str.size(),
492 useBase10 ? 10 : 16);
494 if (ec !=
std::errc() || (p != str.data() + str.size()))
495 Throw<std::invalid_argument>(
"invalid data");
497 ret = detail::make_stvar<STUInt64>(field, val);
499 else if (value.
isInt())
501 ret = detail::make_stvar<STUInt64>(
502 field, to_unsigned<std::uint64_t>(value.
asInt()));
506 ret = detail::make_stvar<STUInt64>(
507 field, safe_cast<std::uint64_t>(value.
asUInt()));
511 error =
bad_type(json_name, fieldName);
526 error =
bad_type(json_name, fieldName);
543 ret = detail::make_stvar<STUInt128>(field, num);
550 error =
bad_type(json_name, fieldName);
567 ret = detail::make_stvar<STUInt160>(field, num);
574 error =
bad_type(json_name, fieldName);
591 ret = detail::make_stvar<STUInt192>(field, num);
598 error =
bad_type(json_name, fieldName);
615 ret = detail::make_stvar<STUInt256>(field, num);
624 ret = detail::make_stvar<STInt32>(
626 beast::lexicalCastThrow<std::int32_t>(
629 else if (value.
isInt())
636 ret = detail::make_stvar<STInt32>(field, value.
asInt());
640 auto const uintValue = value.
asUInt();
648 ret = detail::make_stvar<STInt32>(
653 error =
bad_type(json_name, fieldName);
668 error =
bad_type(json_name, fieldName);
676 ret = detail::make_stvar<STBlob>(
677 field, vBlob->data(), vBlob->size());
681 Throw<std::invalid_argument>(
"invalid data");
734 Throw<std::invalid_argument>(
"invalid data");
737 ret = detail::make_stvar<STVector256>(std::move(tail));
762 if (!value[i].isArrayOrNull())
765 ss << fieldName <<
"[" << i <<
"]";
773 ss << fieldName <<
"[" << i <<
"][" << j <<
"]";
775 json_name +
"." + ss.
str());
791 bool hasCurrency =
false;
795 if (!account && !currency && !issuer)
804 if (!account.isString())
813 if (!uAccount.
parseHex(account.asString()))
816 parseBase58<AccountID>(account.asString());
863 parseBase58<AccountID>(issuer.
asString());
875 uAccount, uCurrency, uIssuer, hasCurrency);
880 ret = detail::make_stvar<STPathSet>(std::move(tail));
893 error =
bad_type(json_name, fieldName);
901 if (
AccountID account; account.parseHex(strValue))
902 return detail::make_stvar<STAccount>(field, account);
904 if (
auto result = parseBase58<AccountID>(strValue))
905 return detail::make_stvar<STAccount>(field, *result);
921 ret = detail::make_stvar<STIssue>(
issueFromJson(field, value));
930 case STI_XCHAIN_BRIDGE:
933 ret = detail::make_stvar<STXChainBridge>(
946 ret = detail::make_stvar<STCurrency>(
957 error =
bad_type(json_name, fieldName);
1011 switch (field.fieldType)
1015 case STI_TRANSACTION:
1016 case STI_LEDGERENTRY:
1017 case STI_VALIDATION:
1027 json_name +
"." + fieldName,
1034 data.emplace_back(std::move(*ret));
1049 json_name +
"." + fieldName,
1054 if (!array.has_value())
1056 data.emplace_back(std::move(*array));
1069 parseLeaf(json_name, fieldName, &inName, value, error);
1074 data.emplace_back(std::move(*leaf));
1082 data.applyTemplateFromSField(inName);
1124 bool const isObjectOrNull(json[i].isObjectOrNull());
1125 bool const singleKey(isObjectOrNull ? json[i].size() == 1 :
true);
1127 if (!isObjectOrNull || !singleKey)
1137 std::string const objectName(json[i].getMemberNames()[0]);
1147 Json::Value const objectFields(json[i][objectName]);
1150 ss << json_name <<
"." <<
"[" << i <<
"]." << objectName;
1153 ss.
str(), objectFields, nameField, depth + 1, error);
1156 std::string errMsg = error[
"error_message"].asString();
1157 error[
"error_message"] =
1158 "Error at '" + ss.
str() +
"'. " + errMsg;
1162 if (ret->getFName().fieldType != STI_OBJECT)
1164 ss <<
"Field type: " << ret->getFName().fieldType <<
" ";
1172 return detail::make_stvar<STArray>(std::move(tail));
1189 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()
std::string const & getName() const
static SField const & getField(int fieldCode)
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.
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.