Skip to content

Commit 1efc9de

Browse files
committed
read_typed_array
1 parent 0004d5f commit 1efc9de

2 files changed

Lines changed: 118 additions & 116 deletions

File tree

include/jsoncons/reflect/decode_traits.hpp

Lines changed: 2 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -36,118 +36,6 @@
3636
namespace jsoncons {
3737
namespace reflect {
3838

39-
template <typename T>
40-
typename std::enable_if<ext_traits::is_back_insertable<T>::value,void>::type
41-
read_typed_array(jsoncons::span<uint8_t> typed_array, typed_array_values element_type, T& v)
42-
{
43-
using value_type = typename T::value_type;
44-
45-
switch (element_type)
46-
{
47-
case typed_array_values::uint8_value:
48-
{
49-
auto ta = typed_array_cast<const uint8_t>(typed_array);
50-
for (auto item : ta)
51-
{
52-
v.push_back(static_cast<value_type>(item));
53-
}
54-
break;
55-
}
56-
case typed_array_values::uint16_value:
57-
{
58-
auto ta = typed_array_cast<const uint16_t>(typed_array);
59-
for (auto item : ta)
60-
{
61-
v.push_back(static_cast<value_type>(item));
62-
}
63-
break;
64-
}
65-
case typed_array_values::uint32_value:
66-
{
67-
auto ta = typed_array_cast<const uint32_t>(typed_array);
68-
for (auto item : ta)
69-
{
70-
v.push_back(static_cast<value_type>(item));
71-
}
72-
break;
73-
}
74-
case typed_array_values::uint64_value:
75-
{
76-
auto ta = typed_array_cast<const uint64_t>(typed_array);
77-
for (auto item : ta)
78-
{
79-
v.push_back(static_cast<value_type>(item));
80-
}
81-
break;
82-
}
83-
case typed_array_values::int8_value:
84-
{
85-
auto ta = typed_array_cast<const int8_t>(typed_array);
86-
for (auto item : ta)
87-
{
88-
v.push_back(static_cast<value_type>(item));
89-
}
90-
break;
91-
}
92-
case typed_array_values::int16_value:
93-
{
94-
auto ta = typed_array_cast<const int16_t>(typed_array);
95-
for (auto item : ta)
96-
{
97-
v.push_back(static_cast<value_type>(item));
98-
}
99-
break;
100-
}
101-
case typed_array_values::int32_value:
102-
{
103-
auto ta = typed_array_cast<const int32_t>(typed_array);
104-
for (auto item : ta)
105-
{
106-
v.push_back(static_cast<value_type>(item));
107-
}
108-
break;
109-
}
110-
case typed_array_values::int64_value:
111-
{
112-
auto ta = typed_array_cast<const int64_t>(typed_array);
113-
for (auto item : ta)
114-
{
115-
v.push_back(static_cast<value_type>(item));
116-
}
117-
break;
118-
}
119-
case typed_array_values::half_value:
120-
{
121-
auto ta = typed_array_cast<const int16_t>(typed_array);
122-
for (auto item : ta)
123-
{
124-
v.push_back(static_cast<value_type>(item));
125-
}
126-
break;
127-
}
128-
case typed_array_values::float_value:
129-
{
130-
auto ta = typed_array_cast<const float>(typed_array);
131-
for (auto item : ta)
132-
{
133-
v.push_back(static_cast<value_type>(item));
134-
}
135-
break;
136-
}
137-
case typed_array_values::double_value:
138-
{
139-
auto ta = typed_array_cast<const double>(typed_array);
140-
for (auto item : ta)
141-
{
142-
v.push_back(static_cast<value_type>(item));
143-
}
144-
break;
145-
}
146-
default:
147-
break;
148-
}
149-
}
150-
15139
// decode_traits
15240

15341
template <typename T,typename Enable = void>
@@ -423,8 +311,7 @@ struct decode_traits<T,
423311
T v = jsoncons::make_obj_using_allocator<T>(aset.get_allocator());
424312
if (cursor.is_typed_array())
425313
{
426-
read_typed_array(cursor.typed_array(), cursor.element_type(), v);
427-
cursor.to_end_array();
314+
cursor.read_typed_array(v);
428315
}
429316
else
430317
{
@@ -492,8 +379,7 @@ struct decode_traits<T,
492379
T v = jsoncons::make_obj_using_allocator<T>(aset.get_allocator());
493380
if (cursor.is_typed_array())
494381
{
495-
read_typed_array(cursor.typed_array(), cursor.element_type(), v);
496-
cursor.to_end_array();
382+
cursor.read_typed_array(v);
497383
}
498384
else
499385
{

include/jsoncons/staj_cursor.hpp

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,122 @@ class basic_staj_cursor
323323
virtual void to_end_array()
324324
{
325325
}
326+
327+
template <typename T>
328+
typename std::enable_if<ext_traits::is_back_insertable<T>::value,void>::type
329+
read_typed_array(T& v)
330+
{
331+
using value_type = typename T::value_type;
332+
333+
if (is_typed_array())
334+
{
335+
switch (element_type())
336+
{
337+
case typed_array_values::uint8_value:
338+
{
339+
auto ta = typed_array_cast<const uint8_t>(typed_array());
340+
for (auto item : ta)
341+
{
342+
v.push_back(static_cast<value_type>(item));
343+
}
344+
break;
345+
}
346+
case typed_array_values::uint16_value:
347+
{
348+
auto ta = typed_array_cast<const uint16_t>(typed_array());
349+
for (auto item : ta)
350+
{
351+
v.push_back(static_cast<value_type>(item));
352+
}
353+
break;
354+
}
355+
case typed_array_values::uint32_value:
356+
{
357+
auto ta = typed_array_cast<const uint32_t>(typed_array());
358+
for (auto item : ta)
359+
{
360+
v.push_back(static_cast<value_type>(item));
361+
}
362+
break;
363+
}
364+
case typed_array_values::uint64_value:
365+
{
366+
auto ta = typed_array_cast<const uint64_t>(typed_array());
367+
for (auto item : ta)
368+
{
369+
v.push_back(static_cast<value_type>(item));
370+
}
371+
break;
372+
}
373+
case typed_array_values::int8_value:
374+
{
375+
auto ta = typed_array_cast<const int8_t>(typed_array());
376+
for (auto item : ta)
377+
{
378+
v.push_back(static_cast<value_type>(item));
379+
}
380+
break;
381+
}
382+
case typed_array_values::int16_value:
383+
{
384+
auto ta = typed_array_cast<const int16_t>(typed_array());
385+
for (auto item : ta)
386+
{
387+
v.push_back(static_cast<value_type>(item));
388+
}
389+
break;
390+
}
391+
case typed_array_values::int32_value:
392+
{
393+
auto ta = typed_array_cast<const int32_t>(typed_array());
394+
for (auto item : ta)
395+
{
396+
v.push_back(static_cast<value_type>(item));
397+
}
398+
break;
399+
}
400+
case typed_array_values::int64_value:
401+
{
402+
auto ta = typed_array_cast<const int64_t>(typed_array());
403+
for (auto item : ta)
404+
{
405+
v.push_back(static_cast<value_type>(item));
406+
}
407+
break;
408+
}
409+
case typed_array_values::half_value:
410+
{
411+
auto ta = typed_array_cast<const int16_t>(typed_array());
412+
for (auto item : ta)
413+
{
414+
v.push_back(static_cast<value_type>(item));
415+
}
416+
break;
417+
}
418+
case typed_array_values::float_value:
419+
{
420+
auto ta = typed_array_cast<const float>(typed_array());
421+
for (auto item : ta)
422+
{
423+
v.push_back(static_cast<value_type>(item));
424+
}
425+
break;
426+
}
427+
case typed_array_values::double_value:
428+
{
429+
auto ta = typed_array_cast<const double>(typed_array());
430+
for (auto item : ta)
431+
{
432+
v.push_back(static_cast<value_type>(item));
433+
}
434+
break;
435+
}
436+
default:
437+
break;
438+
}
439+
to_end_array();
440+
}
441+
}
326442
};
327443

328444
template <typename CharT>

0 commit comments

Comments
 (0)