2
3
4
5
21
22
23
24
25template<
typename input_type =
const char*>
30 JsoNeat(input_type json, jsmntok_t *tok,
unsigned tok_max) :
31 m_json(json), m_tok_heap_alloc(
nullptr), m_tok(tok), m_tok_max(tok_max), m_nmb_tok(do_parse(json)) {
35 JsoNeat(input_type json,
unsigned tok_max) :
40 delete[] m_tok_heap_alloc;
43 operator
bool()
const {
44 return m_json && m_nmb_tok > 0;
48
49
50
51
57
58
59
60
61
62
63
64
65
68 using iterator_category = std::forward_iterator_tag;
69 using difference_type = std::ptrdiff_t;
75 Iterator(pointer ptr, container_type &container) :
76 m_ptr(ptr), m_container(container) {
82
83
84
85
86
88 const unsigned slen = strlen(s);
90 if (m_ptr->type != type)
93 if (slen != m_ptr->end - m_ptr->start)
96 if (strncmp(m_container.get_json() + m_ptr->start, s, slen) != 0)
103
104
105
107 return value_st_equal(
"null", JSMN_PRIMITIVE);
111
112
113
115 return value_st_equal(
"false", JSMN_PRIMITIVE);
119
120
121
123 return value_st_equal(
"true", JSMN_PRIMITIVE);
127
128
130 return m_container.get_json();
133
134
135
136
138 return value_st_equal(key, JSMN_STRING);
142
143
144
145
146
147 bool keyIsEqual(
const char *key, jsmntype_t val_type)
const {
148 if (val_type != m_ptr[1].type)
154
155
156
157
159 return m_ptr->type == JSMN_STRING
160 && strlen(key) < m_ptr->end - m_ptr->start
161 && strncmp(m_container.get_json() + m_ptr->start, key, strlen(key)) == 0;
165
166
167
168
169
171 if (val_type != m_ptr[1].type)
177
178
179
180
183 return m_container.get_value(dst, m_ptr);
186
187
188
189
199
200
201
202
203 template<size_t size>
205 return m_container.get_value_as_string(dst, size, m_ptr);
208
209
210
211
212
214 return m_container.get_value_as_string(dst, size, m_ptr);
218
219
220
221
222
223
224
226 return m_container.get_value_as_string(m_ptr);
230
231
232
233
234
237 return keyIsEqual(key) && m_container.get_value(dst, m_ptr + 1);
240
241
242
243
244
247 if (!getValue(dst, key))
254
255
256
257
258
259
260
261 template<
typename T, std::size_t N>
263 if (!keyIsEqual(key, JSMN_ARRAY))
269 auto count = it->size;
271 for (
int i = 0; i < count && i < N; ++i) {
272 if (!m_container.get_value(dst[i], m_ptr))
280
281
282
283
284
287 if (!keyIsEqual(key, JSMN_OBJECT))
290 return dst._from_json(*
this);
294
295
296
297
298
299
300
301 template<
typename T, size_t N>
303 if (!keyIsEqual(key, JSMN_ARRAY))
309 auto count = it->size;
311 for (
int i = 0; i < count && i < N; ++i) {
312 if (!dst[i]._from_json(it))
319
320
321
322
323
324
325
326 template<
typename T, size_t N>
328 if (!keyIsEqual(key, JSMN_ARRAY))
334 auto count = it->size;
336 for (
int i = 0; i < count && i < N; ++i) {
337 if (!dst[i]._from_json(it))
345 reference operator*()
const {
348 pointer operator->() {
385 reference operator[](
int idx) {
389 operator
bool()
const {
390 return m_ptr >= &m_container.m_tok[0] && &m_container.m_tok[0] + m_container.m_nmb_tok > m_ptr;
394 return a.m_ptr == b.m_ptr;
398 return a.m_ptr != b.m_ptr;
402 return a.m_ptr < b.m_ptr;
406 return a.m_ptr > b.m_ptr;
411
412
413
414
420 if (it->type == JSMN_OBJECT) {
421 auto count = it->size;
422 for (++it; count > 0 && it; --count) {
424 assert(it->type == JSMN_STRING);
430 if (it->type == JSMN_ARRAY) {
431 auto count = it->size;
432 for (++it; count > 0 && it; --count) {
443
444
445
446
456
457
458
459
462 assert(it->type == JSMN_STRING);
469 container_type &m_container;
473
474
475
476
478 return Iterator(&m_tok[0], *
this);
481
482
483
484
486 return Iterator(&m_tok[m_nmb_tok], *
this);
490 int do_parse(
const char *json) {
497 int r = jsmn_parse(&parser, json, strlen(json), m_tok, m_tok_max);
501 if (r < 1 || m_tok[0].type != JSMN_OBJECT) {
507
508
509
510
511
512
513
514 char* copy_string(
char *buf, size_t buf_size, pointer ptr)
const {
515 size_t str_length = ptr->end - ptr->start;
516 if (buf_size <= str_length)
518 memcpy(buf, m_json + ptr->start, str_length);
519 buf[str_length] =
'\0';
524
525
526
527
528
529
530 bool get_value(
float &dst, pointer ptr)
const {
532 if (ptr->type == JSMN_PRIMITIVE && copy_string(buf,
sizeof buf, ptr)) {
540
541
542
543
544
545
546 bool get_value(
bool &dst, pointer ptr)
const {
548 if (ptr->type == JSMN_PRIMITIVE && copy_string(buf,
sizeof buf, ptr)) {
549 if (0 == strcmp(buf,
"true") || 0 == strcmp(buf,
"1")) {
553 if (0 == strcmp(buf,
"false") || 0 == strcmp(buf,
"0")) {
562
563
564
565
566
567
568 bool get_value(std::string &dst, pointer ptr)
const {
569 if (ptr->type == JSMN_STRING) {
570 size_t str_length = ptr->end - ptr->start;
571 dst = std::string(ptr->start, str_length);
578
579
580
581
582
583
584
585
586
588 bool get_value(T &dst, pointer ptr)
const {
590 if (ptr->type == JSMN_PRIMITIVE && copy_string(buf,
sizeof buf, ptr)) {
591 dst =
static_cast<T>(atol(buf));
598
599
600
601
602
603
604
605 template<size_t size>
606 bool get_value(
char (&dst)[size], pointer ptr)
const {
607 if (ptr->type == JSMN_STRING && copy_string(dst, size, ptr)) {
614
615
616
617
618
619
620
621 bool get_value_as_string(
char *dst, size_t size, pointer ptr)
const {
622 if ((ptr->type == JSMN_STRING || ptr->type == JSMN_PRIMITIVE) && copy_string(dst, size, ptr)) {
629
630
631
632
633
634
635
636 char* get_value_as_string(pointer ptr) {
637 if (ptr->type != JSMN_STRING && ptr->type != JSMN_PRIMITIVE)
640 m_json[ptr->end] =
'\0';
641 return m_json + ptr->start;
646 jsmntok_t *m_tok_heap_alloc;
654
655
656
657
658
659template<size_t JSON_MAX_TOKENS,
typename input_type =
const char*>
662 JsoNeat_fs(input_type json) :
663 JsoNeat<input_type>(json, &m_tok_arr[0], JSON_MAX_TOKENS) {
666 jsmntok_t m_tok_arr[JSON_MAX_TOKENS];
675
676
677
678
679
680
681
682template<
int MaxTokens = 32,
typename T,
typename S>
690 auto it = jsmn.begin();
692 return obj._from_json(it);
696
697
698
702
703
704
705
707 key(json_key), val(value) {
715#define _JSONEAT_COUNT_N(_1, _2, _3, _4, _5, _6, _7,_8, _9, _10, N, ...) N
716#define _JSONEAT_COUNT(...) _JSONEAT_COUNT_N( __VA_ARGS__, 10
, 9
, 8
, 7
, 6
, 5
,4
, 3
, 2
, 1
)
727#define _JSONEAT_BUILD_CHAIN_INC( CT, ...) _JSONEAT_BUILD_CHAIN_ ## CT (__VA_ARGS__)
729#define _JSONEAT_KvPairs_ACT(x) jsoneat::KvPair(#x, x)
732
733
734
This has iterator functionality, but also provides lots of token related member functions.
Definition jsoneat.hh:66
bool getValue(T &dst) const
Get value.
Definition jsoneat.hh:182
bool keyStartsWith(const char *key) const
test if key matches
Definition jsoneat.hh:158
bool skip_key_and_value()
skip this key/value pair (in and object)
Definition jsoneat.hh:447
bool value_equals_true() const
test for value being boolean true
Definition jsoneat.hh:122
bool keyIsEqual(const char *key, jsmntype_t val_type) const
test if both key and value-type matches
Definition jsoneat.hh:147
bool getValueAsString(char *dst, size_t size) const
Get value as string instead of number or boolean.
Definition jsoneat.hh:213
bool keyIsEqual(const char *key) const
test if key matches
Definition jsoneat.hh:137
bool takeValue(T &dst, const char *key)
Get value of key/value pair and advance iterator.
Definition jsoneat.hh:246
char * getValueAsString() const
Get value as string null terminated string (in place)
Definition jsoneat.hh:225
bool getValueAsString(char(&dst)[size]) const
Get value as string instead of number or boolean.
Definition jsoneat.hh:204
bool keyStartsWith(const char *key, jsmntype_t val_type) const
test if both key and value-type matches
Definition jsoneat.hh:170
bool takeObject(T &dst, const char *key)
Get object of key/value pair and advance iterator.
Definition jsoneat.hh:286
bool skip_value()
skip this value (of an array)
Definition jsoneat.hh:415
bool getValue(T &dst, const char *key) const
Get value of key/value pair if key.
Definition jsoneat.hh:236
bool takeObjectArray(std::array< T, N > &dst, const char *key)
Get objects from array and advance iterator.
Definition jsoneat.hh:327
bool takeValueArray(T(&dst)[N], const char *key)
Get values from array and advance iterator.
Definition jsoneat.hh:262
bool skip_key()
skip this key
Definition jsoneat.hh:460
bool value_st_equal(const char *s, jsmntype_t type) const
Test type and character string of value for being equal.
Definition jsoneat.hh:87
bool value_equals_false() const
test for value being boolean false
Definition jsoneat.hh:114
bool takeValue(T &dst)
Get value of key/value pair and advance iterator.
Definition jsoneat.hh:191
bool takeObjectArray(T(&dst)[N], const char *key)
Get objects from array and advance iterator.
Definition jsoneat.hh:302
bool value_equals_null() const
test for value being null pointer
Definition jsoneat.hh:106
input_type get_json()
get json text buffer
Definition jsoneat.hh:129
Parse JSON into fixed size token array.
Definition jsoneat.hh:660
Parse JSON into tokens and iterate over it. Will handle all allocations.
Definition jsoneat.hh:26
Iterator end()
get JSON root object end iterator
Definition jsoneat.hh:485
input_type get_json()
get json buffer passed to ctor
Definition jsoneat.hh:52
Iterator begin()
get JSON root object iterator
Definition jsoneat.hh:477
#define _JSONEAT_BUILD_CHAIN_6(_1,...)
Definition jsoneat.hh:722
#define _JSONEAT_BUILD_CHAIN_1(_1)
Definition jsoneat.hh:717
#define _JSONEAT_BUILD_CHAIN(CT,...)
Definition jsoneat.hh:728
#define _JSONEAT_BUILD_CHAIN_INC(CT,...)
Definition jsoneat.hh:727
#define _JSONEAT_BUILD_CHAIN_4(_1,...)
Definition jsoneat.hh:720
#define _JSONEAT_COUNT_N(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N,...)
Definition jsoneat.hh:715
#define _JSONEAT_COUNT(...)
Definition jsoneat.hh:716
#define _JSONEAT_BUILD_CHAIN_9(_1,...)
Definition jsoneat.hh:725
#define _JSONEAT_BUILD_CHAIN_7(_1,...)
Definition jsoneat.hh:723
#define _JSONEAT_BUILD_CHAIN_8(_1,...)
Definition jsoneat.hh:724
#define _JSONEAT_BUILD_CHAIN_2(_1,...)
Definition jsoneat.hh:718
#define _JSONEAT_KvPairs_ACT(x)
Definition jsoneat.hh:729
#define _JSONEAT_BUILD_CHAIN_5(_1,...)
Definition jsoneat.hh:721
bool from_json_member(T &obj, S json)
convenient function template to de-serialize from a JSON string (FIXME: should not be in this header)
Definition jsoneat.hh:683
#define _JSONEAT_BUILD_CHAIN_3(_1,...)
Definition jsoneat.hh:719
pair of a reference and a json-key
Definition jsoneat.hh:700
KvPair(const char *json_key, D &value)
construct object according of arguments.
Definition jsoneat.hh:706