// 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_ENCODE_DECODE_JSON_HPP #define JSONCONS_ENCODE_DECODE_JSON_HPP #include #include #include #include #include // std::basic_istream #include #include namespace jsoncons { // decode_json template typename std::enable_if::value,T>::type decode_json(const std::basic_string& s, const basic_json_decode_options& options = basic_json_options::default_options()) { jsoncons::json_decoder decoder; basic_json_reader> reader(s, decoder, options); reader.read(); return decoder.get_result(); } template typename std::enable_if::value,T>::type decode_json(const std::basic_string& s, const basic_json_decode_options& options = basic_json_options::default_options()) { basic_json_cursor reader(s, options); T val; read_from(basic_json(), reader, val); return val; } template typename std::enable_if::value,T>::type decode_json(std::basic_istream& is, const basic_json_decode_options& options = basic_json_options::default_options()) { jsoncons::json_decoder decoder; basic_json_reader> reader(is, decoder, options); reader.read(); 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_options::default_options()) { basic_json_cursor reader(is, options); T val; read_from(basic_json(), reader, val); return val; } template T decode_json(const basic_json& j, const std::basic_string& s, const basic_json_decode_options& options = basic_json_options::default_options()) { basic_json_cursor reader(s, options); T val; read_from(j, reader, val); return val; } template T decode_json(const basic_json& j, std::basic_istream& is, const basic_json_decode_options& options = basic_json_options::default_options()) { basic_json_cursor reader(is, options); T val; read_from(j, reader, val); return val; } // encode_json template void encode_json(const T& val, basic_json_content_handler& receiver) { write_to(basic_json(), val, receiver); receiver.flush(); } // to stream template typename std::enable_if::value>::type encode_json(const T& val, std::basic_ostream& os, const basic_json_encode_options& options = basic_json_options::default_options(), indenting line_indent = indenting::no_indent) { if (line_indent == indenting::indent) { basic_json_encoder encoder(os, options); val.dump(encoder); } else { basic_json_compressed_encoder encoder(os, options); val.dump(encoder); } } template typename std::enable_if::value>::type encode_json(const T& val, std::basic_ostream& os, const basic_json_encode_options& options = basic_json_options::default_options(), indenting line_indent = indenting::no_indent) { if (line_indent == indenting::indent) { basic_json_encoder encoder(os, options); encode_json(val, encoder); } else { basic_json_compressed_encoder encoder(os, options); encode_json(val, encoder); } } template void encode_json(const T& val, std::basic_ostream& os, indenting line_indent) { encode_json(val, os, basic_json_options::default_options(), line_indent); } // to string template typename std::enable_if::value>::type encode_json(const T& val, std::basic_string& s, const basic_json_encode_options& options = basic_json_options::default_options(), indenting line_indent = indenting::no_indent) { if (line_indent == indenting::indent) { basic_json_encoder>> encoder(s, options); val.dump(encoder); } else { basic_json_compressed_encoder>> encoder(s, options); val.dump(encoder); } } template typename std::enable_if::value>::type encode_json(const T& val, std::basic_string& s, const basic_json_encode_options& options = basic_json_options::default_options(), indenting line_indent = indenting::no_indent) { if (line_indent == indenting::indent) { basic_json_encoder>> encoder(s, options); encode_json(val, encoder); } else { basic_json_compressed_encoder>> encoder(s, options); encode_json(val, encoder); } } template void encode_json(const T& val, std::basic_string& s, indenting line_indent) { encode_json(val, s, basic_json_options::default_options(), line_indent); } template void encode_json(const basic_json& j, const T& val, basic_json_content_handler& receiver) { write_to(j, val, receiver); receiver.flush(); } template void encode_json(const basic_json& j, const T& val, std::basic_ostream& os, const basic_json_encode_options& options = basic_json_options::default_options(), indenting line_indent = indenting::no_indent) { if (line_indent == indenting::indent) { basic_json_encoder encoder(os, options); encode_json(j, val, encoder); } else { basic_json_compressed_encoder encoder(os, options); encode_json(j, val, encoder); } } template void encode_json(const basic_json& j, const T& val, std::basic_ostream& os, indenting line_indent) { if (line_indent == indenting::indent) { basic_json_encoder encoder(os, basic_json_options::default_options()); encode_json(j, val, encoder); } else { basic_json_compressed_encoder encoder(os, basic_json_options::default_options()); encode_json(j, val, encoder); } } template void encode_json(const basic_json& j, const T& val, std::basic_string& s, const basic_json_encode_options& options = basic_json_options::default_options(), indenting line_indent = indenting::no_indent) { if (line_indent == indenting::indent) { basic_json_encoder>> encoder(s, options); encode_json(j, val, encoder); } else { basic_json_compressed_encoder>> encoder(s, options); encode_json(j, val, encoder); } } template void encode_json(const basic_json& j, const T& val, std::basic_string& s, indenting line_indent) { if (line_indent == indenting::indent) { basic_json_encoder>> encoder(s, basic_json_options::default_options()); encode_json(j, val, encoder); } else { basic_json_compressed_encoder>> encoder(s, basic_json_options::default_options()); encode_json(j, val, encoder); } } } #endif