diff --git a/src/coders/binary_json.cpp b/src/coders/binary_json.cpp index c65012d3..c296dc16 100644 --- a/src/coders/binary_json.cpp +++ b/src/coders/binary_json.cpp @@ -2,6 +2,7 @@ #include +#include "gzip.h" #include "byte_utils.h" using namespace json; @@ -148,12 +149,21 @@ static Map* object_from_binary(ByteReader& reader) { } std::unique_ptr json::from_binary(const ubyte* src, size_t size) { - ByteReader reader(src, size); - std::unique_ptr value (value_from_binary(reader)); - if (value->type != valtype::map) { - throw std::runtime_error("root value is not an object"); + if (size < 2) { + throw std::runtime_error("bytes length is less than 2"); + } + if (src[0] == gzip::MAGIC[0] && src[1] == gzip::MAGIC[1]) { + // reading compressed document + auto data = gzip::decompress(src, size); + return from_binary(data.data(), data.size()); + } else { + ByteReader reader(src, size); + std::unique_ptr value (value_from_binary(reader)); + if (value->type != valtype::map) { + throw std::runtime_error("root value is not an object"); + } + std::unique_ptr obj (value->value.map); + value->value.map = nullptr; + return obj; } - std::unique_ptr obj (value->value.map); - value->value.map = nullptr; - return obj; } diff --git a/src/coders/gzip.h b/src/coders/gzip.h index 2bf6cba7..f83cdfe6 100644 --- a/src/coders/gzip.h +++ b/src/coders/gzip.h @@ -5,6 +5,7 @@ #include "../typedefs.h" namespace gzip { + const unsigned char MAGIC[] = "\x1F\x8B"; std::vector compress(const ubyte* src, size_t size); std::vector decompress(const ubyte* src, size_t size); }