Skip to content

Commit

Permalink
csv_encoder use json_pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
danielaparker committed Jan 23, 2025
1 parent 595264f commit 179d71b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
17 changes: 9 additions & 8 deletions include/jsoncons_ext/csv/csv_encoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ class basic_csv_encoder final : public basic_json_visitor<CharT>
using allocator_type = Allocator;
using char_allocator_type = typename std::allocator_traits<allocator_type>:: template rebind_alloc<CharT>;
using string_type = std::basic_string<CharT, std::char_traits<CharT>, char_allocator_type>;
using json_pointer_type = jsonpointer::basic_json_pointer<char_type>;
using jpointer_type = jsonpointer::basic_json_pointer<char_type>;
using string_allocator_type = typename std::allocator_traits<allocator_type>:: template rebind_alloc<string_type>;
using json_pointer_allocator_type = typename std::allocator_traits<allocator_type>:: template rebind_alloc<json_pointer_type>;
using jpointer_allocator_type = typename std::allocator_traits<allocator_type>:: template rebind_alloc<jpointer_type>;
using string_string_allocator_type = typename std::allocator_traits<allocator_type>:: template rebind_alloc<std::pair<const string_type,string_type>>;
using jpointer_string_allocator_type = typename std::allocator_traits<allocator_type>:: template rebind_alloc<std::pair<const jpointer_type,string_type>>;
using string_vector_allocator_type = typename std::allocator_traits<allocator_type>:: template rebind_alloc<std::pair<const string_type, std::vector<string_type, string_allocator_type>>>;
using column_type = std::vector<string_type, string_allocator_type>;
using column_path_column_map_type = std::unordered_map<string_type, column_type, std::hash<string_type>,std::equal_to<string_type>,string_vector_allocator_type>;
Expand Down Expand Up @@ -126,8 +127,8 @@ class basic_csv_encoder final : public basic_json_visitor<CharT>
jsoncons::detail::write_double fp_;

std::vector<string_type,string_allocator_type> column_names_;
std::vector<json_pointer_type,json_pointer_allocator_type> column_paths_;
std::unordered_map<string_type,string_type, std::hash<string_type>,std::equal_to<string_type>,string_string_allocator_type> column_path_name_map_;
std::vector<jpointer_type,jpointer_allocator_type> column_paths_;
std::unordered_map<jpointer_type,string_type, std::hash<jpointer_type>,std::equal_to<jpointer_type>,jpointer_string_allocator_type> column_path_name_map_;
std::unordered_map<string_type,string_type, std::hash<string_type>,std::equal_to<string_type>,string_string_allocator_type> column_path_value_map_;
column_path_column_map_type column_path_column_map_;

Expand Down Expand Up @@ -342,7 +343,7 @@ class basic_csv_encoder final : public basic_json_visitor<CharT>
bool first = true;
for (std::size_t i = 0; i < column_paths_.size(); ++i)
{
auto it = column_path_name_map_.find(column_paths_[i].string());
auto it = column_path_name_map_.find(column_paths_[i]);
if (it != column_path_name_map_.end())
{
if (!first)
Expand Down Expand Up @@ -381,7 +382,7 @@ class basic_csv_encoder final : public basic_json_visitor<CharT>
bool first = true;
for (std::size_t i = 0; i < column_paths_.size(); ++i)
{
auto it = column_path_name_map_.find(column_paths_[i].string());
auto it = column_path_name_map_.find(column_paths_[i]);
if (it != column_path_name_map_.end())
{
if (!first)
Expand Down Expand Up @@ -603,7 +604,7 @@ class basic_csv_encoder final : public basic_json_visitor<CharT>
std::size_t col = 0;
for (std::size_t i = 0; i < column_paths_.size(); ++i)
{
auto it = column_path_name_map_.find(column_paths_[i].string());
auto it = column_path_name_map_.find(column_paths_[i]);
if (it != column_path_name_map_.end())
{
if (col > 0)
Expand Down Expand Up @@ -650,7 +651,7 @@ class basic_csv_encoder final : public basic_json_visitor<CharT>
std::size_t col = 0;
for (std::size_t i = 0; i < column_paths_.size(); ++i)
{
auto it = column_path_name_map_.find(column_paths_[i].string());
auto it = column_path_name_map_.find(column_paths_[i]);
if (it != column_path_name_map_.end())
{
if (col > 0)
Expand Down
22 changes: 22 additions & 0 deletions include/jsoncons_ext/jsonpointer/jsonpointer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1443,4 +1443,26 @@ namespace jsoncons { namespace jsonpointer {
} // namespace jsonpointer
} // namespace jsoncons

namespace std {
template <typename CharT>
struct hash<jsoncons::jsonpointer::basic_json_pointer<CharT>>
{
std::size_t operator()(const jsoncons::jsonpointer::basic_json_pointer<CharT>& ptr) const noexcept
{
constexpr std::uint64_t prime{0x100000001B3};
std::uint64_t result{0xcbf29ce484222325};

for (const auto& str : ptr)
{
for (std::size_t i = 0; i < str.length(); ++i)
{
result = (result * prime) ^ str[i];
}
}
return result;
}
};

} // namespace std

#endif
20 changes: 10 additions & 10 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 with column mappings")
{
#if 0
//#if 0
SECTION("array of objects to csv")
{
std::string expected = R"(Number,Date Time
Expand Down Expand Up @@ -122,12 +122,12 @@ TEST_CASE("test json to flat csv with column mappings")

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

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 @@ -405,12 +405,12 @@ NY,LON,TOR;LON
//std::cout << buf << "\n";
CHECK(expected == buf);
}
#endif
//#endif
}

TEST_CASE("test json to non-flat csv with column mappings")
{
#if 0
//#if 0
SECTION("array of objects to csv")
{
std::string expected = R"(Number,Date Time
Expand Down Expand Up @@ -523,12 +523,12 @@ TEST_CASE("test json to non-flat csv with column mappings")

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

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 @@ -719,13 +719,13 @@ NY,LON,TOR;LON

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


TEST_CASE("test json to flat csv with column names")
{
#if 0
//#if 0
SECTION("array of objects to csv")
{
std::string expected = R"(boolean,datetime,float,text
Expand Down Expand Up @@ -856,7 +856,7 @@ Chicago Sun-Times,1.27
j.dump(encoder);
CHECK(expected == buf);
}
#endif
//#endif

SECTION("object of arrays and subarrays to csv with column_names")
{
Expand Down

0 comments on commit 179d71b

Please sign in to comment.