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/STIssue.h>
33 #include <ripple/protocol/STParsedJSON.h>
34 #include <ripple/protocol/STPathSet.h>
35 #include <ripple/protocol/STVector256.h>
36 #include <ripple/protocol/TER.h>
37 #include <ripple/protocol/TxFormats.h>
38 #include <ripple/protocol/UintTypes.h>
39 #include <ripple/protocol/impl/STVar.h>
46 namespace STParsedJSONDetail {
47 template <
typename U,
typename S>
53 Throw<std::runtime_error>(
"Value out of range");
54 return static_cast<U
>(value);
57 template <
typename U1,
typename U2>
63 Throw<std::runtime_error>(
"Value out of range");
64 return static_cast<U1
>(value);
73 return object +
"." + field;
81 "Field '" +
make_name(
object, field) +
"' is not a JSON object.");
102 "Field '" +
make_name(
object, field) +
"' is unknown.");
110 "Field '" +
make_name(
object, field) +
"' is out of range.");
118 "Field '" +
make_name(
object, field) +
"' has bad type.");
126 "Field '" +
make_name(
object, field) +
"' has invalid data.");
140 "Field '" +
make_name(
object, field) +
"' must be a JSON array.");
148 "Field '" +
make_name(
object, field) +
"' must be a string.");
156 "Field '" +
object +
"' exceeds nesting depth limit.");
165 "]' must be an object with a single key/object value.");
173 "Object '" + sField.
getName() +
174 "' contents did not meet requirements for that type.");
183 " is not an object. Arrays may only contain objects.");
206 switch (field.fieldType)
211 constexpr
auto minValue =
213 constexpr
auto maxValue =
219 if (!strValue.
empty() &&
220 ((strValue[0] <
'0') || (strValue[0] >
'9')))
226 if (!ter ||
TERtoInt(*ter) < minValue ||
233 ret = detail::make_stvar<STUInt8>(
239 error =
bad_type(json_name, fieldName);
245 ret = detail::make_stvar<STUInt8>(
247 beast::lexicalCastThrow<std::uint8_t>(strValue));
250 else if (value.
isInt())
252 if (value.
asInt() < minValue || value.
asInt() > maxValue)
258 ret = detail::make_stvar<STUInt8>(
263 if (value.
asUInt() > maxValue)
269 ret = detail::make_stvar<STUInt8>(
274 error =
bad_type(json_name, fieldName);
292 if (!strValue.
empty() &&
293 ((strValue[0] <
'0') || (strValue[0] >
'9')))
297 ret = detail::make_stvar<STUInt16>(
308 ret = detail::make_stvar<STUInt16>(
325 ret = detail::make_stvar<STUInt16>(
327 beast::lexicalCastThrow<std::uint16_t>(strValue));
330 else if (value.
isInt())
332 ret = detail::make_stvar<STUInt16>(
333 field, to_unsigned<std::uint16_t>(value.
asInt()));
337 ret = detail::make_stvar<STUInt16>(
338 field, to_unsigned<std::uint16_t>(value.
asUInt()));
342 error =
bad_type(json_name, fieldName);
359 ret = detail::make_stvar<STUInt32>(
361 beast::lexicalCastThrow<std::uint32_t>(
364 else if (value.
isInt())
366 ret = detail::make_stvar<STUInt32>(
367 field, to_unsigned<std::uint32_t>(value.
asInt()));
371 ret = detail::make_stvar<STUInt32>(
372 field, safe_cast<std::uint32_t>(value.
asUInt()));
376 error =
bad_type(json_name, fieldName);
398 str.data(), str.data() + str.size(), val, 16);
400 if (ec !=
std::errc() || (p != str.data() + str.size()))
401 Throw<std::invalid_argument>(
"invalid data");
403 ret = detail::make_stvar<STUInt64>(field, val);
405 else if (value.
isInt())
407 ret = detail::make_stvar<STUInt64>(
408 field, to_unsigned<std::uint64_t>(value.
asInt()));
412 ret = detail::make_stvar<STUInt64>(
413 field, safe_cast<std::uint64_t>(value.
asUInt()));
417 error =
bad_type(json_name, fieldName);
432 error =
bad_type(json_name, fieldName);
449 ret = detail::make_stvar<STUInt128>(field, num);
456 error =
bad_type(json_name, fieldName);
473 ret = detail::make_stvar<STUInt160>(field, num);
480 error =
bad_type(json_name, fieldName);
497 ret = detail::make_stvar<STUInt256>(field, num);
504 error =
bad_type(json_name, fieldName);
512 ret = detail::make_stvar<STBlob>(
513 field, vBlob->data(), vBlob->size());
517 Throw<std::invalid_argument>(
"invalid data");
556 Throw<std::invalid_argument>(
"invalid data");
559 ret = detail::make_stvar<STVector256>(std::move(tail));
584 if (!value[i].isArrayOrNull())
587 ss << fieldName <<
"[" << i <<
"]";
595 ss << fieldName <<
"[" << i <<
"][" << j <<
"]";
597 json_name +
"." + ss.
str());
613 bool hasCurrency =
false;
620 if (!account.isString())
629 if (!uAccount.
parseHex(account.asString()))
632 parseBase58<AccountID>(account.asString());
679 parseBase58<AccountID>(issuer.
asString());
691 uAccount, uCurrency, uIssuer, hasCurrency);
696 ret = detail::make_stvar<STPathSet>(std::move(tail));
709 error =
bad_type(json_name, fieldName);
717 if (
AccountID account; account.parseHex(strValue))
718 return detail::make_stvar<STAccount>(field, account);
720 if (
auto result = parseBase58<AccountID>(strValue))
721 return detail::make_stvar<STAccount>(field, *result);
737 ret = detail::make_stvar<STIssue>(
issueFromJson(field, value));
746 error =
bad_type(json_name, fieldName);
800 switch (field.fieldType)
816 json_name +
"." + fieldName,
823 data.emplace_back(std::move(*ret));
838 json_name +
"." + fieldName,
843 if (!array.has_value())
845 data.emplace_back(std::move(*array));
858 parseLeaf(json_name, fieldName, &inName, value, error);
863 data.emplace_back(std::move(*leaf));
871 data.applyTemplateFromSField(inName);
913 bool const isObjectOrNull(json[i].isObjectOrNull());
914 bool const singleKey(isObjectOrNull ? json[i].size() == 1 :
true);
916 if (!isObjectOrNull || !singleKey)
926 std::string const objectName(json[i].getMemberNames()[0]);
936 Json::Value const objectFields(json[i][objectName]);
939 ss << json_name <<
"."
940 <<
"[" << i <<
"]." << objectName;
943 ss.
str(), objectFields, nameField, depth + 1, error);
946 std::string errMsg = error[
"error_message"].asString();
947 error[
"error_message"] =
948 "Error at '" + ss.
str() +
"'. " + errMsg;
961 return detail::make_stvar<STArray>(std::move(tail));
978 using namespace STParsedJSONDetail;
988 using namespace STParsedJSONDetail;
994 auto p =
dynamic_cast<STArray*
>(&arr->get());
998 array = std::move(*p);