diff --git a/docs/bson.md b/docs/bson.md index bd20bd36..4f800941 100644 --- a/docs/bson.md +++ b/docs/bson.md @@ -17,7 +17,7 @@ struct Person { }; ``` -A `person` can be turned into a bytes vector like this: +A `person` can be serialized like this: ```cpp const auto person = Person{...}; @@ -30,6 +30,12 @@ You can parse bytes like this: const rfl::Result result = rfl::bson::read(bytes); ``` +You can also parse raw pointers (either `const char*` or `const uint8_t*`): + +```cpp +const rfl::Result result = rfl::bson::read(ptr, length); +``` + ## Loading and saving You can also load and save to disc using a very similar syntax: diff --git a/include/rfl/bson/read.hpp b/include/rfl/bson/read.hpp index efba5db1..46bee733 100644 --- a/include/rfl/bson/read.hpp +++ b/include/rfl/bson/read.hpp @@ -18,29 +18,32 @@ using InputVarType = typename Reader::InputVarType; /// Parses an object from a BSON var. template -auto read(const InputVarType& _obj) { +Result> read(const InputVarType& _obj) { const auto r = Reader(); return Parser::read(r, _obj); } -/// Parses an object from flexbuf using reflection. +/// Parses an BSON object using reflection. template -Result> read(char* _bytes, - const size_t _size) { +auto read(const uint8_t* _bytes, const size_t _size) { Reader::BSONValue value; value.val_.value.v_doc.data_len = static_cast(_size); - value.val_.value.v_doc.data = reinterpret_cast(_bytes); + value.val_.value.v_doc.data = const_cast(_bytes); value.val_.value_type = BSON_TYPE_DOCUMENT; auto doc = InputVarType{&value}; - auto result = read(doc); - return result; + return read(doc); +} + +/// Parses an BSON object using reflection. +template +auto read(const char* _bytes, const size_t _size) { + return read(reinterpret_cast(_bytes), _size); } /// Parses an object from BSON using reflection. template auto read(const std::vector& _bytes) { - auto bytes = _bytes; - return read(bytes.data(), bytes.size()); + return read(_bytes.data(), _bytes.size()); } /// Parses an object from a stream.