From 1d055261b4144dbf86b2658437015b15d4dd9bff Mon Sep 17 00:00:00 2001 From: Richard Date: Sun, 4 Sep 2022 00:32:56 +0100 Subject: initial --- include/jsoncons_ext/csv/decode_csv.hpp | 208 ++++++++++++++++++++++++++++++++ 1 file changed, 208 insertions(+) create mode 100644 include/jsoncons_ext/csv/decode_csv.hpp (limited to 'include/jsoncons_ext/csv/decode_csv.hpp') diff --git a/include/jsoncons_ext/csv/decode_csv.hpp b/include/jsoncons_ext/csv/decode_csv.hpp new file mode 100644 index 0000000..b91c58b --- /dev/null +++ b/include/jsoncons_ext/csv/decode_csv.hpp @@ -0,0 +1,208 @@ +/// Copyright 2013 Daniel Parker +// Distributed under the Boost license, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +// See https://github.com/danielaparker/jsoncons for latest version + +#ifndef JSONCONS_CSV_DECODE_CSV_HPP +#define JSONCONS_CSV_DECODE_CSV_HPP + +#include +#include +#include +#include + +namespace jsoncons { +namespace csv { + + template + typename std::enable_if::value && + type_traits::is_sequence_of::value,T>::type + decode_csv(const Source& s, const basic_csv_decode_options& options = basic_csv_decode_options()) + { + using char_type = typename Source::value_type; + + json_decoder decoder; + + basic_csv_reader> reader(s,decoder,options); + reader.read(); + if (!decoder.is_valid()) + { + JSONCONS_THROW(ser_error(conv_errc::conversion_failed, reader.line(), reader.column())); + } + return decoder.get_result(); + } + + template + typename std::enable_if::value && + type_traits::is_char_sequence::value,T>::type + decode_csv(const Source& s, const basic_csv_decode_options& options = basic_csv_decode_options()) + { + using char_type = typename Source::value_type; + + basic_csv_cursor cursor(s, options); + jsoncons::json_decoder> decoder; + + std::error_code ec; + T val = decode_traits::decode(cursor, decoder, ec); + if (ec) + { + JSONCONS_THROW(ser_error(ec, cursor.context().line(), cursor.context().column())); + } + return val; + } + + template + typename std::enable_if::value,T>::type + decode_csv(std::basic_istream& is, const basic_csv_decode_options& options = basic_csv_decode_options()) + { + using char_type = CharT; + + json_decoder decoder; + + basic_csv_reader> reader(is,decoder,options); + reader.read(); + if (!decoder.is_valid()) + { + JSONCONS_THROW(ser_error(conv_errc::conversion_failed, reader.line(), reader.column())); + } + return decoder.get_result(); + } + + template + typename std::enable_if::value,T>::type + decode_csv(std::basic_istream& is, const basic_csv_decode_options& options = basic_csv_decode_options()) + { + basic_csv_cursor cursor(is, options); + jsoncons::json_decoder> decoder; + + std::error_code ec; + T val = decode_traits::decode(cursor, decoder, ec); + if (ec) + { + JSONCONS_THROW(ser_error(ec, cursor.context().line(), cursor.context().column())); + } + return val; + } + + template + typename std::enable_if::value,T>::type + decode_csv(InputIt first, InputIt last, + const basic_csv_decode_options::value_type>& options = + basic_csv_decode_options::value_type>()) + { + using char_type = typename std::iterator_traits::value_type; + + jsoncons::json_decoder decoder; + basic_csv_reader> reader(iterator_source(first,last), decoder, options); + reader.read(); + if (!decoder.is_valid()) + { + JSONCONS_THROW(ser_error(conv_errc::conversion_failed, reader.line(), reader.column())); + } + return decoder.get_result(); + } + + template + typename std::enable_if::value,T>::type + decode_csv(InputIt first, InputIt last, + const basic_csv_decode_options::value_type>& options = + basic_csv_decode_options::value_type>()) + { + using char_type = typename std::iterator_traits::value_type; + + basic_csv_cursor> cursor(iterator_source(first, last), options); + jsoncons::json_decoder> decoder; + std::error_code ec; + T val = decode_traits::decode(cursor, decoder, ec); + if (ec) + { + JSONCONS_THROW(ser_error(ec, cursor.context().line(), cursor.context().column())); + } + return val; + } + + // With leading allocator parameter + + template + typename std::enable_if::value && + type_traits::is_sequence_of::value,T>::type + decode_csv(temp_allocator_arg_t, const TempAllocator& temp_alloc, + const Source& s, + const basic_csv_decode_options& options = basic_csv_decode_options()) + { + using char_type = typename Source::value_type; + + json_decoder decoder(temp_alloc); + + basic_csv_reader,TempAllocator> reader(s,decoder,options,temp_alloc); + reader.read(); + if (!decoder.is_valid()) + { + JSONCONS_THROW(ser_error(conv_errc::conversion_failed, reader.line(), reader.column())); + } + return decoder.get_result(); + } + + template + typename std::enable_if::value && + type_traits::is_char_sequence::value,T>::type + decode_csv(temp_allocator_arg_t, const TempAllocator& temp_alloc, + const Source& s, + const basic_csv_decode_options& options = basic_csv_decode_options()) + { + using char_type = typename Source::value_type; + + basic_csv_cursor,TempAllocator> cursor(s, options, temp_alloc); + json_decoder,TempAllocator> decoder(temp_alloc, temp_alloc); + + std::error_code ec; + T val = decode_traits::decode(cursor, decoder, ec); + if (ec) + { + JSONCONS_THROW(ser_error(ec, cursor.context().line(), cursor.context().column())); + } + return val; + } + + template + typename std::enable_if::value,T>::type + decode_csv(temp_allocator_arg_t, const TempAllocator& temp_alloc, + std::basic_istream& is, + const basic_csv_decode_options& options = basic_csv_decode_options()) + { + using char_type = CharT; + + json_decoder decoder(temp_alloc); + + basic_csv_reader,TempAllocator> reader(is,decoder,options,temp_alloc); + reader.read(); + if (!decoder.is_valid()) + { + JSONCONS_THROW(ser_error(conv_errc::conversion_failed, reader.line(), reader.column())); + } + return decoder.get_result(); + } + + template + typename std::enable_if::value,T>::type + decode_csv(temp_allocator_arg_t, const TempAllocator& temp_alloc, + std::basic_istream& is, + const basic_csv_decode_options& options = basic_csv_decode_options()) + { + basic_csv_cursor,TempAllocator> cursor(is, options, temp_alloc); + json_decoder,TempAllocator> decoder(temp_alloc, temp_alloc); + + std::error_code ec; + T val = decode_traits::decode(cursor, decoder, ec); + if (ec) + { + JSONCONS_THROW(ser_error(ec, cursor.context().line(), cursor.context().column())); + } + return val; + } + +} // namespace csv +} // namespace jsoncons + +#endif -- cgit v1.2.3