diff --git a/src/coders/binary_json.cpp b/src/coders/binary_json.cpp index 026593b7..fd5d2c23 100644 --- a/src/coders/binary_json.cpp +++ b/src/coders/binary_json.cpp @@ -26,7 +26,7 @@ static void to_binary(ByteBuilder& builder, const Value& value) { builder.put(BJSON_END); break; case Type::bytes: { - const auto bytes = std::get(value).get(); + const auto& bytes = std::get(value).get(); builder.put(BJSON_TYPE_BYTES); builder.putInt32(bytes->size()); builder.put(bytes->data(), bytes->size()); @@ -132,7 +132,9 @@ static Value value_from_binary(ByteReader& reader) { throw std::runtime_error( "buffer_size > remaining_size "+std::to_string(size)); } - return std::make_shared(reader.pointer(), size); + auto bytes = std::make_shared(reader.pointer(), size); + reader.skip(size); + return bytes; } } throw std::runtime_error( diff --git a/test/coders/binary_json.cpp b/test/coders/binary_json.cpp new file mode 100644 index 00000000..2a3ecea1 --- /dev/null +++ b/test/coders/binary_json.cpp @@ -0,0 +1,38 @@ +#include + +#include "data/dynamic.hpp" +#include "coders/binary_json.hpp" + +TEST(BJSON, EncodeDecode) { + const std::string name = "JSON-encoder"; + const int bytesSize = 5000; + const int year = 2019; + const float score = 3.141592; + dynamic::ByteBuffer srcBytes(bytesSize); + for (int i = 0; i < bytesSize; i ++) { + srcBytes[i] = rand(); + } + + std::vector bjsonBytes; + { + dynamic::Map map; + map.put("name", name); + map.put("year", year); + map.put("score", score); + map.put("data", &srcBytes); + + bjsonBytes = json::to_binary(&map, false); + } + { + auto map = json::from_binary(bjsonBytes.data(), bjsonBytes.size()); + EXPECT_EQ(map->get("name"), name); + EXPECT_EQ(map->get("year"), year); + EXPECT_FLOAT_EQ(map->get("score"), score); + auto bytesptr = map->bytes("data"); + const auto& bytes = *bytesptr; + EXPECT_EQ(bytes.size(), bytesSize); + for (int i = 0; i < bytesSize; i++) { + EXPECT_EQ(bytes[i], srcBytes[i]); + } + } +} diff --git a/test/coders/json.cpp b/test/coders/json.cpp index 7212a087..5738559b 100644 --- a/test/coders/json.cpp +++ b/test/coders/json.cpp @@ -3,8 +3,6 @@ #include "coders/json.hpp" #include "util/stringutil.hpp" -#include - TEST(JSON, EncodeDecode) { const std::string name = "JSON-encoder"; const int bytesSize = 20;