Skip to content

Commit

Permalink
csv encode
Browse files Browse the repository at this point in the history
  • Loading branch information
danielaparker committed Jan 17, 2025
1 parent d3fb277 commit a235fdc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
11 changes: 10 additions & 1 deletion include/jsoncons_ext/csv/csv_encoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,17 @@ class basic_csv_encoder final : public basic_json_visitor<CharT>
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<CharT>();
}
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:
Expand Down Expand Up @@ -417,7 +426,7 @@ class basic_csv_encoder final : public basic_json_visitor<CharT>
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)
Expand Down
16 changes: 10 additions & 6 deletions test/csv/src/csv_encoder_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]],
Expand All @@ -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
Expand Down Expand Up @@ -447,6 +451,6 @@ NY,LON,TOR;LON

CHECK(expected == buf);
}
#endif
//#endif
}

0 comments on commit a235fdc

Please sign in to comment.