From 1d055261b4144dbf86b2658437015b15d4dd9bff Mon Sep 17 00:00:00 2001 From: Richard Date: Sun, 4 Sep 2022 00:32:56 +0100 Subject: initial --- include/jsoncons/decode_json.hpp | 209 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 209 insertions(+) create mode 100644 include/jsoncons/decode_json.hpp (limited to 'include/jsoncons/decode_json.hpp') diff --git a/include/jsoncons/decode_json.hpp b/include/jsoncons/decode_json.hpp new file mode 100644 index 0000000..e6ce1a1 --- /dev/null +++ b/include/jsoncons/decode_json.hpp @@ -0,0 +1,209 @@ +// Copyright 2017 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_DECODE_JSON_HPP +#define JSONCONS_DECODE_JSON_HPP + +#include +#include +#include +#include +#include // std::basic_istream +#include +#include + +namespace jsoncons { + + // decode_json + + template + typename std::enable_if::value && + type_traits::is_sequence_of::value,T>::type + decode_json(const Source& s, + const basic_json_decode_options& options = basic_json_decode_options()) + { + using char_type = typename Source::value_type; + + jsoncons::json_decoder decoder; + basic_json_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_json(const Source& s, + const basic_json_decode_options& options = basic_json_decode_options()) + { + using char_type = typename Source::value_type; + + basic_json_cursor> cursor(s, options, default_json_parsing()); + 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_json(std::basic_istream& is, + const basic_json_decode_options& options = basic_json_decode_options()) + { + jsoncons::json_decoder decoder; + basic_json_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_json(std::basic_istream& is, + const basic_json_decode_options& options = basic_json_decode_options()) + { + basic_json_cursor cursor(is, options, default_json_parsing()); + json_decoder> decoder{}; + + std::error_code ec; + T val = decode_traits::decode(cursor, decoder, ec); + if (ec) + { + JSONCONS_THROW(ser_error(ec, cursor.line(), cursor.column())); + } + return val; + } + + template + typename std::enable_if::value,T>::type + decode_json(InputIt first, InputIt last, + const basic_json_decode_options::value_type>& options = + basic_json_decode_options::value_type>()) + { + using char_type = typename std::iterator_traits::value_type; + + jsoncons::json_decoder decoder; + basic_json_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_json(InputIt first, InputIt last, + const basic_json_decode_options::value_type>& options = + basic_json_decode_options::value_type>()) + { + using char_type = typename std::iterator_traits::value_type; + + basic_json_cursor> cursor(iterator_source(first, last), options, default_json_parsing()); + jsoncons::json_decoder> decoder; + std::error_code ec; + T val = decode_traits::decode(cursor, decoder, ec); + if (ec) + { + JSONCONS_THROW(ser_error(ec, cursor.line(), cursor.column())); + } + return val; + } + + // With leading allocator parameter + + template + typename std::enable_if::value && + type_traits::is_sequence_of::value,T>::type + decode_json(temp_allocator_arg_t, const TempAllocator& temp_alloc, + const Source& s, + const basic_json_decode_options& options = basic_json_decode_options()) + { + using char_type = typename Source::value_type; + + json_decoder decoder(temp_alloc); + + basic_json_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_json(temp_allocator_arg_t, const TempAllocator& temp_alloc, + const Source& s, + const basic_json_decode_options& options = basic_json_decode_options()) + { + using char_type = typename Source::value_type; + + basic_json_cursor,TempAllocator> cursor(s, options, default_json_parsing(), temp_alloc); + json_decoder,TempAllocator> decoder(result_allocator_arg, 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_json(temp_allocator_arg_t, const TempAllocator& temp_alloc, + std::basic_istream& is, + const basic_json_decode_options& options = basic_json_decode_options()) + { + json_decoder decoder(temp_alloc); + + basic_json_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_json(temp_allocator_arg_t, const TempAllocator& temp_alloc, + std::basic_istream& is, + const basic_json_decode_options& options = basic_json_decode_options()) + { + basic_json_cursor,TempAllocator> cursor(is, options, default_json_parsing(), temp_alloc); + json_decoder,TempAllocator> decoder(result_allocator_arg, 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; + } + +} // jsoncons + +#endif + -- cgit v1.2.3