mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-06 02:36:18 +08:00
Added support for reading and writing into streams
This commit is contained in:
@@ -95,8 +95,10 @@ const auto homer =
|
||||
|
||||
// We can now transform this into a JSON string.
|
||||
const std::string json_string = rfl::json::write(homer);
|
||||
|
||||
std::cout << json_string << std::endl;
|
||||
|
||||
// We can also directly write into the stream.
|
||||
rfl::json::write(homer, std::cout) << std::endl;
|
||||
```
|
||||
|
||||
This results in the following JSON string:
|
||||
|
||||
@@ -244,6 +244,25 @@ const auto person = Person{...};
|
||||
rfl::flexbuf::save("/path/to/file.fb", person);
|
||||
```
|
||||
|
||||
## Reading from and writing into streams
|
||||
|
||||
You can also read from and write into any `std::istream` and `std::ostream` respectively.
|
||||
|
||||
```cpp
|
||||
const rfl::Result<Person> result = rfl::flexbuf::read<Person>(my_istream);
|
||||
|
||||
const auto person = Person{...};
|
||||
rfl::flexbuf::write(person, my_ostream);
|
||||
```
|
||||
|
||||
Note that `std::cout` is also an ostream, so this works as well:
|
||||
|
||||
```cpp
|
||||
rfl::flexbuf::write(person, std::cout) << std::endl;
|
||||
```
|
||||
|
||||
(Since flexbuffers is a binary format, the readability of this will be limited, but it might be useful for debugging).
|
||||
|
||||
## Custom constructors
|
||||
|
||||
One of the great things about C++ is that it gives you control over
|
||||
|
||||
@@ -39,6 +39,23 @@ const auto person = Person{...};
|
||||
rfl::json::save("/path/to/file.json", person);
|
||||
```
|
||||
|
||||
## Reading from and writing into streams
|
||||
|
||||
You can also read from and write into any `std::istream` and `std::ostream` respectively.
|
||||
|
||||
```cpp
|
||||
const rfl::Result<Person> result = rfl::json::read<Person>(my_istream);
|
||||
|
||||
const auto person = Person{...};
|
||||
rfl::json::write(person, my_ostream);
|
||||
```
|
||||
|
||||
Note that `std::cout` is also an ostream, so this works as well:
|
||||
|
||||
```cpp
|
||||
rfl::json::write(person, std::cout) << std::endl;
|
||||
```
|
||||
|
||||
## Custom constructors
|
||||
|
||||
One of the great things about C++ is that it gives you control over
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <flatbuffers/flexbuffers.h>
|
||||
|
||||
#include <istream>
|
||||
#include <vector>
|
||||
|
||||
#include "rfl/Result.hpp"
|
||||
@@ -22,7 +23,7 @@ Result<T> read(const InputVarType& _obj) {
|
||||
|
||||
/// Parses an object from flexbuf using reflection.
|
||||
template <class T>
|
||||
Result<T> read(const unsigned char* _bytes, const size_t _size) {
|
||||
Result<T> read(const char* _bytes, const size_t _size) {
|
||||
const InputVarType root =
|
||||
flexbuffers::GetRoot(reinterpret_cast<const uint8_t*>(_bytes), _size);
|
||||
return read<T>(root);
|
||||
@@ -30,10 +31,18 @@ Result<T> read(const unsigned char* _bytes, const size_t _size) {
|
||||
|
||||
/// Parses an object from flexbuf using reflection.
|
||||
template <class T>
|
||||
Result<T> read(const std::vector<unsigned char>& _bytes) {
|
||||
Result<T> read(const std::vector<char>& _bytes) {
|
||||
return read<T>(_bytes.data(), _bytes.size());
|
||||
}
|
||||
|
||||
/// Parses an object directly from a stream.
|
||||
template <class T>
|
||||
Result<T> read(std::istream& _stream) {
|
||||
std::istreambuf_iterator<char> begin(_stream), end;
|
||||
const auto bytes = std::vector<char>(begin, end);
|
||||
return read<T>(bytes.data(), bytes.size());
|
||||
}
|
||||
|
||||
} // namespace flexbuf
|
||||
} // namespace rfl
|
||||
|
||||
|
||||
@@ -14,7 +14,10 @@ namespace flexbuf {
|
||||
|
||||
template <class T>
|
||||
Result<Nothing> save(const std::string& _fname, const T& _obj) {
|
||||
return rfl::io::save_bytes(_fname, write(_obj));
|
||||
const auto write_func = [](const auto& _obj, auto& _stream) -> auto& {
|
||||
return write(_obj, _stream);
|
||||
};
|
||||
return rfl::io::save_bytes(_fname, _obj, write_func);
|
||||
}
|
||||
|
||||
} // namespace flexbuf
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <flatbuffers/flexbuffers.h>
|
||||
|
||||
#include <cstddef>
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
@@ -12,17 +13,31 @@
|
||||
namespace rfl {
|
||||
namespace flexbuf {
|
||||
|
||||
/// Writes an object to flexbuf.
|
||||
template <class T>
|
||||
std::vector<unsigned char> write(const T& _obj) {
|
||||
std::vector<uint8_t> to_buffer(const T& _obj) {
|
||||
auto w = Writer();
|
||||
const auto flexbuf_obj = Parser<T>::write(w, _obj);
|
||||
flexbuffers::Builder fbb;
|
||||
flexbuf_obj->insert(std::nullopt, &fbb);
|
||||
fbb.Finish();
|
||||
const auto vec = fbb.GetBuffer();
|
||||
const auto data = reinterpret_cast<const unsigned char*>(vec.data());
|
||||
return std::vector<unsigned char>(data, data + vec.size());
|
||||
return fbb.GetBuffer();
|
||||
}
|
||||
|
||||
/// Writes an object to flexbuf.
|
||||
template <class T>
|
||||
std::vector<char> write(const T& _obj) {
|
||||
const auto buffer = to_buffer(_obj);
|
||||
const auto data = reinterpret_cast<const char*>(buffer.data());
|
||||
return std::vector<char>(data, data + buffer.size());
|
||||
}
|
||||
|
||||
/// Writes an object to an ostream.
|
||||
template <class T>
|
||||
std::ostream& write(const T& _obj, std::ostream& _stream) {
|
||||
const auto buffer = to_buffer(_obj);
|
||||
const auto data = reinterpret_cast<const char*>(buffer.data());
|
||||
_stream.write(data, buffer.size());
|
||||
return _stream;
|
||||
}
|
||||
|
||||
} // namespace flexbuf
|
||||
|
||||
@@ -11,11 +11,11 @@
|
||||
namespace rfl {
|
||||
namespace io {
|
||||
|
||||
Result<std::vector<unsigned char>> load_bytes(const std::string& _fname) {
|
||||
Result<std::vector<char>> load_bytes(const std::string& _fname) {
|
||||
std::ifstream input(_fname, std::ios::binary);
|
||||
if (input.is_open()) {
|
||||
std::istreambuf_iterator<char> begin(input), end;
|
||||
const auto bytes = std::vector<unsigned char>(begin, end);
|
||||
const auto bytes = std::vector<char>(begin, end);
|
||||
input.close();
|
||||
return bytes;
|
||||
} else {
|
||||
|
||||
@@ -11,16 +11,16 @@ namespace rfl {
|
||||
namespace io {
|
||||
|
||||
Result<std::string> load_string(const std::string& _fname) {
|
||||
std::ifstream infile(_fname);
|
||||
if (infile.is_open()) {
|
||||
auto r = std::string(std::istreambuf_iterator<char>(infile),
|
||||
std::istreambuf_iterator<char>());
|
||||
infile.close();
|
||||
return r;
|
||||
} else {
|
||||
return Error("Unable to open file '" + _fname +
|
||||
"' or file could not be found.");
|
||||
}
|
||||
std::ifstream infile(_fname);
|
||||
if (infile.is_open()) {
|
||||
auto r = std::string(std::istreambuf_iterator<char>(infile),
|
||||
std::istreambuf_iterator<char>());
|
||||
infile.close();
|
||||
return r;
|
||||
} else {
|
||||
return Error("Unable to open file '" + _fname +
|
||||
"' or file could not be found.");
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace io
|
||||
|
||||
@@ -11,11 +11,12 @@
|
||||
namespace rfl {
|
||||
namespace io {
|
||||
|
||||
Result<Nothing> save_bytes(const std::string& _fname,
|
||||
const std::vector<unsigned char>& _bytes) {
|
||||
template <class T, class WriteFunction>
|
||||
Result<Nothing> save_bytes(const std::string& _fname, const T& _obj,
|
||||
const WriteFunction& _write) {
|
||||
try {
|
||||
std::ofstream output(_fname, std::ios::out | std::ios::binary);
|
||||
output.write(reinterpret_cast<const char*>(_bytes.data()), _bytes.size());
|
||||
_write(_obj, output);
|
||||
output.close();
|
||||
} catch (std::exception& e) {
|
||||
return Error(e.what());
|
||||
|
||||
@@ -10,17 +10,18 @@
|
||||
namespace rfl {
|
||||
namespace io {
|
||||
|
||||
Result<Nothing> save_string(const std::string& _fname,
|
||||
const std::string& _str) {
|
||||
try {
|
||||
std::ofstream outfile;
|
||||
outfile.open(_fname);
|
||||
outfile << _str;
|
||||
outfile.close();
|
||||
} catch (std::exception& e) {
|
||||
return Error(e.what());
|
||||
}
|
||||
return Nothing{};
|
||||
template <class T, class WriteFunction>
|
||||
Result<Nothing> save_string(const std::string& _fname, const T& _obj,
|
||||
const WriteFunction& _write) {
|
||||
try {
|
||||
std::ofstream outfile;
|
||||
outfile.open(_fname);
|
||||
_write(_obj, outfile);
|
||||
outfile.close();
|
||||
} catch (std::exception& e) {
|
||||
return Error(e.what());
|
||||
}
|
||||
return Nothing{};
|
||||
}
|
||||
|
||||
} // namespace io
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <yyjson.h>
|
||||
|
||||
#include <istream>
|
||||
#include <string>
|
||||
|
||||
#include "rfl/json/Parser.hpp"
|
||||
@@ -17,17 +18,25 @@ using InputVarType = typename Reader::InputVarType;
|
||||
/// Parses an object from a JSON var.
|
||||
template <class T>
|
||||
Result<T> read(const InputVarType& _obj) {
|
||||
const auto r = Reader();
|
||||
return Parser<T>::read(r, _obj);
|
||||
const auto r = Reader();
|
||||
return Parser<T>::read(r, _obj);
|
||||
}
|
||||
|
||||
/// Parses an object from JSON using reflection.
|
||||
template <class T>
|
||||
Result<T> read(const std::string& _json_str) {
|
||||
using PtrType = std::unique_ptr<yyjson_doc, void (*)(yyjson_doc*)>;
|
||||
yyjson_doc* doc = yyjson_read(_json_str.c_str(), _json_str.size(), 0);
|
||||
const auto ptr = PtrType(doc, yyjson_doc_free);
|
||||
return read<T>(InputVarType(yyjson_doc_get_root(doc)));
|
||||
using PtrType = std::unique_ptr<yyjson_doc, void (*)(yyjson_doc*)>;
|
||||
yyjson_doc* doc = yyjson_read(_json_str.c_str(), _json_str.size(), 0);
|
||||
const auto ptr = PtrType(doc, yyjson_doc_free);
|
||||
return read<T>(InputVarType(yyjson_doc_get_root(doc)));
|
||||
}
|
||||
|
||||
/// Parses an object from a stringstream.
|
||||
template <class T>
|
||||
Result<T> read(std::istream& _stream) {
|
||||
const auto json_str = std::string(std::istreambuf_iterator<char>(_stream),
|
||||
std::istreambuf_iterator<char>());
|
||||
return read<T>(json_str);
|
||||
}
|
||||
|
||||
} // namespace json
|
||||
|
||||
@@ -14,7 +14,10 @@ namespace json {
|
||||
|
||||
template <class T>
|
||||
Result<Nothing> save(const std::string& _fname, const T& _obj) {
|
||||
return rfl::io::save_string(_fname, write(_obj));
|
||||
const auto write_func = [](const auto& _obj, auto& _stream) -> auto& {
|
||||
return write(_obj, _stream);
|
||||
};
|
||||
return rfl::io::save_string(_fname, _obj, write_func);
|
||||
}
|
||||
|
||||
} // namespace json
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <yyjson.h>
|
||||
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
@@ -11,17 +12,30 @@
|
||||
namespace rfl {
|
||||
namespace json {
|
||||
|
||||
/// Parses an object to JSON.
|
||||
/// Returns a JSON string.
|
||||
template <class T>
|
||||
std::string write(const T& _obj) {
|
||||
auto w = Writer(yyjson_mut_doc_new(NULL));
|
||||
const auto json_obj = Parser<T>::write(w, _obj);
|
||||
yyjson_mut_doc_set_root(w.doc_, json_obj.val_);
|
||||
const char* json_c_str = yyjson_mut_write(w.doc_, 0, NULL);
|
||||
const auto json_str = std::string(json_c_str);
|
||||
free((void*)json_c_str);
|
||||
yyjson_mut_doc_free(w.doc_);
|
||||
return json_str;
|
||||
auto w = Writer(yyjson_mut_doc_new(NULL));
|
||||
const auto json_obj = Parser<T>::write(w, _obj);
|
||||
yyjson_mut_doc_set_root(w.doc_, json_obj.val_);
|
||||
const char* json_c_str = yyjson_mut_write(w.doc_, 0, NULL);
|
||||
const auto json_str = std::string(json_c_str);
|
||||
free((void*)json_c_str);
|
||||
yyjson_mut_doc_free(w.doc_);
|
||||
return json_str;
|
||||
}
|
||||
|
||||
/// Writes a JSON into an ostream.
|
||||
template <class T>
|
||||
std::ostream& write(const T& _obj, std::ostream& _stream) {
|
||||
auto w = Writer(yyjson_mut_doc_new(NULL));
|
||||
const auto json_obj = Parser<T>::write(w, _obj);
|
||||
yyjson_mut_doc_set_root(w.doc_, json_obj.val_);
|
||||
const char* json_c_str = yyjson_mut_write(w.doc_, 0, NULL);
|
||||
_stream << json_c_str;
|
||||
free((void*)json_c_str);
|
||||
yyjson_mut_doc_free(w.doc_);
|
||||
return _stream;
|
||||
}
|
||||
|
||||
} // namespace json
|
||||
|
||||
Reference in New Issue
Block a user