From d1d40903a582099762db04feeec451d7f0a617a8 Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Tue, 13 Feb 2024 16:26:01 +0100 Subject: codec-json: Removed RootSchema from decoders and encoders --- modules/codec-json/c++/json.hpp | 10 +-- modules/codec-json/c++/json.tmpl.hpp | 126 ++++++++++++++++++++--------------- 2 files changed, 79 insertions(+), 57 deletions(-) (limited to 'modules/codec-json') diff --git a/modules/codec-json/c++/json.hpp b/modules/codec-json/c++/json.hpp index c7e4203..ad29acb 100644 --- a/modules/codec-json/c++/json.hpp +++ b/modules/codec-json/c++/json.hpp @@ -8,7 +8,8 @@ namespace saw { namespace encode { -struct Json {}; +struct Json { +}; } template @@ -72,7 +73,8 @@ class codec { public: struct config { size_t depth = 16; - size_t length = 1024; + size_t length = 4096; + bool pretty = false; }; private: config cfg_; @@ -94,7 +96,7 @@ public: error_or encode(const data& from_encode, data& to_encode){ // To Be encoded buffer_view buff_v{to_encode.get_buffer()}; - auto eov = impl::json_encode::encode(from_encode, buff_v); + auto eov = impl::json_encode::encode(from_encode, buff_v); if(eov.is_error()){ return std::move(eov.get_error()); } @@ -107,7 +109,7 @@ public: error_or decode(data& from_decode, data& to_decode){ buffer_view buff_v{from_decode.get_buffer()}; - auto eov = impl::json_decode::decode(buff_v, to_decode); + auto eov = impl::json_decode::decode(buff_v, to_decode); if(eov.is_error()){ return std::move(eov.get_error()); } diff --git a/modules/codec-json/c++/json.tmpl.hpp b/modules/codec-json/c++/json.tmpl.hpp index aa8f9b9..11eba49 100644 --- a/modules/codec-json/c++/json.tmpl.hpp +++ b/modules/codec-json/c++/json.tmpl.hpp @@ -7,14 +7,16 @@ namespace saw { namespace impl { -template +template class json_encode { - static_assert(always_false, "This schema type is not being handled by the json encoding."); + static_assert(always_false, "This schema type is not being handled by the json encoding."); }; -template -struct json_encode, RootSchema, FromEncode> { - static error_or encode(const data, FromEncode>& from, buffer& to) { +template +struct json_encode, FromEncode> { + using Schema = schema::Primitive; + + static error_or encode(const data& from, buffer& to) { auto val = from.get(); std::array data; auto tc_result = std::to_chars(reinterpret_cast(data.data()), reinterpret_cast(data.data())+data.size(), val); @@ -43,8 +45,10 @@ struct json_encode, RootSchema, FromEncode> { } }; -template -struct json_encode { +template +struct json_encode { + using Schema = schema::String; + static error_or encode(const data& from, buffer& to) { { auto err = to.push('"'); @@ -69,11 +73,13 @@ struct json_encode { } }; -template -struct json_encode, RootSchema, FromEncode> { +template +struct json_encode, FromEncode> { + using Schema = schema::Tuple; + template static error_or encode_element(const data, FromEncode>& from, buffer& to){ - auto eov = json_encode::type, RootSchema, FromEncode>::encode(from.template get(), to); + auto eov = json_encode::type, FromEncode>::encode(from.template get(), to); if(eov.is_error()){ return eov; @@ -98,7 +104,7 @@ struct json_encode, RootSchema, FromEncode> { return void_t{}; } - static error_or encode(const data, FromEncode>& from, buffer& to) { + static error_or encode(const data& from, buffer& to) { { auto err = to.push('['); if(!err.template is_type()){ @@ -122,12 +128,14 @@ struct json_encode, RootSchema, FromEncode> { } }; -template -struct json_encode, RootSchema, FromEncode> { +template +struct json_encode, FromEncode> { + using Schema = schema::Array; + template - static error_or encode_level(const data, FromEncode>& from, buffer& to, std::array& index){ + static error_or encode_level(const data& from, buffer& to, std::array& index){ if constexpr (Level == D){ - auto eov = json_encode::encode(from.at(index), to); + auto eov = json_encode::encode(from.at(index), to); if(eov.is_error()){ return eov; } @@ -163,18 +171,18 @@ struct json_encode, RootSchema, FromEncode> { return void_t{}; } - static error_or encode(const data, FromEncode>& from, buffer& to) { + static error_or encode(const data& from, buffer& to) { std::array index; return encode_level<0>(from, to, index); } }; -template -struct json_encode, RootSchema, FromEncode> { +template +struct json_encode, FromEncode> { using Schema = schema::FixedArray; static error_or encode_at(const data& from, buffer& to, std::array& index){ - auto eov = json_encode::encode(from.at(index), to); + auto eov = json_encode::encode(from.at(index), to); if(eov.is_error()){ return eov; } @@ -229,11 +237,12 @@ struct json_encode, RootSchema, FromEncode> { } }; -template -struct json_encode...>, RootSchema, FromEncode> { +template +struct json_encode...>, FromEncode> { + using Schema = schema::Struct...>; template - static error_or encode_element(const data...>, FromEncode>& from, buffer& to){ + static error_or encode_element(const data& from, buffer& to){ // Encode the name { std::string_view view = parameter_key_pack_type::literal.view(); @@ -259,7 +268,7 @@ struct json_encode...>, RootSchema, FromEnc } // Encode the value - auto eov = json_encode::type, RootSchema, FromEncode>::encode(from.template get::literal>(), to); + auto eov = json_encode::type, FromEncode>::encode(from.template get::literal>(), to); // Go to the next element if constexpr ( (i+1) < sizeof...(T)){ @@ -281,7 +290,7 @@ struct json_encode...>, RootSchema, FromEnc return void_t{}; } - static error_or encode(const data...>, FromEncode>& from, buffer& to) { + static error_or encode(const data& from, buffer& to) { { auto err = to.push('{'); if(!err.template is_type()){ @@ -305,11 +314,12 @@ struct json_encode...>, RootSchema, FromEnc } }; -template -struct json_encode...>, RootSchema, FromEncode> { +template +struct json_encode...>, FromEncode> { + using Schema = schema::Union...>; template - static error_or encode_element(const data...>, FromEncode>& from, buffer& to){ + static error_or encode_element(const data& from, buffer& to){ if(from.template holds_alternative::literal>()){ // Encode the name { @@ -336,7 +346,7 @@ struct json_encode...>, RootSchema, FromEnco } // Encode the value - auto eov = json_encode::type, RootSchema, FromEncode>::encode(from.template get::literal>(), to); + auto eov = json_encode::type, FromEncode>::encode(from.template get::literal>(), to); } // Go to the next element if constexpr ( (i+1) < sizeof...(T)){ @@ -352,7 +362,7 @@ struct json_encode...>, RootSchema, FromEnco return void_t{}; } - static error_or encode(const data...>, FromEncode>& from, buffer& to) { + static error_or encode(const data& from, buffer& to) { { auto err = to.push('{'); if(!err.template is_type()){ @@ -388,12 +398,14 @@ struct json_helper { } }; -template +template struct json_decode; -template -struct json_decode, RootSchema, ToDecode> { - static error_or decode(buffer_view& buff, data, ToDecode>& to) { +template +struct json_decode, ToDecode> { + using Schema = schema::Primitive; + + static error_or decode(buffer_view& buff, data& to) { assert( ( buff.read() >= '0' && buff.read() <= '9') || ( buff.read() == '+' || buff.read() == '-') @@ -435,7 +447,7 @@ struct json_decode, RootSchema, ToDecode> { { std::string_view num_view{reinterpret_cast(&buff.read()), offset}; - typename native_data_type>::type result; + typename native_data_type::type result; auto fc_result = std::from_chars(num_view.data(), num_view.data() + num_view.size(), result); if(fc_result.ec != std::errc{}){ return make_error(); @@ -449,9 +461,11 @@ struct json_decode, RootSchema, ToDecode> { } }; -template -struct json_decode { - static error_or decode(buffer_view& buff, data& to){ +template +struct json_decode { + using Schema = schema::String; + + static error_or decode(buffer_view& buff, data& to){ assert(buff.read() == '"'); buff.read_advance(1); @@ -534,10 +548,12 @@ struct json_decode { } }; -template -struct json_decode...>, RootSchema, ToDecode> { +template +struct json_decode...>, ToDecode> { + using Schema = schema::Struct...>; + template - static error_or decode_field_search(buffer_view& buff, data...>, ToDecode>& to, std::array& fields, const data& search_name){ + static error_or decode_field_search(buffer_view& buff, data& to, std::array& fields, const data& search_name){ if constexpr ( i < sizeof...(T)){ using Type = typename parameter_pack_type::type; constexpr static string_literal Literal = parameter_key_pack_type::literal; @@ -546,7 +562,7 @@ struct json_decode...>, RootSchema, ToDeco return make_error(); } fields[i] = true; - auto eov = json_decode::decode(buff, to.template get()); + auto eov = json_decode::decode(buff, to.template get()); if(eov.is_error()){ return eov; } @@ -559,10 +575,10 @@ struct json_decode...>, RootSchema, ToDeco return void_t{}; } - static error_or decode_fields(buffer_view& buff, data...>, ToDecode>& to, std::array& fields){ + static error_or decode_fields(buffer_view& buff, data& to, std::array& fields){ for(;;){ data name; - auto eov = json_decode::decode(buff, name); + auto eov = json_decode::decode(buff, name); if(eov.is_error()){ return eov; } @@ -610,7 +626,7 @@ struct json_decode...>, RootSchema, ToDeco return void_t{}; } - static error_or decode(buffer_view& buff, data...>, ToDecode>& to){ + static error_or decode(buffer_view& buff, data& to){ std::array found_fields; std::fill(found_fields.begin(), found_fields.end(), false); @@ -639,10 +655,12 @@ struct json_decode...>, RootSchema, ToDeco } }; -template -struct json_decode, RootSchema, ToDecode> { +template +struct json_decode, ToDecode> { + using Schema = schema::Tuple; + template - static error_or decode_element(buffer_view& buff, data, ToDecode>& to){ + static error_or decode_element(buffer_view& buff, data& to){ if constexpr (i < sizeof...(T)){ if constexpr ( i > 0 ){ if(buff.read() != ','){ @@ -655,7 +673,7 @@ struct json_decode, RootSchema, ToDecode> { } using Type = typename parameter_pack_type::type; - auto eov = json_decode::decode(buff, to.template get()); + auto eov = json_decode::decode(buff, to.template get()); if(eov.is_error()){ return eov; } @@ -677,7 +695,7 @@ struct json_decode, RootSchema, ToDecode> { return void_t{}; } - static error_or decode(buffer_view& buff, data, ToDecode>& to){ + static error_or decode(buffer_view& buff, data& to){ assert(buff.read() == '['); buff.read_advance(1); @@ -696,8 +714,10 @@ struct json_decode, RootSchema, ToDecode> { }; // The whole std::vector approach is hacky af. ToDo change it maybe? -template -struct json_decode, RootSchema, ToDecode> { +template +struct json_decode, ToDecode> { + using Schema = schema::Array; + template static error_or decode_flat_level(buffer_view& buff, std::vector>& to, std::array& index, std::array& dims, bool log_dim){ if constexpr (Level == D) { @@ -707,7 +727,7 @@ struct json_decode, RootSchema, ToDecode> { }catch(std::exception& e){ return make_error(); } - auto eov = json_decode::decode(buff, to.back()); + auto eov = json_decode::decode(buff, to.back()); if(eov.is_error()){ return eov; } @@ -774,7 +794,7 @@ struct json_decode, RootSchema, ToDecode> { return void_t{}; } - static error_or decode(buffer_view& buff, data, ToDecode>& to){ + static error_or decode(buffer_view& buff, data& to){ std::array index; std::array dims; std::fill(dims.begin(), dims.end(), 0); -- cgit v1.2.3