Redesigned the writer (#36) and added YAML support based on the new writer (#23)

This commit is contained in:
Patrick Urbanke
2024-01-03 07:22:36 +01:00
parent 0a524a9893
commit 5c81e40f09
133 changed files with 3082 additions and 838 deletions
+5 -4
View File
@@ -7,6 +7,7 @@
#include <sstream>
#include <string>
#include "../parsing/Parent.hpp"
#include "Parser.hpp"
namespace rfl {
@@ -15,9 +16,9 @@ namespace json {
/// Returns a JSON string.
template <class T>
std::string write(const T& _obj) {
using ParentType = parsing::Parent<Writer>;
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_);
Parser<T>::write(w, _obj, typename ParentType::Root{});
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);
@@ -28,9 +29,9 @@ std::string write(const T& _obj) {
/// Writes a JSON into an ostream.
template <class T>
std::ostream& write(const T& _obj, std::ostream& _stream) {
using ParentType = parsing::Parent<Writer>;
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_);
Parser<T>::write(w, _obj, typename ParentType::Root{});
const char* json_c_str = yyjson_mut_write(w.doc_, 0, NULL);
_stream << json_c_str;
free((void*)json_c_str);