aboutsummaryrefslogtreecommitdiff
path: root/src/serialization
diff options
context:
space:
mode:
authorLee Clagett <code@leeclagett.com>2020-05-31 01:22:33 -0400
committerLee Clagett <code@leeclagett.com>2020-08-14 23:01:00 +0000
commit4e2377995d286eb01456ed1dfd29cd278282ec19 (patch)
treec722a778b7c07d71008d35ce6d5907f92a7de25e /src/serialization
parentMerge pull request #6582 (diff)
downloadmonero-4e2377995d286eb01456ed1dfd29cd278282ec19.tar.xz
Change ZMQ-JSON txextra to hex and remove unnecessary base fields
Diffstat (limited to 'src/serialization')
-rw-r--r--src/serialization/json_object.cpp20
-rw-r--r--src/serialization/json_object.h11
2 files changed, 31 insertions, 0 deletions
diff --git a/src/serialization/json_object.cpp b/src/serialization/json_object.cpp
index 6228b4bec..1f71126c3 100644
--- a/src/serialization/json_object.cpp
+++ b/src/serialization/json_object.cpp
@@ -146,6 +146,26 @@ void fromJsonValue(const rapidjson::Value& val, std::string& str)
str = val.GetString();
}
+void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const std::vector<std::uint8_t>& src)
+{
+ const std::string hex = epee::to_hex::string(epee::to_span(src));
+ dest.String(hex.data(), hex.size());
+}
+
+void fromJsonValue(const rapidjson::Value& val, std::vector<std::uint8_t>& dest)
+{
+ if (!val.IsString())
+ {
+ throw WRONG_TYPE("string");
+ }
+
+ dest.resize(val.GetStringLength() / 2);
+ if ((val.GetStringLength() % 2) != 0 || !epee::from_hex::to_buffer(epee::to_mut_span(dest), {val.GetString(), val.GetStringLength()}))
+ {
+ throw BAD_INPUT();
+ }
+}
+
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, bool i)
{
dest.Bool(i);
diff --git a/src/serialization/json_object.h b/src/serialization/json_object.h
index 2a9b63b08..9f652a679 100644
--- a/src/serialization/json_object.h
+++ b/src/serialization/json_object.h
@@ -153,6 +153,9 @@ inline void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const std::s
}
void fromJsonValue(const rapidjson::Value& val, std::string& str);
+void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const std::vector<std::uint8_t>&);
+void fromJsonValue(const rapidjson::Value& src, std::vector<std::uint8_t>& i);
+
void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, bool i);
void fromJsonValue(const rapidjson::Value& val, bool& b);
@@ -353,6 +356,10 @@ inline typename std::enable_if<sfinae::is_map_like<Map>::value, void>::type from
template <typename Vec>
inline typename std::enable_if<sfinae::is_vector_like<Vec>::value, void>::type toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const Vec &vec)
{
+ using value_type = typename Vec::value_type;
+ static_assert(!std::is_same<value_type, char>::value, "encoding an array of chars is faster as hex");
+ static_assert(!std::is_same<value_type, unsigned char>::value, "encoding an array of unsigned char is faster as hex");
+
dest.StartArray();
for (const auto& t : vec)
toJsonValue(dest, t);
@@ -362,6 +369,10 @@ inline typename std::enable_if<sfinae::is_vector_like<Vec>::value, void>::type t
template <typename Vec>
inline typename std::enable_if<sfinae::is_vector_like<Vec>::value, void>::type fromJsonValue(const rapidjson::Value& val, Vec& vec)
{
+ using value_type = typename Vec::value_type;
+ static_assert(!std::is_same<value_type, char>::value, "encoding a vector of chars is faster as hex");
+ static_assert(!std::is_same<value_type, unsigned char>::value, "encoding a vector of unsigned char is faster as hex");
+
if (!val.IsArray())
{
throw WRONG_TYPE("json array");