20 #include <ripple/basics/StringUtilities.h>
21 #include <ripple/basics/contract.h>
22 #include <ripple/basics/safe_cast.h>
23 #include <ripple/beast/core/LexicalCast.h>
24 #include <ripple/protocol/ErrorCodes.h>
25 #include <ripple/protocol/LedgerFormats.h>
26 #include <ripple/protocol/STAccount.h>
27 #include <ripple/protocol/STAmount.h>
28 #include <ripple/protocol/STArray.h>
29 #include <ripple/protocol/STBitString.h>
30 #include <ripple/protocol/STBlob.h>
31 #include <ripple/protocol/STInteger.h>
32 #include <ripple/protocol/STParsedJSON.h>
33 #include <ripple/protocol/STPathSet.h>
34 #include <ripple/protocol/STVector256.h>
35 #include <ripple/protocol/TER.h>
36 #include <ripple/protocol/TxFormats.h>
37 #include <ripple/protocol/UintTypes.h>
38 #include <ripple/protocol/impl/STVar.h>
45 namespace STParsedJSONDetail {
46 template <
typename U,
typename S>
52 Throw<std::runtime_error>(
"Value out of range");
53 return static_cast<U
>(value);
56 template <
typename U1,
typename U2>
62 Throw<std::runtime_error>(
"Value out of range");
63 return static_cast<U1
>(value);
72 return object +
"." + field;
80 "Field '" +
make_name(
object, field) +
"' is not a JSON object.");
101 "Field '" +
make_name(
object, field) +
"' is unknown.");
109 "Field '" +
make_name(
object, field) +
"' is out of range.");
117 "Field '" +
make_name(
object, field) +
"' has bad type.");
125 "Field '" +
make_name(
object, field) +
"' has invalid data.");
139 "Field '" +
make_name(
object, field) +
"' must be a JSON array.");
147 "Field '" +
make_name(
object, field) +
"' must be a string.");
155 "Field '" +
object +
"' exceeds nesting depth limit.");
164 "]' must be an object with a single key/object value.");
172 "Object '" + sField.
getName() +
173 "' contents did not meet requirements for that type.");
182 " is not an object. Arrays may only contain objects.");
205 switch (field.fieldType)
210 constexpr
auto minValue =
212 constexpr
auto maxValue =
218 if (!strValue.
empty() &&
219 ((strValue[0] <
'0') || (strValue[0] >
'9')))
225 if (!ter ||
TERtoInt(*ter) < minValue ||
232 ret = detail::make_stvar<STUInt8>(
238 error =
bad_type(json_name, fieldName);
244 ret = detail::make_stvar<STUInt8>(
246 beast::lexicalCastThrow<std::uint8_t>(strValue));
249 else if (value.
isInt())
251 if (value.
asInt() < minValue || value.
asInt() > maxValue)
257 ret = detail::make_stvar<STUInt8>(
262 if (value.
asUInt() > maxValue)
268 ret = detail::make_stvar<STUInt8>(
273 error =
bad_type(json_name, fieldName);
291 if (!strValue.
empty() &&
292 ((strValue[0] <
'0') || (strValue[0] >
'9')))
296 ret = detail::make_stvar<STUInt16>(
307 ret = detail::make_stvar<STUInt16>(
324 ret = detail::make_stvar<STUInt16>(
326 beast::lexicalCastThrow<std::uint16_t>(strValue));
329 else if (value.
isInt())
331 ret = detail::make_stvar<STUInt16>(
332 field, to_unsigned<std::uint16_t>(value.
asInt()));
336 ret = detail::make_stvar<STUInt16>(
337 field, to_unsigned<std::uint16_t>(value.
asUInt()));
341 error =
bad_type(json_name, fieldName);
358 ret = detail::make_stvar<STUInt32>(
360 beast::lexicalCastThrow<std::uint32_t>(
363 else if (value.
isInt())
365 ret = detail::make_stvar<STUInt32>(
366 field, to_unsigned<std::uint32_t>(value.
asInt()));
370 ret = detail::make_stvar<STUInt32>(
371 field, safe_cast<std::uint32_t>(value.
asUInt()));
375 error =
bad_type(json_name, fieldName);
397 str.data(), str.data() + str.size(), val, 16);
399 if (ec !=
std::errc() || (p != str.data() + str.size()))
400 Throw<std::invalid_argument>(
"invalid data");
402 ret = detail::make_stvar<STUInt64>(field, val);
404 else if (value.
isInt())
406 ret = detail::make_stvar<STUInt64>(
407 field, to_unsigned<std::uint64_t>(value.
asInt()));
411 ret = detail::make_stvar<STUInt64>(
412 field, safe_cast<std::uint64_t>(value.
asUInt()));
416 error =
bad_type(json_name, fieldName);
431 error =
bad_type(json_name, fieldName);
448 ret = detail::make_stvar<STUInt128>(field, num);
455 error =
bad_type(json_name, fieldName);
472 ret = detail::make_stvar<STUInt160>(field, num);
479 error =
bad_type(json_name, fieldName);
496 ret = detail::make_stvar<STUInt256>(field, num);
503 error =
bad_type(json_name, fieldName);
511 ret = detail::make_stvar<STBlob>(
512 field, vBlob->data(), vBlob->size());
516 Throw<std::invalid_argument>(
"invalid data");
555 Throw<std::invalid_argument>(
"invalid data");
558 ret = detail::make_stvar<STVector256>(std::move(tail));
583 if (!value[i].isArrayOrNull())
586 ss << fieldName <<
"[" << i <<
"]";
594 ss << fieldName <<
"[" << i <<
"][" << j <<
"]";
596 json_name +
"." + ss.
str());
612 bool hasCurrency =
false;
619 if (!account.isString())
628 if (!uAccount.
parseHex(account.asString()))
631 parseBase58<AccountID>(account.asString());
678 parseBase58<AccountID>(issuer.
asString());
690 uAccount, uCurrency, uIssuer, hasCurrency);
695 ret = detail::make_stvar<STPathSet>(std::move(tail));
708 error =
bad_type(json_name, fieldName);
716 if (
AccountID account; account.parseHex(strValue))
717 return detail::make_stvar<STAccount>(field, account);
719 if (
auto result = parseBase58<AccountID>(strValue))
720 return detail::make_stvar<STAccount>(field, *result);
734 error =
bad_type(json_name, fieldName);
788 switch (field.fieldType)
804 json_name +
"." + fieldName,
811 data.emplace_back(std::move(*ret));
826 json_name +
"." + fieldName,
831 if (!array.has_value())
833 data.emplace_back(std::move(*array));
846 parseLeaf(json_name, fieldName, &inName, value, error);
851 data.emplace_back(std::move(*leaf));
859 data.applyTemplateFromSField(inName);
901 bool const isObjectOrNull(json[i].isObjectOrNull());
902 bool const singleKey(isObjectOrNull ? json[i].size() == 1 :
true);
904 if (!isObjectOrNull || !singleKey)
914 std::string const objectName(json[i].getMemberNames()[0]);
924 Json::Value const objectFields(json[i][objectName]);
927 ss << json_name <<
"."
928 <<
"[" << i <<
"]." << objectName;
931 ss.
str(), objectFields, nameField, depth + 1, error);
934 std::string errMsg = error[
"error_message"].asString();
935 error[
"error_message"] =
936 "Error at '" + ss.
str() +
"'. " + errMsg;
949 return detail::make_stvar<STArray>(std::move(tail));
966 using namespace STParsedJSONDetail;
976 using namespace STParsedJSONDetail;
982 auto p =
dynamic_cast<STArray*
>(&arr->get());
986 array = std::move(*p);