Skip to content

Commit 6c9795a

Browse files
committed
decode_json calls reflect::decode_json
1 parent c663719 commit 6c9795a

File tree

4 files changed

+40
-202
lines changed

4 files changed

+40
-202
lines changed

include/jsoncons/decode_json.hpp

Lines changed: 6 additions & 197 deletions
Original file line numberDiff line numberDiff line change
@@ -7,206 +7,15 @@
77
#ifndef JSONCONS_DECODE_JSON_HPP
88
#define JSONCONS_DECODE_JSON_HPP
99

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

2212
namespace jsoncons {
2313

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+
}
21019

21120
} // namespace jsoncons
21221

include/jsoncons/json_cursor.hpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,35 @@ class basic_json_cursor : public basic_staj_cursor<CharT>, private virtual ser_c
157157
}
158158
}
159159

160+
template <typename Sourceable>
161+
basic_json_cursor(std::allocator_arg_t, const Allocator& alloc,
162+
Sourceable&& source,
163+
const basic_json_decode_options<CharT>& options,
164+
std::error_code& ec,
165+
typename std::enable_if<!std::is_constructible<jsoncons::basic_string_view<CharT>,Sourceable>::value>::type* = 0)
166+
: source_(std::forward<Sourceable>(source)),
167+
parser_(options,default_json_parsing(),alloc)
168+
{
169+
parser_.cursor_mode(true);
170+
171+
if (!done())
172+
{
173+
std::error_code local_ec;
174+
read_next(local_ec);
175+
if (local_ec)
176+
{
177+
if (local_ec == json_errc::unexpected_eof)
178+
{
179+
done_ = true;
180+
}
181+
else
182+
{
183+
ec = local_ec;
184+
}
185+
}
186+
}
187+
}
188+
160189
template <typename Sourceable>
161190
basic_json_cursor(std::allocator_arg_t, const Allocator& alloc,
162191
Sourceable&& source,

include/jsoncons/reflect/decode_json.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ try_decode_json(const allocator_set<Allocator,TempAllocator>& alloc_set,
196196
using char_type = typename Source::value_type;
197197

198198
std::error_code ec;
199-
basic_json_cursor<char_type, Source, TempAllocator> cursor{s, options,
200-
default_json_parsing(), alloc_set.get_temp_allocator(), ec};
199+
basic_json_cursor<char_type,string_source<char_type>,TempAllocator> cursor(
200+
std::allocator_arg, alloc_set.get_temp_allocator(), s, options, default_json_parsing(), ec);
201201
if (ec)
202202
{
203203
return result_type{read_error{ec, cursor.line(), cursor.column()}};
@@ -241,8 +241,8 @@ try_decode_json(const allocator_set<Allocator,TempAllocator>& alloc_set,
241241
using result_type = read_result<value_type>;
242242

243243
std::error_code ec;
244-
basic_json_cursor<char_type,stream_source<char_type>,TempAllocator> cursor(is, options,
245-
default_json_parsing(), alloc_set.get_temp_allocator(), ec);
244+
basic_json_cursor<char_type,stream_source<char_type>,TempAllocator> cursor(
245+
std::allocator_arg, alloc_set.get_temp_allocator(), is, options, ec);
246246
if (ec)
247247
{
248248
return result_type{read_error{ec, cursor.line(), cursor.column()}};

include/jsoncons/reflect/json_conv_traits.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1496,7 +1496,7 @@ namespace variant_detail
14961496
}
14971497

14981498
template<std::size_t N,typename Json,typename Variant,typename T,typename ... U>
1499-
typename std::enable_if<N < std::variant_size_v<Variant>, Variant>::type
1499+
typename std::enable_if<N < std::variant_size_v<Variant>, conv_result<Variant>>::type
15001500
as_variant(const Json& j)
15011501
{
15021502
if (j.template is<T>())

0 commit comments

Comments
 (0)