Commit 062aeaf7 authored by Julian Becker's avatar Julian Becker

BSON: Reworked the `binary_writer` such that it precomputes the size of the BSON-output.

This way, the output_adapter can work on simple output iterators and no longer requires random access iterators.
parent 81f4b34e
...@@ -18,8 +18,6 @@ template<typename CharType> struct output_adapter_protocol ...@@ -18,8 +18,6 @@ template<typename CharType> struct output_adapter_protocol
{ {
virtual void write_character(CharType c) = 0; virtual void write_character(CharType c) = 0;
virtual void write_characters(const CharType* s, std::size_t length) = 0; virtual void write_characters(const CharType* s, std::size_t length) = 0;
virtual void write_characters_at(std::size_t position, const CharType* s, std::size_t length) = 0;
virtual std::size_t reserve_characters(std::size_t length) = 0;
virtual ~output_adapter_protocol() = default; virtual ~output_adapter_protocol() = default;
}; };
...@@ -44,18 +42,6 @@ class output_vector_adapter : public output_adapter_protocol<CharType> ...@@ -44,18 +42,6 @@ class output_vector_adapter : public output_adapter_protocol<CharType>
std::copy(s, s + length, std::back_inserter(v)); std::copy(s, s + length, std::back_inserter(v));
} }
void write_characters_at(std::size_t position, const CharType* s, std::size_t length) override
{
std::copy(s, s + length, std::begin(v) + position);
}
std::size_t reserve_characters(std::size_t length) override
{
const auto position = v.size();
std::fill_n(std::back_inserter(v), length, static_cast<CharType>(0x00));
return position;
}
private: private:
std::vector<CharType>& v; std::vector<CharType>& v;
}; };
...@@ -77,22 +63,6 @@ class output_stream_adapter : public output_adapter_protocol<CharType> ...@@ -77,22 +63,6 @@ class output_stream_adapter : public output_adapter_protocol<CharType>
stream.write(s, static_cast<std::streamsize>(length)); stream.write(s, static_cast<std::streamsize>(length));
} }
void write_characters_at(std::size_t position, const CharType* s, std::size_t length) override
{
const auto orig_offset = stream.tellp();
stream.seekp(static_cast<typename std::basic_ostream<CharType>::pos_type>(position));
stream.write(s, static_cast<std::streamsize>(length));
stream.seekp(orig_offset);
}
std::size_t reserve_characters(std::size_t length) override
{
const auto position = stream.tellp();
std::vector<CharType> empty(length, static_cast<CharType>(0));
stream.write(empty.data(), length);
return static_cast<std::size_t>(position);
}
private: private:
std::basic_ostream<CharType>& stream; std::basic_ostream<CharType>& stream;
}; };
...@@ -114,19 +84,6 @@ class output_string_adapter : public output_adapter_protocol<CharType> ...@@ -114,19 +84,6 @@ class output_string_adapter : public output_adapter_protocol<CharType>
str.append(s, length); str.append(s, length);
} }
void write_characters_at(std::size_t position, const CharType* s, std::size_t length) override
{
std::copy(s, s + length, std::begin(str) + position);
}
std::size_t reserve_characters(std::size_t length) override
{
const auto position = str.size();
std::fill_n(std::back_inserter(str), length, static_cast<CharType>(0x00));
return position;
}
private: private:
StringType& str; StringType& str;
}; };
......
...@@ -6594,9 +6594,45 @@ class basic_json ...@@ -6594,9 +6594,45 @@ class basic_json
/*! /*!
@brief Serializes the given JSON object `j` to BSON and returns a vector @brief Serializes the given JSON object `j` to BSON and returns a vector
containing the corresponding BSON-representation. containing the corresponding BSON-representation.
@param j The JSON object to convert to BSON.
@return The BSON representation of the JSON input `j`. BSON (Binary JSON) is a binary format in which zero or more ordered key/value pairs are
@pre The input `j` shall be an object: `j.is_object() == true` stored as a single entity (a so-called document).
The library uses the following mapping from JSON values types to BSON types:
JSON value type | value/range | BSON type | marker
--------------- | --------------------------------- | ----------- | ------
null | `null` | null | 0x0A
boolean | `true`, `false` | boolean | 0x08
number_integer | -9223372036854775808..-2147483649 | int64 | 0x12
number_integer | -2147483648..2147483647 | int32 | 0x10
number_integer | 2147483648..9223372036854775807 | int64 | 0x12
number_unsigned | 0..2147483647 | int32 | 0x10
number_unsigned | 2147483648..9223372036854775807 | int64 | 0x12
number_float | *any value* | double | 0x01
string | *any value* | string | 0x02
array | *any value* | document | 0x04
object | *any value* | document | 0x03
@warning The mapping is **incomplete**, since only JSON-objects (and things
contained therein) can be serialized to BSON.
@pre The input `j` is required to be an object: `j.is_object() == true`
@note Any BSON output created via @ref to_bson can be successfully parsed
by @ref from_bson.
@param[in] j JSON value to serialize
@return BSON serialization as byte vector
@complexity Linear in the size of the JSON value @a j.
@sa http://bsonspec.org/spec.html
@sa @ref from_bson(detail::input_adapter, const bool strict) for the
analogous deserialization
@sa @ref to_ubjson(const basic_json&) for the related UBJSON format
@sa @ref to_cbor(const basic_json&) for the related CBOR format
@sa @ref to_msgpack(const basic_json&) for the related MessagePack format
*/ */
static std::vector<uint8_t> to_bson(const basic_json& j) static std::vector<uint8_t> to_bson(const basic_json& j)
{ {
...@@ -6611,6 +6647,7 @@ class basic_json ...@@ -6611,6 +6647,7 @@ class basic_json
@param j The JSON object to convert to BSON. @param j The JSON object to convert to BSON.
@param o The output adapter that receives the binary BSON representation. @param o The output adapter that receives the binary BSON representation.
@pre The input `j` shall be an object: `j.is_object() == true` @pre The input `j` shall be an object: `j.is_object() == true`
@sa @ref to_bson(const basic_json&)
*/ */
static void to_bson(const basic_json& j, detail::output_adapter<uint8_t> o) static void to_bson(const basic_json& j, detail::output_adapter<uint8_t> o)
{ {
......
This diff is collapsed.
...@@ -102,16 +102,6 @@ class alt_string ...@@ -102,16 +102,6 @@ class alt_string
str_impl.resize(n, c); str_impl.resize(n, c);
} }
auto begin() -> std::string::iterator
{
return str_impl.begin();
}
auto end() -> std::string::iterator
{
return str_impl.end();
}
template <typename op_type> template <typename op_type>
bool operator<(const op_type& op) const bool operator<(const op_type& op) const
{ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment