|
7 | 7 | #ifndef JSONCONS_DECODE_JSON_HPP
|
8 | 8 | #define JSONCONS_DECODE_JSON_HPP
|
9 | 9 |
|
10 |
| -#include <iostream> |
11 |
| -#include <istream> // std::basic_istream |
12 |
| -#include <tuple> |
13 |
| - |
14 |
| -#include <jsoncons/config/compiler_support.hpp> |
15 |
| -#include <jsoncons/allocator_set.hpp> |
16 |
| -#include <jsoncons/conv_error.hpp> |
17 |
| -#include <jsoncons/decode_traits.hpp> |
18 |
| -#include <jsoncons/json_cursor.hpp> |
19 |
| -#include <jsoncons/basic_json.hpp> |
20 |
| -#include <jsoncons/source.hpp> |
| 10 | +#include <jsoncons/reflect/decode_json.hpp> |
21 | 11 |
|
22 | 12 | namespace jsoncons {
|
23 | 13 |
|
24 |
| - // decode_json |
25 |
| - |
26 |
| - template <typename T,typename Source> |
27 |
| - typename std::enable_if<ext_traits::is_basic_json<T>::value && |
28 |
| - ext_traits::is_sequence_of<Source,typename T::char_type>::value,T>::type |
29 |
| - decode_json(const Source& s, |
30 |
| - const basic_json_decode_options<typename Source::value_type>& options = basic_json_decode_options<typename Source::value_type>()) |
31 |
| - { |
32 |
| - using char_type = typename Source::value_type; |
33 |
| - |
34 |
| - jsoncons::json_decoder<T> decoder; |
35 |
| - basic_json_reader<char_type, string_source<char_type>> reader(s, decoder, options); |
36 |
| - reader.read(); |
37 |
| - if (!decoder.is_valid()) |
38 |
| - { |
39 |
| - JSONCONS_THROW(ser_error(conv_errc::conversion_failed, reader.line(), reader.column())); |
40 |
| - } |
41 |
| - return decoder.get_result(); |
42 |
| - } |
43 |
| - |
44 |
| - template <typename T,typename Source> |
45 |
| - typename std::enable_if<!ext_traits::is_basic_json<T>::value && |
46 |
| - ext_traits::is_char_sequence<Source>::value,T>::type |
47 |
| - decode_json(const Source& s, |
48 |
| - const basic_json_decode_options<typename Source::value_type>& options = basic_json_decode_options<typename Source::value_type>()) |
49 |
| - { |
50 |
| - using char_type = typename Source::value_type; |
51 |
| - |
52 |
| - basic_json_cursor<char_type,string_source<char_type>> cursor(s, options, default_json_parsing()); |
53 |
| - jsoncons::json_decoder<basic_json<char_type>> decoder; |
54 |
| - std::error_code ec; |
55 |
| - T val = decode_traits<T,char_type>::decode(cursor, decoder, ec); |
56 |
| - if (JSONCONS_UNLIKELY(ec)) |
57 |
| - { |
58 |
| - JSONCONS_THROW(ser_error(ec, cursor.context().line(), cursor.context().column())); |
59 |
| - } |
60 |
| - return val; |
61 |
| - } |
62 |
| - |
63 |
| - template <typename T,typename CharT> |
64 |
| - typename std::enable_if<ext_traits::is_basic_json<T>::value,T>::type |
65 |
| - decode_json(std::basic_istream<CharT>& is, |
66 |
| - const basic_json_decode_options<CharT>& options = basic_json_decode_options<CharT>()) |
67 |
| - { |
68 |
| - jsoncons::json_decoder<T> decoder; |
69 |
| - basic_json_reader<CharT, stream_source<CharT>> reader(is, decoder, options); |
70 |
| - reader.read(); |
71 |
| - if (!decoder.is_valid()) |
72 |
| - { |
73 |
| - JSONCONS_THROW(ser_error(conv_errc::conversion_failed, reader.line(), reader.column())); |
74 |
| - } |
75 |
| - return decoder.get_result(); |
76 |
| - } |
77 |
| - |
78 |
| - template <typename T,typename CharT> |
79 |
| - typename std::enable_if<!ext_traits::is_basic_json<T>::value,T>::type |
80 |
| - decode_json(std::basic_istream<CharT>& is, |
81 |
| - const basic_json_decode_options<CharT>& options = basic_json_decode_options<CharT>()) |
82 |
| - { |
83 |
| - basic_json_cursor<CharT> cursor(is, options, default_json_parsing()); |
84 |
| - json_decoder<basic_json<CharT>> decoder{}; |
85 |
| - |
86 |
| - std::error_code ec; |
87 |
| - T val = decode_traits<T,CharT>::decode(cursor, decoder, ec); |
88 |
| - if (JSONCONS_UNLIKELY(ec)) |
89 |
| - { |
90 |
| - JSONCONS_THROW(ser_error(ec, cursor.line(), cursor.column())); |
91 |
| - } |
92 |
| - return val; |
93 |
| - } |
94 |
| - |
95 |
| - template <typename T,typename InputIt> |
96 |
| - typename std::enable_if<ext_traits::is_basic_json<T>::value,T>::type |
97 |
| - decode_json(InputIt first, InputIt last, |
98 |
| - const basic_json_decode_options<typename std::iterator_traits<InputIt>::value_type>& options = |
99 |
| - basic_json_decode_options<typename std::iterator_traits<InputIt>::value_type>()) |
100 |
| - { |
101 |
| - using char_type = typename std::iterator_traits<InputIt>::value_type; |
102 |
| - |
103 |
| - jsoncons::json_decoder<T> decoder; |
104 |
| - basic_json_reader<char_type, iterator_source<InputIt>> reader(iterator_source<InputIt>(first,last), decoder, options); |
105 |
| - reader.read(); |
106 |
| - if (!decoder.is_valid()) |
107 |
| - { |
108 |
| - JSONCONS_THROW(ser_error(conv_errc::conversion_failed, reader.line(), reader.column())); |
109 |
| - } |
110 |
| - return decoder.get_result(); |
111 |
| - } |
112 |
| - |
113 |
| - template <typename T,typename InputIt> |
114 |
| - typename std::enable_if<!ext_traits::is_basic_json<T>::value,T>::type |
115 |
| - decode_json(InputIt first, InputIt last, |
116 |
| - const basic_json_decode_options<typename std::iterator_traits<InputIt>::value_type>& options = |
117 |
| - basic_json_decode_options<typename std::iterator_traits<InputIt>::value_type>()) |
118 |
| - { |
119 |
| - using char_type = typename std::iterator_traits<InputIt>::value_type; |
120 |
| - |
121 |
| - basic_json_cursor<char_type,iterator_source<InputIt>> cursor(iterator_source<InputIt>(first, last), options, default_json_parsing()); |
122 |
| - jsoncons::json_decoder<basic_json<char_type>> decoder; |
123 |
| - std::error_code ec; |
124 |
| - T val = decode_traits<T,char_type>::decode(cursor, decoder, ec); |
125 |
| - if (JSONCONS_UNLIKELY(ec)) |
126 |
| - { |
127 |
| - JSONCONS_THROW(ser_error(ec, cursor.line(), cursor.column())); |
128 |
| - } |
129 |
| - return val; |
130 |
| - } |
131 |
| - |
132 |
| - // With leading allocator_set parameter |
133 |
| - |
134 |
| - template <typename T,typename Source,typename Allocator,typename TempAllocator > |
135 |
| - typename std::enable_if<ext_traits::is_basic_json<T>::value && |
136 |
| - ext_traits::is_sequence_of<Source,typename T::char_type>::value,T>::type |
137 |
| - decode_json(const allocator_set<Allocator,TempAllocator>& alloc_set, |
138 |
| - const Source& s, |
139 |
| - const basic_json_decode_options<typename Source::value_type>& options = basic_json_decode_options<typename Source::value_type>()) |
140 |
| - { |
141 |
| - using char_type = typename Source::value_type; |
142 |
| - |
143 |
| - json_decoder<T,TempAllocator> decoder(alloc_set.get_allocator(), alloc_set.get_temp_allocator()); |
144 |
| - |
145 |
| - basic_json_reader<char_type, string_source<char_type>,TempAllocator> reader(s, decoder, options, alloc_set.get_temp_allocator()); |
146 |
| - reader.read(); |
147 |
| - if (!decoder.is_valid()) |
148 |
| - { |
149 |
| - JSONCONS_THROW(ser_error(conv_errc::conversion_failed, reader.line(), reader.column())); |
150 |
| - } |
151 |
| - return decoder.get_result(); |
152 |
| - } |
153 |
| - |
154 |
| - template <typename T,typename Source,typename Allocator,typename TempAllocator > |
155 |
| - typename std::enable_if<!ext_traits::is_basic_json<T>::value && |
156 |
| - ext_traits::is_char_sequence<Source>::value,T>::type |
157 |
| - decode_json(const allocator_set<Allocator,TempAllocator>& alloc_set, |
158 |
| - const Source& s, |
159 |
| - const basic_json_decode_options<typename Source::value_type>& options = basic_json_decode_options<typename Source::value_type>()) |
160 |
| - { |
161 |
| - using char_type = typename Source::value_type; |
162 |
| - |
163 |
| - basic_json_cursor<char_type,string_source<char_type>,TempAllocator> cursor(s, options, default_json_parsing(), alloc_set.get_temp_allocator()); |
164 |
| - json_decoder<basic_json<char_type,sorted_policy,TempAllocator>,TempAllocator> decoder(alloc_set.get_temp_allocator(), alloc_set.get_temp_allocator()); |
165 |
| - |
166 |
| - std::error_code ec; |
167 |
| - T val = decode_traits<T,char_type>::decode(cursor, decoder, ec); |
168 |
| - if (JSONCONS_UNLIKELY(ec)) |
169 |
| - { |
170 |
| - JSONCONS_THROW(ser_error(ec, cursor.context().line(), cursor.context().column())); |
171 |
| - } |
172 |
| - return val; |
173 |
| - } |
174 |
| - |
175 |
| - template <typename T,typename CharT,typename Allocator,typename TempAllocator > |
176 |
| - typename std::enable_if<ext_traits::is_basic_json<T>::value,T>::type |
177 |
| - decode_json(const allocator_set<Allocator,TempAllocator>& alloc_set, |
178 |
| - std::basic_istream<CharT>& is, |
179 |
| - const basic_json_decode_options<CharT>& options = basic_json_decode_options<CharT>()) |
180 |
| - { |
181 |
| - json_decoder<T,TempAllocator> decoder(alloc_set.get_allocator(), alloc_set.get_temp_allocator()); |
182 |
| - |
183 |
| - basic_json_reader<CharT, stream_source<CharT>,TempAllocator> reader(is, decoder, options, alloc_set.get_temp_allocator()); |
184 |
| - reader.read(); |
185 |
| - if (!decoder.is_valid()) |
186 |
| - { |
187 |
| - JSONCONS_THROW(ser_error(conv_errc::conversion_failed, reader.line(), reader.column())); |
188 |
| - } |
189 |
| - return decoder.get_result(); |
190 |
| - } |
191 |
| - |
192 |
| - template <typename T,typename CharT,typename Allocator,typename TempAllocator > |
193 |
| - typename std::enable_if<!ext_traits::is_basic_json<T>::value,T>::type |
194 |
| - decode_json(const allocator_set<Allocator,TempAllocator>& alloc_set, |
195 |
| - std::basic_istream<CharT>& is, |
196 |
| - const basic_json_decode_options<CharT>& options = basic_json_decode_options<CharT>()) |
197 |
| - { |
198 |
| - basic_json_cursor<CharT,stream_source<CharT>,TempAllocator> cursor(is, options, default_json_parsing(), alloc_set.get_temp_allocator()); |
199 |
| - json_decoder<basic_json<CharT,sorted_policy,TempAllocator>,TempAllocator> decoder(alloc_set.get_temp_allocator(),alloc_set.get_temp_allocator()); |
200 |
| - |
201 |
| - std::error_code ec; |
202 |
| - T val = decode_traits<T,CharT>::decode(cursor, decoder, ec); |
203 |
| - if (JSONCONS_UNLIKELY(ec)) |
204 |
| - { |
205 |
| - JSONCONS_THROW(ser_error(ec, cursor.context().line(), cursor.context().column())); |
206 |
| - } |
207 |
| - return val; |
208 |
| - } |
209 |
| - |
| 14 | +template <typename T, typename... Args> |
| 15 | +T decode_json(Args&& ... args) |
| 16 | +{ |
| 17 | + return jsoncons::reflect::decode_json<T>(std::forward<Args>(args)...); |
| 18 | +} |
210 | 19 |
|
211 | 20 | } // namespace jsoncons
|
212 | 21 |
|
|
0 commit comments