forked from zserge/jsmn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsmn_iterator.h
103 lines (86 loc) · 2.96 KB
/
jsmn_iterator.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#ifndef __JSMN_ITERATOR_H__
#define __JSMN_ITERATOR_H__
#include "jsmn.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* Error return codes for jsmn iterator
*/
enum {
/* Input parameter error */
JSMNITER_ERR_PARAMETER = -1,
/* JSMN index doesn't point at an Array/Object */
JSMNITER_ERR_TYPE = -2,
/* Group item misses string identifier */
JSMNITER_ERR_NOIDENT = -3,
/* Broken JSON */
JSMNITER_ERR_BROKEN = -4,
};
/**
* Struct with state information for jsmn iterator
* - When the no more items for iterator the parser_pos value will point to
* JSMN index for next object after current Array/Object
*/
typedef struct {
jsmntok_t *jsmn_tokens;
unsigned int jsmn_len;
unsigned int parent_pos;
unsigned int parser_pos;
unsigned int index;
} jsmn_iterator_t;
/**
* @brief Takes an JSMN Array/Object and locates index for last item in collection
* @details Iterates over JSMN Array/Object until last item is found
*
* @param jsmn_tokens JSMN tokens
* @param jsmn_len JSMN token count
* @param parser_pos Current JSMN token
*
* @return < 0 - Error has occured, corresponds to one of JSMNITER_ERR_*
* >=0 - JSMN index for last item in Array/Object
*/
int jsmn_iterator_find_last( jsmntok_t *jsmn_tokens, unsigned int jsmn_len, unsigned int parser_pos );
/**
* @brief Initialize iterator
* @details Set initial value for iterator struct
*
* @param iterator Iterator struct
* @param jsmn_tokens JSMN tokens
* @param jsmn_len JSMN token count
* @param parser_pos Current JSMN token
*
* @return < 0 - Error has occured, corresponds to one of JSMNITER_ERR_*
* >=0 - Ok
*/
int jsmn_iterator_init( jsmn_iterator_t *iterator, jsmntok_t *jsmn_tokens, unsigned int jsmn_len,
unsigned int parser_pos );
/**
* @brief Get next item in JSMN Array/Object
* @details Gets JSMN position for next identifier and value in Array/Object
*
* @param iterator Iterator struct
* @param jsmn_identifier Return pointer for identifier, NULL for Array
* @param jsmn_value Return pointer for value
* @param next_value_index Possible to indicate where next value begins, allows determine end of sub
* Array/Object withouth manually searching for it
*
* @return < 0 - Error has occured, corresponds to one of JSMNITER_ERR_*
* 0 - No more values
* > 0 - Value (and identifier) has been returned
*/
int jsmn_iterator_next( jsmn_iterator_t *iterator, jsmntok_t **jsmn_identifier, jsmntok_t **jsmn_value,
unsigned int next_value_index );
/**
* @brief Return current parser position
* @details For Array the parser point to current value index
* For Object the parser points to the identifier
*
* @param iterator [description]
* @return [description]
*/
#define jsmn_iterator_position(_iterator_) ((_iterator_)->parser_pos)
#ifdef __cplusplus
}
#endif
#endif /*__JSMN_ITERATOR_H__*/