From 179d71b8947bd3bcdaae1328f5ff25cc653ad2dc Mon Sep 17 00:00:00 2001 From: Daniel Parker Date: Thu, 23 Jan 2025 12:50:50 -0500 Subject: [PATCH] csv_encoder use json_pointer --- include/jsoncons_ext/csv/csv_encoder.hpp | 17 +++++++------- .../jsoncons_ext/jsonpointer/jsonpointer.hpp | 22 +++++++++++++++++++ test/csv/src/csv_encoder_tests.cpp | 20 ++++++++--------- 3 files changed, 41 insertions(+), 18 deletions(-) diff --git a/include/jsoncons_ext/csv/csv_encoder.hpp b/include/jsoncons_ext/csv/csv_encoder.hpp index 0487bbeb4..47180b0cd 100644 --- a/include/jsoncons_ext/csv/csv_encoder.hpp +++ b/include/jsoncons_ext/csv/csv_encoder.hpp @@ -37,10 +37,11 @@ class basic_csv_encoder final : public basic_json_visitor using allocator_type = Allocator; using char_allocator_type = typename std::allocator_traits:: template rebind_alloc; using string_type = std::basic_string, char_allocator_type>; - using json_pointer_type = jsonpointer::basic_json_pointer; + using jpointer_type = jsonpointer::basic_json_pointer; using string_allocator_type = typename std::allocator_traits:: template rebind_alloc; - using json_pointer_allocator_type = typename std::allocator_traits:: template rebind_alloc; + using jpointer_allocator_type = typename std::allocator_traits:: template rebind_alloc; using string_string_allocator_type = typename std::allocator_traits:: template rebind_alloc>; + using jpointer_string_allocator_type = typename std::allocator_traits:: template rebind_alloc>; using string_vector_allocator_type = typename std::allocator_traits:: template rebind_alloc>>; using column_type = std::vector; using column_path_column_map_type = std::unordered_map,std::equal_to,string_vector_allocator_type>; @@ -126,8 +127,8 @@ class basic_csv_encoder final : public basic_json_visitor jsoncons::detail::write_double fp_; std::vector column_names_; - std::vector column_paths_; - std::unordered_map,std::equal_to,string_string_allocator_type> column_path_name_map_; + std::vector column_paths_; + std::unordered_map,std::equal_to,jpointer_string_allocator_type> column_path_name_map_; std::unordered_map,std::equal_to,string_string_allocator_type> column_path_value_map_; column_path_column_map_type column_path_column_map_; @@ -342,7 +343,7 @@ class basic_csv_encoder final : public basic_json_visitor 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) @@ -381,7 +382,7 @@ class basic_csv_encoder final : public basic_json_visitor 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) @@ -603,7 +604,7 @@ class basic_csv_encoder final : public basic_json_visitor 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) @@ -650,7 +651,7 @@ class basic_csv_encoder final : public basic_json_visitor 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) diff --git a/include/jsoncons_ext/jsonpointer/jsonpointer.hpp b/include/jsoncons_ext/jsonpointer/jsonpointer.hpp index 7a390e5c1..25e00bae5 100644 --- a/include/jsoncons_ext/jsonpointer/jsonpointer.hpp +++ b/include/jsoncons_ext/jsonpointer/jsonpointer.hpp @@ -1443,4 +1443,26 @@ namespace jsoncons { namespace jsonpointer { } // namespace jsonpointer } // namespace jsoncons +namespace std { + template + struct hash> + { + std::size_t operator()(const jsoncons::jsonpointer::basic_json_pointer& 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 diff --git a/test/csv/src/csv_encoder_tests.cpp b/test/csv/src/csv_encoder_tests.cpp index b473f2a22..ffef04982 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 with column mappings") { -#if 0 +//#if 0 SECTION("array of objects to csv") { std::string expected = R"(Number,Date Time @@ -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 @@ -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 @@ -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 @@ -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 @@ -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") {