Skip to content

Commit 6da9293

Browse files
committed
json_conv_traits
1 parent 629f693 commit 6da9293

File tree

3 files changed

+38
-27
lines changed

3 files changed

+38
-27
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.5...3.14)
1+
cmake_minimum_required(VERSION 3.15)
22

33
project(jsoncons CXX)
44

include/jsoncons/reflect/decode_json.hpp

Lines changed: 2 additions & 2 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,string_source<char_type>,TempAllocator> cursor(s, options,
200-
default_json_parsing(), alloc_set.get_temp_allocator(), ec);
199+
basic_json_cursor<char_type, Source, TempAllocator> cursor{s, options,
200+
default_json_parsing(), alloc_set.get_temp_allocator(), ec};
201201
if (ec)
202202
{
203203
return result_type{read_error{ec, cursor.line(), cursor.column()}};

include/jsoncons/reflect/decode_traits.hpp

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,23 @@
1717
#include <utility>
1818
#include <vector>
1919

20+
#include <jsoncons/allocator_set.hpp>
21+
#include <jsoncons/basic_json.hpp>
2022
#include <jsoncons/config/compiler_support.hpp>
2123
#include <jsoncons/conv_error.hpp>
24+
#include <jsoncons/decode_traits.hpp>
25+
#include <jsoncons/json_cursor.hpp>
2226
#include <jsoncons/json_exception.hpp>
2327
#include <jsoncons/json_type.hpp>
24-
#include <jsoncons/reflect/json_conv_traits.hpp>
2528
#include <jsoncons/json_visitor.hpp>
29+
#include <jsoncons/reflect/decode_json.hpp>
30+
#include <jsoncons/reflect/json_conv_traits.hpp>
31+
#include <jsoncons/reflect/read_result.hpp>
2632
#include <jsoncons/semantic_tag.hpp>
2733
#include <jsoncons/ser_context.hpp>
34+
#include <jsoncons/source.hpp>
2835
#include <jsoncons/staj_cursor.hpp>
2936
#include <jsoncons/staj_event.hpp>
30-
#include <jsoncons/reflect/read_result.hpp>
3137
#include <jsoncons/utility/more_type_traits.hpp>
3238

3339
namespace jsoncons {
@@ -184,7 +190,8 @@ struct decode_traits<T,
184190
!ext_traits::is_typed_array<T>::value
185191
>::type>
186192
{
187-
using value_type = typename T::value_type;
193+
using element_type = typename T::value_type;
194+
using value_type = T;
188195
using result_type = read_result<value_type>;
189196

190197
template <typename CharT>
@@ -205,7 +212,7 @@ struct decode_traits<T,
205212
cursor.next(ec);
206213
while (cursor.current().event_type() != staj_event_type::end_array && !ec)
207214
{
208-
auto r = decode_traits<value_type>::try_decode(cursor);
215+
auto r = decode_traits<element_type>::try_decode(cursor);
209216
if (!r)
210217
{
211218
return result_type(r.error());
@@ -225,7 +232,8 @@ struct typed_array_visitor : public default_json_visitor
225232
T& v_;
226233
int level_{0};
227234
public:
228-
using value_type = typename T::value_type;
235+
using element_type = typename T::value_type;
236+
using value_type = T;
229237
using result_type = read_result<value_type>;
230238

231239
typed_array_visitor(T& v)
@@ -278,7 +286,7 @@ struct typed_array_visitor : public default_json_visitor
278286
const ser_context&,
279287
std::error_code&) override
280288
{
281-
v_.push_back(static_cast<value_type>(value));
289+
v_.push_back(static_cast<element_type>(value));
282290
JSONCONS_VISITOR_RETURN;
283291
}
284292

@@ -287,7 +295,7 @@ struct typed_array_visitor : public default_json_visitor
287295
const ser_context&,
288296
std::error_code&) override
289297
{
290-
v_.push_back(static_cast<value_type>(value));
298+
v_.push_back(static_cast<element_type>(value));
291299
JSONCONS_VISITOR_RETURN;
292300
}
293301

@@ -296,35 +304,35 @@ struct typed_array_visitor : public default_json_visitor
296304
const ser_context&,
297305
std::error_code&) override
298306
{
299-
visit_half_(typename std::integral_constant<bool, std::is_integral<value_type>::value>::type(), value);
307+
visit_half_(typename std::integral_constant<bool, std::is_integral<element_type>::value>::type(), value);
300308
JSONCONS_VISITOR_RETURN;
301309
}
302310

303311
void visit_half_(std::true_type, uint16_t value)
304312
{
305-
v_.push_back(static_cast<value_type>(value));
313+
v_.push_back(static_cast<element_type>(value));
306314
}
307315

308316
void visit_half_(std::false_type, uint16_t value)
309317
{
310-
v_.push_back(static_cast<value_type>(binary::decode_half(value)));
318+
v_.push_back(static_cast<element_type>(binary::decode_half(value)));
311319
}
312320

313321
JSONCONS_VISITOR_RETURN_TYPE visit_double(double value,
314322
semantic_tag,
315323
const ser_context&,
316324
std::error_code&) override
317325
{
318-
v_.push_back(static_cast<value_type>(value));
326+
v_.push_back(static_cast<element_type>(value));
319327
JSONCONS_VISITOR_RETURN;
320328
}
321329

322-
JSONCONS_VISITOR_RETURN_TYPE visit_typed_array(const jsoncons::span<const value_type>& data,
330+
JSONCONS_VISITOR_RETURN_TYPE visit_typed_array(const jsoncons::span<const element_type>& data,
323331
semantic_tag,
324332
const ser_context&,
325333
std::error_code&) override
326334
{
327-
v_ = std::vector<value_type>(data.begin(),data.end());
335+
v_ = std::vector<element_type>(data.begin(),data.end());
328336
JSONCONS_VISITOR_RETURN;
329337
}
330338

@@ -348,7 +356,8 @@ struct decode_traits<T,
348356
ext_traits::is_typed_array<T>::value
349357
>::type>
350358
{
351-
using value_type = typename T::value_type;
359+
using element_type = typename T::value_type;
360+
using value_type = T;
352361
using result_type = read_result<value_type>;
353362

354363
template <typename CharT>
@@ -375,7 +384,7 @@ struct decode_traits<T,
375384
}
376385
for (auto ch : bytes)
377386
{
378-
v.push_back(static_cast<value_type>(ch));
387+
v.push_back(static_cast<element_type>(ch));
379388
}
380389
cursor.next(ec);
381390
return result_type{std::move(v)};
@@ -422,7 +431,7 @@ struct decode_traits<T,
422431
ext_traits::is_typed_array<T>::value
423432
>::type>
424433
{
425-
using value_type = typename T::value_type;
434+
using value_type = T;
426435
using result_type = read_result<value_type>;
427436

428437
template <typename CharT>
@@ -474,7 +483,8 @@ struct decode_traits<T,
474483
ext_traits::is_insertable<T>::value
475484
>::type>
476485
{
477-
using value_type = typename T::value_type;
486+
using element_type = typename T::value_type;
487+
using value_type = T;
478488
using result_type = read_result<value_type>;
479489

480490
template <typename CharT>
@@ -499,7 +509,7 @@ struct decode_traits<T,
499509
cursor.next(ec);
500510
while (cursor.current().event_type() != staj_event_type::end_array && !ec)
501511
{
502-
auto r = decode_traits<value_type>::try_decode(cursor);
512+
auto r = decode_traits<element_type>::try_decode(cursor);
503513
if (!r)
504514
{
505515
return result_type(r.error());
@@ -528,19 +538,20 @@ struct decode_traits<T,
528538
template <typename T, std::size_t N>
529539
struct decode_traits<std::array<T,N>>
530540
{
531-
using value_type = typename std::array<T,N>::value_type;
541+
using element_type = typename std::array<T,N>::value_type;
542+
using value_type = typename std::array<T,N>;
532543
using result_type = read_result<value_type>;
533544

534545
template <typename CharT>
535-
static std::array<T, N> try_decode(basic_staj_cursor<CharT>& cursor)
546+
static result_type try_decode(basic_staj_cursor<CharT>& cursor)
536547
{
537548
std::error_code ec;
538549

539550
std::array<T,N> v;
540551
cursor.array_expected(ec);
541552
if (JSONCONS_UNLIKELY(ec))
542553
{
543-
return result_type{read_error{ec, cursor.line(), cursor.column()}};
554+
return result_type(read_error{ec, cursor.line(), cursor.column()});
544555
}
545556
v.fill(T{});
546557
if (cursor.current().event_type() != staj_event_type::begin_array)
@@ -550,7 +561,7 @@ struct decode_traits<std::array<T,N>>
550561
cursor.next(ec);
551562
for (std::size_t i = 0; i < N && cursor.current().event_type() != staj_event_type::end_array && !ec; ++i)
552563
{
553-
auto r = decode_traits<value_type>::try_decode(cursor);
564+
auto r = decode_traits<element_type>::try_decode(cursor);
554565
if (!r)
555566
{
556567
return result_type(r.error());
@@ -576,7 +587,7 @@ struct decode_traits<T,
576587
>::type>
577588
{
578589
using mapped_type = typename T::mapped_type;
579-
using value_type = typename T::value_type;
590+
using value_type = T;
580591
using key_type = typename T::key_type;
581592
using result_type = read_result<value_type>;
582593

@@ -647,7 +658,7 @@ struct decode_traits<T,
647658
>::type>
648659
{
649660
using mapped_type = typename T::mapped_type;
650-
using value_type = typename T::value_type;
661+
using value_type = T;
651662
using key_type = typename T::key_type;
652663
using result_type = read_result<value_type>;
653664

0 commit comments

Comments
 (0)