From be09d1faf1ba1bf83825d1c02714d4ae27ddffe0 Mon Sep 17 00:00:00 2001 From: Claudius 'keldu' Holeksa Date: Fri, 16 Aug 2024 11:56:18 +0200 Subject: Encode bools for json --- modules/codec-json/c++/json.tmpl.hpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/modules/codec-json/c++/json.tmpl.hpp b/modules/codec-json/c++/json.tmpl.hpp index fd789a4..979be96 100644 --- a/modules/codec-json/c++/json.tmpl.hpp +++ b/modules/codec-json/c++/json.tmpl.hpp @@ -40,6 +40,33 @@ class json_encode { static_assert(always_false, "This schema type is not being handled by the json encoding."); }; +template +struct json_encode { + using Schema = schema::Bool; + + static error_or encode(const data& from, buffer& to, uint64_t, bool){ + auto val = from.get(); + std::string_view view = val ? "true" : "false"; + uint64_t view_s = view.size(); + + auto& buff = to; + { + error err = buff.write_require_length(view_s); + if(!err.template is_type()){ + return std::move(err); + } + } + + { + error err = buff.push(reinterpret_cast(&view[0], view_s); + if(!err.template is_type()){ + return std::move(err); + } + } + return make_void(); + } +}; + template struct json_encode, FromEncode> { using Schema = schema::Primitive; -- cgit v1.2.3