diff --git a/src/serialization/json_object.cpp b/src/serialization/json_object.cpp index e98ba0483..926eb18c0 100644 --- a/src/serialization/json_object.cpp +++ b/src/serialization/json_object.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2016-2019, The Monero Project +// Copyright (c) 2016-2020, The Monero Project // // All rights reserved. // @@ -32,7 +32,11 @@ #include #include #include -#include "string_tools.h" + +// drop macro from windows.h +#ifdef GetObject + #undef GetObject +#endif namespace cryptonote { @@ -109,6 +113,19 @@ namespace } } +void read_hex(const rapidjson::Value& val, epee::span dest) +{ + if (!val.IsString()) + { + throw WRONG_TYPE("string"); + } + + if (!epee::from_hex::to_buffer(dest, {val.GetString(), val.Size()})) + { + throw BAD_INPUT(); + } +} + void toJsonValue(rapidjson::Writer& dest, const rapidjson::Value& src) { src.Accept(dest); diff --git a/src/serialization/json_object.h b/src/serialization/json_object.h index a1a5105d5..664b539b5 100644 --- a/src/serialization/json_object.h +++ b/src/serialization/json_object.h @@ -1,4 +1,4 @@ -// Copyright (c) 2016-2019, The Monero Project +// Copyright (c) 2016-2020, The Monero Project // // All rights reserved. // @@ -30,7 +30,6 @@ #include #include -#include "string_tools.h" // out of order because windows.h GetObject macro conflicts with GenericValue<..>::GetObject() #include #include #include @@ -39,6 +38,8 @@ #include "rpc/message_data_structs.h" #include "cryptonote_protocol/cryptonote_protocol_defs.h" #include "common/sfinae_helpers.h" +#include "hex.h" +#include "span.h" #define OBJECT_HAS_MEMBER_OR_THROW(val, key) \ do \ @@ -118,6 +119,8 @@ inline constexpr bool is_to_hex() return std::is_pod() && !std::is_integral(); } +void read_hex(const rapidjson::Value& val, epee::span dest); + // POD to json key template inline typename std::enable_if()>::type toJsonKey(rapidjson::Writer& dest, const Type& pod) @@ -137,18 +140,8 @@ inline typename std::enable_if()>::type toJsonValue(rapidjson::W template inline typename std::enable_if()>::type fromJsonValue(const rapidjson::Value& val, Type& t) { - if (!val.IsString()) - { - throw WRONG_TYPE("string"); - } - - //TODO: handle failure to convert hex string to POD type - bool success = epee::string_tools::hex_to_pod(val.GetString(), t); - - if (!success) - { - throw BAD_INPUT(); - } + static_assert(std::is_standard_layout(), "expected standard layout type"); + json::read_hex(val, epee::as_mut_byte_span(t)); } void toJsonValue(rapidjson::Writer& dest, const rapidjson::Value& src);