diff options
Diffstat (limited to 'modules/codec-json')
-rw-r--r-- | modules/codec-json/c++/json.tmpl.hpp | 31 | ||||
-rw-r--r-- | modules/codec-json/tests/codec-json.cpp | 16 |
2 files changed, 23 insertions, 24 deletions
diff --git a/modules/codec-json/c++/json.tmpl.hpp b/modules/codec-json/c++/json.tmpl.hpp index 1eb46bc..e51d456 100644 --- a/modules/codec-json/c++/json.tmpl.hpp +++ b/modules/codec-json/c++/json.tmpl.hpp @@ -199,7 +199,7 @@ struct json_encode<schema::Array<T,D>, FromEncode> { using Schema = schema::Array<T,D>; template<size_t Level> - static error_or<void> encode_level(const data<Schema, FromEncode>& from, buffer& to, std::array<std::size_t, D>& index, uint64_t depth, bool pretty){ + static error_or<void> encode_level(const data<Schema, FromEncode>& from, buffer& to, data<schema::FixedArray<schema::UInt64,D>, FromEncode>& index, uint64_t depth, bool pretty){ if constexpr (Level == D){ if(pretty){ auto eov = json_helper::print_pretty_indent(to, depth); @@ -232,7 +232,7 @@ struct json_encode<schema::Array<T,D>, FromEncode> { } } { - index[Level] = i; + index.at(Level) = i; auto eov = encode_level<Level+1>(from, to, index, depth+1u, pretty); if(eov.is_error()){ return eov; @@ -256,7 +256,7 @@ struct json_encode<schema::Array<T,D>, FromEncode> { } static error_or<void> encode(const data<Schema, FromEncode>& from, buffer& to, uint64_t depth, bool pretty) { - std::array<std::size_t, D> index; + data<schema::FixedArray<schema::UInt64,D>, FromEncode> index; return encode_level<0>(from, to, index, depth+1u, pretty); } }; @@ -289,7 +289,7 @@ struct json_encode<schema::FixedArray<T,D...>, FromEncode> { } } { - index[Level] = i; + index.at(Level) = i; if constexpr (sizeof...(DimPack) > 0){ auto eov = encode_level<Level+1, DimPack...>(from, to, index, depth, pretty); if(eov.is_error()){ @@ -837,7 +837,7 @@ struct json_decode<schema::Array<T,D>, ToDecode> { using Schema = schema::Array<T,D>; template<size_t Level> - static error_or<void> decode_flat_level(buffer_view& buff, std::vector<data<T, encode::Native>>& to, std::array<std::size_t, D>& index, std::array<std::size_t, D>& dims, bool log_dim){ + static error_or<void> decode_flat_level(buffer_view& buff, std::vector<data<T, encode::Native>>& to, data<schema::FixedArray<schema::UInt64,D>, ToDecode>& index, data<schema::FixedArray<schema::UInt64,D>, ToDecode>& dims, bool log_dim){ if constexpr (Level == D) { json_helper::skip_whitespace(buff); @@ -867,10 +867,10 @@ struct json_decode<schema::Array<T,D>, ToDecode> { is_empty = true; } - index[Level] = 0; + index.at(Level) = 0; for(;!is_empty;){ // We should have an element right now - auto eov = decode_flat_level<Level+1>(buff,to,index,dims, index[Level] == 0 && log_dim); + auto eov = decode_flat_level<Level+1>(buff,to,index,dims, index.at(Level).get() == 0 && log_dim); if(eov.is_error()){ return eov; } @@ -879,7 +879,7 @@ struct json_decode<schema::Array<T,D>, ToDecode> { return make_error<err::buffer_exhausted>(); } - ++index[Level]; + ++index.at(Level); if(buff.read() == ','){ buff.read_advance(1); } else if(buff.read() == ']'){ @@ -894,8 +894,8 @@ struct json_decode<schema::Array<T,D>, ToDecode> { } } if(log_dim){ - dims[Level] = index[Level]; - }else if (dims[Level] != index[Level]){ + dims.at(Level) = index.at(Level); + }else if (dims.at(Level) != index.at(Level)){ return make_error<err::invalid_state>("Not matching Array endings"); } } @@ -903,14 +903,14 @@ struct json_decode<schema::Array<T,D>, ToDecode> { } template<std::size_t Level> - static error_or<void> decode_unflat_level(std::vector<data<T,encode::Native>>& flat, data<schema::Array<T,D>, ToDecode>& to, std::array<std::size_t, D>& index, std::size_t& flat_index) { + static error_or<void> decode_unflat_level(std::vector<data<T,encode::Native>>& flat, data<schema::Array<T,D>, ToDecode>& to, data<schema::FixedArray<schema::UInt64,D>, ToDecode>& index, std::size_t& flat_index) { if constexpr ( Level == D ){ - auto& flat_data = flat.at(flat_index); + auto& flat_data = flat.at({flat_index}); to.at(index) = std::move(flat_data); ++flat_index; }else { const std::size_t dim_size = to.get_dim_size(Level); - for(index[Level] = 0; index[Level] < dim_size; ++index[Level]){ + for(index.at(Level) = 0; index.at(Level).get() < dim_size; ++index.at(Level)){ auto eov = decode_unflat_level<Level+1>(flat, to, index, flat_index); if(eov.is_error()){ @@ -922,9 +922,8 @@ struct json_decode<schema::Array<T,D>, ToDecode> { } static error_or<void> decode(buffer_view& buff, data<Schema, ToDecode>& to){ - std::array<std::size_t, D> index; - std::array<std::size_t, D> dims; - std::fill(dims.begin(), dims.end(), 0); + data<schema::FixedArray<schema::UInt64,D>, ToDecode> index; + data<schema::FixedArray<schema::UInt64,D>, ToDecode> dims; std::vector<data<T,encode::Native>> flat_array; auto eov = decode_flat_level<0>(buff, flat_array, index, dims, true); if(eov.is_error()){ diff --git a/modules/codec-json/tests/codec-json.cpp b/modules/codec-json/tests/codec-json.cpp index a181dc0..c5f08ec 100644 --- a/modules/codec-json/tests/codec-json.cpp +++ b/modules/codec-json/tests/codec-json.cpp @@ -273,10 +273,10 @@ SAW_TEST("Three Dim Array write and read"){ data<schema::TestMultiArray, encode::Native> native{2,1,2}; data<schema::TestMultiArray, encode::Json> json; - native.at({0,0,0}).set("multi"); - native.at({0,0,1}).set("baz"); - native.at({1,0,0}).set("foo"); - native.at({1,0,1}).set("bar"); + native.at({{0,0,0}}).set("multi"); + native.at({{0,0,1}}).set("baz"); + native.at({{1,0,0}}).set("foo"); + native.at({{1,0,1}}).set("bar"); codec<schema::TestMultiArray, encode::Json> codec; @@ -291,10 +291,10 @@ SAW_TEST("Three Dim Array write and read"){ native = {}; eov = codec.decode(json, native); SAW_EXPECT(eov.is_value(), "Decoding error"); - SAW_EXPECT(native.at({0,0,0}) == "multi", "Invalid Value at 0,0,0"); - SAW_EXPECT(native.at({0,0,1}) == "baz", "Invalid Value at 0,0,1"); - SAW_EXPECT(native.at({1,0,0}) == "foo", "Invalid Value at 1,0,0"); - SAW_EXPECT(native.at({1,0,1}) == "bar", "Invalid Value at 1,0,1"); + SAW_EXPECT(native.at({{0,0,0}}) == "multi", "Invalid Value at 0,0,0"); + SAW_EXPECT(native.at({{0,0,1}}) == "baz", "Invalid Value at 0,0,1"); + SAW_EXPECT(native.at({{1,0,0}}) == "foo", "Invalid Value at 1,0,0"); + SAW_EXPECT(native.at({{1,0,1}}) == "bar", "Invalid Value at 1,0,1"); } |