summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--modules/codec-json/c++/json.tmpl.hpp14
-rw-r--r--modules/codec-json/tests/codec-json.cpp85
-rw-r--r--modules/codec/c++/data.hpp1
3 files changed, 95 insertions, 5 deletions
diff --git a/modules/codec-json/c++/json.tmpl.hpp b/modules/codec-json/c++/json.tmpl.hpp
index c2e5575..d17c02d 100644
--- a/modules/codec-json/c++/json.tmpl.hpp
+++ b/modules/codec-json/c++/json.tmpl.hpp
@@ -3,8 +3,6 @@
#include <charconv>
#include <sstream>
-#include <iostream>
-
namespace saw {
namespace impl {
struct json_helper {
@@ -739,7 +737,6 @@ struct json_decode<schema::Struct<schema::Member<T,Lits>...>, ToDecode> {
std::array<bool, sizeof...(T)> found_fields;
std::fill(found_fields.begin(), found_fields.end(), false);
- std::cout<<buff.read()<<" "<<(int16_t)buff.read()<<std::endl;
SAW_ASSERT(buff.read() == '{'){
return make_error<err::invalid_state>();
}
@@ -835,6 +832,7 @@ struct json_decode<schema::Array<T,D>, ToDecode> {
static error_or<void> decode_flat_level(buffer_view& buff, std::vector<data<T, encode::Native, storage::Default>>& to, std::array<std::size_t, D>& index, std::array<std::size_t, D>& dims, bool log_dim){
if constexpr (Level == D) {
json_helper::skip_whitespace(buff);
+
try {
to.push_back({});
}catch(std::exception& e){
@@ -852,9 +850,17 @@ struct json_decode<schema::Array<T,D>, ToDecode> {
if ( buff.read_composite_length() == 0 ){
return make_error<err::buffer_exhausted>();
}
+ /**
+ * Check if array is empty.
+ */
+ bool is_empty = false;
+ if(buff.read() == ']'){
+ buff.read_advance(1);
+ is_empty = true;
+ }
index[Level] = 0;
- for(;;){
+ 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);
if(eov.is_error()){
diff --git a/modules/codec-json/tests/codec-json.cpp b/modules/codec-json/tests/codec-json.cpp
index dd8df8a..3c97935 100644
--- a/modules/codec-json/tests/codec-json.cpp
+++ b/modules/codec-json/tests/codec-json.cpp
@@ -24,6 +24,12 @@ using TestStruct = Struct<
Member<Int32, "foo">,
Member<String, "bar">
>;
+
+using TestArrayStruct = Array<TestStruct>;
+
+using TestStructArrayStruct = Struct<
+ Member<TestArrayStruct, "banana">
+>;
}
SAW_TEST("UInt8 write"){
@@ -317,6 +323,85 @@ SAW_TEST("Struct read and write"){
SAW_EXPECT(native.get<"bar">() == "baz", "Invalid value for bar");
}
+SAW_TEST("Array Struct Empty read and write"){
+ using namespace saw;
+ data<schema::TestArrayStruct, encode::Native, storage::Default> native{0u};
+ data<schema::TestArrayStruct, encode::Json> json;
+
+ codec<schema::TestArrayStruct, encode::Json> json_codec;
+
+ error_or<void> eov = json_codec.encode(native, json);
+ SAW_EXPECT(eov.is_value(), "Encoding error");
+
+ std::string_view str_v = "[]";
+ std::string enc_val = convert_to_string(json.get_buffer());
+
+ SAW_EXPECT(enc_val == str_v, std::string{"Struct not encoded correctly. Encoded: "} + enc_val + std::string{" Expected: "} + std::string{str_v});
+
+ native = {};
+ eov = json_codec.decode(json, native);
+ SAW_EXPECT(eov.is_value(), "Decoding error");
+}
+
+SAW_TEST("Array Struct read and write"){
+ using namespace saw;
+ data<schema::TestArrayStruct, encode::Native, storage::Default> native{4u};
+ data<schema::TestArrayStruct, encode::Json> json;
+
+ native.at(0).get<"foo">().set(5);
+ native.at(0).get<"bar">().set("baz");
+
+ native.at(1).get<"foo">().set(6);
+ native.at(1).get<"bar">().set("baz1");
+
+ native.at(2).get<"foo">().set(326);
+ native.at(2).get<"bar">().set("baz12");
+
+ codec<schema::TestArrayStruct, encode::Json> json_codec;
+
+ error_or<void> eov = json_codec.encode(native, json);
+ SAW_EXPECT(eov.is_value(), "Encoding error");
+
+ std::string_view str_v = "[{\"foo\":5,\"bar\":\"baz\"},{\"foo\":6,\"bar\":\"baz1\"},{\"foo\":326,\"bar\":\"baz12\"},{\"foo\":0,\"bar\":\"\"}]";
+ std::string enc_val = convert_to_string(json.get_buffer());
+
+ SAW_EXPECT(enc_val == str_v, std::string{"Struct not encoded correctly. Encoded: "} + enc_val + std::string{" Expected: "} + std::string{str_v});
+
+ native = {};
+ eov = json_codec.decode(json, native);
+ SAW_EXPECT(eov.is_value(), "Decoding error");
+}
+
+SAW_TEST("Struct Array Struct read and write"){
+ using namespace saw;
+ data<schema::TestStructArrayStruct, encode::Native, storage::Default> native;
+ native.template get<"banana">() = {4u};
+ data<schema::TestStructArrayStruct, encode::Json> json;
+
+ native.template get<"banana">().at(0).get<"foo">().set(5);
+ native.template get<"banana">().at(0).get<"bar">().set("baz");
+
+ native.template get<"banana">().at(1).get<"foo">().set(6);
+ native.template get<"banana">().at(1).get<"bar">().set("baz1");
+
+ native.template get<"banana">().at(2).get<"foo">().set(326);
+ native.template get<"banana">().at(2).get<"bar">().set("baz12");
+
+ codec<schema::TestStructArrayStruct, encode::Json> json_codec;
+
+ error_or<void> eov = json_codec.encode(native, json);
+ SAW_EXPECT(eov.is_value(), "Encoding error");
+
+ std::string_view str_v = "{\"banana\":[{\"foo\":5,\"bar\":\"baz\"},{\"foo\":6,\"bar\":\"baz1\"},{\"foo\":326,\"bar\":\"baz12\"},{\"foo\":0,\"bar\":\"\"}]}";
+ std::string enc_val = convert_to_string(json.get_buffer());
+
+ SAW_EXPECT(enc_val == str_v, std::string{"Struct not encoded correctly. Encoded: "} + enc_val + std::string{" Expected: "} + std::string{str_v});
+
+ native = {};
+ eov = json_codec.decode(json, native);
+ SAW_EXPECT(eov.is_value(), "Decoding error");
+}
+
SAW_TEST("Int8 read"){
using namespace saw;
data<schema::Int8, encode::Native, storage::Default> native_int;
diff --git a/modules/codec/c++/data.hpp b/modules/codec/c++/data.hpp
index bd4c15b..371ae71 100644
--- a/modules/codec/c++/data.hpp
+++ b/modules/codec/c++/data.hpp
@@ -537,7 +537,6 @@ class data<schema::Array<T,Dim>, encode::Native, storage::Default> {
uint64_t s = 1;
for(uint64_t iter = 0; iter < Dim; ++iter){
- assert(dims_.at(iter) > 0);
s *= dims_.at(iter);
}