binary json: compressed documents support
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
|
#include "gzip.h"
|
||||||
#include "byte_utils.h"
|
#include "byte_utils.h"
|
||||||
|
|
||||||
using namespace json;
|
using namespace json;
|
||||||
@@ -148,12 +149,21 @@ static Map* object_from_binary(ByteReader& reader) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<Map> json::from_binary(const ubyte* src, size_t size) {
|
std::unique_ptr<Map> json::from_binary(const ubyte* src, size_t size) {
|
||||||
ByteReader reader(src, size);
|
if (size < 2) {
|
||||||
std::unique_ptr<Value> value (value_from_binary(reader));
|
throw std::runtime_error("bytes length is less than 2");
|
||||||
if (value->type != valtype::map) {
|
}
|
||||||
throw std::runtime_error("root value is not an object");
|
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 (value_from_binary(reader));
|
||||||
|
if (value->type != valtype::map) {
|
||||||
|
throw std::runtime_error("root value is not an object");
|
||||||
|
}
|
||||||
|
std::unique_ptr<Map> obj (value->value.map);
|
||||||
|
value->value.map = nullptr;
|
||||||
|
return obj;
|
||||||
}
|
}
|
||||||
std::unique_ptr<Map> obj (value->value.map);
|
|
||||||
value->value.map = nullptr;
|
|
||||||
return obj;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
#include "../typedefs.h"
|
#include "../typedefs.h"
|
||||||
|
|
||||||
namespace gzip {
|
namespace gzip {
|
||||||
|
const unsigned char MAGIC[] = "\x1F\x8B";
|
||||||
std::vector<ubyte> compress(const ubyte* src, size_t size);
|
std::vector<ubyte> compress(const ubyte* src, size_t size);
|
||||||
std::vector<ubyte> decompress(const ubyte* src, size_t size);
|
std::vector<ubyte> decompress(const ubyte* src, size_t size);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user