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 c607373 commit e0652d0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
10 changes: 8 additions & 2 deletions include/jsoncons_ext/csv/csv_encoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,10 @@ class basic_csv_encoder final : public basic_json_visitor<CharT>
stack_.back().pointer_.push_back('/');
stack_.back().pointer_.append(std::string(name));
stack_.back().pathname_ = std::string(name);
if (options_.column_names().empty())
{
column_pointer_name_map_.emplace(stack_.back().pathname_, name);
}
break;
}
case stack_item_kind::object:
Expand All @@ -528,6 +532,10 @@ class basic_csv_encoder final : public basic_json_visitor<CharT>
stack_.back().pointer_ = stack_[stack_.size()-2].pointer_;
stack_.back().pointer_.push_back('/');
stack_.back().pointer_.append(std::string(name));
if (options_.column_names().empty())
{
column_pointer_name_map_.emplace(stack_.back().pathname_, stack_.back().pathname_);
}
break;
}
case stack_item_kind::column_mapping:
Expand Down Expand Up @@ -582,7 +590,6 @@ class basic_csv_encoder final : public basic_json_visitor<CharT>
column_names_.emplace_back(stack_.back().pathname_);
}
cname_value_map_[stack_.back().pathname_] = std::basic_string<CharT>();
column_pointer_name_map_.emplace(stack_.back().pathname_, stack_.back().pathname_);
}
auto it = cname_value_map_.find(stack_.back().pathname_);
if (it != cname_value_map_.end())
Expand Down Expand Up @@ -664,7 +671,6 @@ class basic_csv_encoder final : public basic_json_visitor<CharT>
if (options_.column_names().empty())
{
column_names_.emplace_back(stack_.back().pathname_);
column_pointer_name_map_.emplace(stack_.back().pathname_, stack_.back().pathname_);
}
cname_value_map_[stack_.back().pathname_] = std::basic_string<CharT>();
}
Expand Down
11 changes: 6 additions & 5 deletions test/csv/src/csv_encoder_tests.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2013-2024 Daniel Parker
// Copyright 2013-2025 Daniel Parker
// Distributed under Boost license

#include <jsoncons/json.hpp>
Expand All @@ -9,7 +9,6 @@ namespace csv = jsoncons::csv;

TEST_CASE("test json to flat csv")
{
//#if 0
SECTION("array of objects to csv")
{
std::string expected = R"(boolean,datetime,float,text
Expand Down Expand Up @@ -61,6 +60,8 @@ true,1948-01-01T14:57:13,1.27,Chicago Sun-Times
CHECK(expected == buf);
}

#if 0

SECTION("array of objects with some missing members to csv")
{
std::string expected = R"(boolean,datetime,float,text
Expand Down Expand Up @@ -226,7 +227,7 @@ NY,LON,TOR;LON
//std::cout << buf << "\n";
CHECK(expected == buf);
}
//#endif
#endif
/*SECTION("array of subarrays to csv")
{
const std::string jtext = R"(
Expand All @@ -253,7 +254,7 @@ NY,LON,TOR;LON

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 @@ -444,6 +445,6 @@ NY,LON,TOR;LON

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

0 comments on commit e0652d0

Please sign in to comment.