diff --git a/include/jsoncons_ext/csv/csv_encoder.hpp b/include/jsoncons_ext/csv/csv_encoder.hpp index be4aa935c..47a0e1886 100644 --- a/include/jsoncons_ext/csv/csv_encoder.hpp +++ b/include/jsoncons_ext/csv/csv_encoder.hpp @@ -363,8 +363,17 @@ class basic_csv_encoder final : public basic_json_visitor else { append_array_path_component(); + if (stack_[0].count_ == 0) + { + //if (options_.column_names().empty()) + //{ + // column_pointers_.emplace_back(stack_.back().pointer_); + //} + column_pointer_value_map_[stack_.back().pointer_] = std::basic_string(); + } value_buffer_.clear(); stack_.emplace_back(stack_item_kind::multivalued_field); + stack_.back().pointer_ = stack_[stack_.size()-2].pointer_; } break; case stack_item_kind::flat_object: @@ -417,7 +426,7 @@ class basic_csv_encoder final : public basic_json_visitor case stack_item_kind::flat_row: if (stack_[stack_.size()-2].item_kind_ == stack_item_kind::flat_row_mapping) { - if (stack_[0].count_ == 0 && !options_.column_names().empty()) + if (stack_[0].count_ == 0 && !column_pointer_name_map_.empty()) { std::size_t col = 0; for (std::size_t i = 0; i < column_pointers_.size(); ++i) diff --git a/test/csv/src/csv_encoder_tests.cpp b/test/csv/src/csv_encoder_tests.cpp index e6831fab2..185ccae24 100644 --- a/test/csv/src/csv_encoder_tests.cpp +++ b/test/csv/src/csv_encoder_tests.cpp @@ -9,7 +9,7 @@ namespace csv = jsoncons::csv; TEST_CASE("test json to flat csv") { -#if 0 +//#if 0 SECTION("array of objects to csv") { std::string expected = R"(boolean,datetime,float,text @@ -198,7 +198,7 @@ NY,LON,TOR;LON CHECK(expected == buf); } -#endif + SECTION("object of arrays and subarrays to csv") { std::string expected = R"(a,b,c @@ -228,9 +228,13 @@ NY,LON,TOR;LON //std::cout << buf << "\n"; CHECK(expected == buf); } -/* +//#endif + SECTION("array of subarrays to csv") { + std::string expected = R"(1;2;3,4;5;6 +7;8;9,10;11;12 +)"; const std::string jtext = R"( [ [[1,2,3],[4,5,6]], @@ -251,12 +255,12 @@ NY,LON,TOR;LON std::cout << buf << "\n"; //CHECK(expected == buf); } -*/ + } TEST_CASE("test json to non-flat csv") { -#if 0 +//#if 0 SECTION("array of objects to csv") { std::string expected = R"(/boolean,/datetime,/float,/nested/nested/date,/nested/nested/integer,/nested/time,/text @@ -447,6 +451,6 @@ NY,LON,TOR;LON CHECK(expected == buf); } -#endif +//#endif }