diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-05-07 20:50:48 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-11-07 16:41:37 +0000 |
commit | ce5c689a5f9e544d724b2a41fda2f752ccc4a4ae (patch) | |
tree | 33f2c53d2cb1b67f3c7024bbec2366be875b57d9 /lib/jsonParse-persistence.cpp | |
parent | Fixup clang, cppcheck and iwyu warnings (diff) | |
download | ilt-ce5c689a5f9e544d724b2a41fda2f752ccc4a4ae.tar.bz2 ilt-ce5c689a5f9e544d724b2a41fda2f752ccc4a4ae.tar.xz ilt-ce5c689a5f9e544d724b2a41fda2f752ccc4a4ae.zip |
Initial commit where writing objects back out to JSON
It's not perfect, writes explicit nulls, doesn't do shared @id
Diffstat (limited to 'lib/jsonParse-persistence.cpp')
-rw-r--r-- | lib/jsonParse-persistence.cpp | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/lib/jsonParse-persistence.cpp b/lib/jsonParse-persistence.cpp index dce9d3c..6ecfff8 100644 --- a/lib/jsonParse-persistence.cpp +++ b/lib/jsonParse-persistence.cpp @@ -1,4 +1,8 @@ #include "jsonParse-persistence.h" +#include <algorithm> +#include <array> +#include <iomanip> +#include <ostream> #include <type_traits> #include <utility> @@ -83,4 +87,116 @@ namespace Persistence { { return stk.top(); } + + static inline void + wrv(std::ostream & strm, char ch) + { + strm.put(ch); + }; + static inline void + wrh(std::ostream & strm, char ch) + { + using namespace std::literals; + strm << R"(\u)"sv << std::setw(4) << std::hex << (int)ch << std::setw(1); + } + static inline void + wre(std::ostream & strm, char e) + { + strm << '\\' << e; + } + template<char E> + static inline void + wre(std::ostream & strm, char) + { + wre(strm, E); + } + + using OutFunc = void (*)(std::ostream &, char); + using OutFuncs = std::array<OutFunc, 255>; + static constexpr OutFuncs outFuncs {[]() { + OutFuncs outFuncs; + outFuncs.fill(&wrv); + for (int x = 0; x < 0x20; x += 1) { + outFuncs[x] = &wrh; + } + outFuncs['\"'] = &wre<'"'>; + outFuncs['\\'] = &wre<'\\'>; + outFuncs['\b'] = &wre<'b'>; + outFuncs['\f'] = &wre<'f'>; + outFuncs['\n'] = &wre<'n'>; + outFuncs['\r'] = &wre<'r'>; + outFuncs['\t'] = &wre<'t'>; + return outFuncs; + }()}; + + JsonWritePersistence::JsonWritePersistence(std::ostream & s) : strm {s} + { + strm << std::boolalpha // for Boolean + << std::defaultfloat // for Number + << std::setfill('0'); // for String \uNNNN + } + + void + JsonWritePersistence::beginObject() const + { + strm << '{'; + } + + void + JsonWritePersistence::beginArray() const + { + strm << '['; + } + + void + JsonWritePersistence::pushValue(bool value) const + { + strm << value; + } + + void + JsonWritePersistence::pushValue(float value) const + { + strm << value; + } + + void JsonWritePersistence::pushValue(std::nullptr_t) const + { + strm << "null"; + } + + void + JsonWritePersistence::pushValue(const std::string_view value) const + { + strm << '"'; + std::for_each(value.begin(), value.end(), [this](char ch) { + outFuncs[ch](strm, ch); + }); + strm << '"'; + } + + void + JsonWritePersistence::nextValue() const + { + strm << ','; + } + + void + JsonWritePersistence::pushKey(const std::string_view k) const + { + pushValue(k); + strm << ':'; + } + + void + JsonWritePersistence::endArray() const + { + strm << ']'; + } + + void + JsonWritePersistence::endObject() const + { + strm << '}'; + } } |