/// 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