Virgil IoT KIT
json_parser.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2015, Marvell International Ltd.
3  * All Rights Reserved.
4  */
5 
121 #ifndef __JSON_PARSER_H__
122 #define __JSON_PARSER_H__
123 
124 #define MOD_JSON 7
125 
126 #include <stdbool.h>
127 #include <stdint.h>
128 #include "jsmn.h"
129 
130 #ifdef __cplusplus
131 namespace VirgilIoTKit {
132 extern "C" {
133 #endif
134 
135 #define MOD_ERROR_START(x) (x << 12 | 0)
136 
137 #ifdef JSMN_SHORT_TOKENS
138 typedef short jsmnindex_t;
139 typedef unsigned char jsmnenumtype_t;
140 #else
141 typedef int jsmnindex_t;
142 typedef jsmntype_t jsmnenumtype_t;
143 #endif
144 
145 #define VS_JSON_ERR_OK 0
146 
172 };
173 
179 typedef jsmntok_t jsontok_t;
180 /*
181  * JSON parser Object.
182  *
183  * This is for internal use only.
184  * This should not be used by application code
185  */
186 typedef jsmn_parser json_parser_t;
193 typedef struct {
197  char *js;
204 } jobj_t;
205 
239 int
240 json_init(jobj_t *jobj, jsontok_t *tokens, int num_tokens, char *js, size_t js_len);
241 
274 int
275 json_parse_start(jobj_t *jobj, char *js, size_t js_len);
276 
285 void
286 json_parse_stop(jobj_t *jobj);
287 
295 bool
296 json_is_object(jobj_t *jobj);
297 
305 bool
306 json_is_array(jobj_t *jobj);
325 int
326 json_get_val_bool(jobj_t *jobj, char *key, bool *value);
327 
346 int
347 json_get_val_int(jobj_t *jobj, char *key, int *value);
348 
367 int
368 json_get_val_int64(jobj_t *jobj, char *key, int64_t *value);
369 
388 int
389 json_get_val_float(jobj_t *jobj, char *key, float *value);
390 
411 int
412 json_get_val_str(jobj_t *jobj, char *key, char *value, int max_len);
413 
434 int
435 json_get_val_str_len(jobj_t *jobj, char *key, int *len);
436 
456 int
457 json_get_composite_object(jobj_t *jobj, char *key);
458 
471 int
473 
494 int
495 json_get_array_object(jobj_t *jobj, char *key, int *num_elements);
496 
509 int
511 
521 int
523 
542 int
543 json_array_get_bool(jobj_t *jobj, uint16_t index, bool *value);
544 
563 int
564 json_array_get_int(jobj_t *jobj, uint16_t index, int *value);
565 
584 int
585 json_array_get_int64(jobj_t *jobj, uint16_t index, int64_t *value);
586 
605 int
606 json_array_get_float(jobj_t *jobj, uint16_t index, float *value);
607 
628 int
629 json_array_get_str(jobj_t *jobj, uint16_t index, char *value, int maxlen);
630 
651 int
652 json_array_get_str_len(jobj_t *jobj, uint16_t index, int *len);
653 
675 int
676 json_array_get_composite_object(jobj_t *jobj, uint16_t index);
677 
690 int
692 
714 int
715 json_array_get_array_object(jobj_t *jobj, uint16_t index, int *num_elements);
716 
729 int
731 
732 #ifdef __cplusplus
733 } // extern "C"
734 } // namespace VirgilIoTKit
735 #endif
736 
737 #endif /* __JSON_PARSER_H__ */
jobj_t::num_tokens
int num_tokens
Number of tokens available for parser.
Definition: json_parser.h:203
WM_E_JSON_INVAL
@ WM_E_JSON_INVAL
Invalid characters in JSON.
Definition: json_parser.h:155
WM_E_JSON_ERRNO_BASE
@ WM_E_JSON_ERRNO_BASE
Definition: json_parser.h:149
json_array_get_bool
int json_array_get_bool(jobj_t *jobj, uint16_t index, bool *value)
Get JSON bool value from array.
jobj_t::js
char * js
Pointer to JSON string.
Definition: json_parser.h:197
json_array_get_composite_object
int json_array_get_composite_object(jobj_t *jobj, uint16_t index)
Get JSON composite object from array.
json_is_array
bool json_is_array(jobj_t *jobj)
Find out if the current object is a JSON Array.
wm_json_errno
wm_json_errno
Json Error Codes.
Definition: json_parser.h:148
json_parse_stop
void json_parse_stop(jobj_t *jobj)
Stop JSON Parsing.
json_array_get_float
int json_array_get_float(jobj_t *jobj, uint16_t index, float *value)
Get JSON float value from array.
WM_E_JSON_INVALID_JOBJ
@ WM_E_JSON_INVALID_JOBJ
Invalid JSON object.
Definition: json_parser.h:159
json_get_val_str_len
int json_get_val_str_len(jobj_t *jobj, char *key, int *len)
Get JSON string length.
WM_E_JSON_FAIL
@ WM_E_JSON_FAIL
Generic JSON parse failure.
Definition: json_parser.h:151
jobj_t::parser
json_parser_t parser
Internal JSON parser object.
Definition: json_parser.h:195
json_get_val_bool
int json_get_val_bool(jobj_t *jobj, char *key, bool *value)
Get JSON bool value.
jsmnenumtype_t
jsmntype_t jsmnenumtype_t
Definition: json_parser.h:142
jobj_t
Object used by the JSON parser internally.
Definition: json_parser.h:193
json_is_object
bool json_is_object(jobj_t *jobj)
Find out if the current object is a JSON Object.
json_parser_t
jsmn_parser json_parser_t
Definition: json_parser.h:186
jsontok_t
jsmntok_t jsontok_t
JSON token.
Definition: json_parser.h:179
WM_E_JSON_INVALID_INDEX
@ WM_E_JSON_INVALID_INDEX
Invalid array index given.
Definition: json_parser.h:165
WM_E_JSON_INCOMPLETE
@ WM_E_JSON_INCOMPLETE
Incomplete JSON string.
Definition: json_parser.h:157
WM_E_JSON_INVALID_TYPE
@ WM_E_JSON_INVALID_TYPE
JSON element of invalid type.
Definition: json_parser.h:163
json_array_get_int
int json_array_get_int(jobj_t *jobj, uint16_t index, int *value)
Get JSON integer value from array.
MOD_JSON
#define MOD_JSON
Definition: json_parser.h:124
json_array_get_str
int json_array_get_str(jobj_t *jobj, uint16_t index, char *value, int maxlen)
Get JSON string value from array.
WM_E_JSON_OBUF
@ WM_E_JSON_OBUF
JSON buffer overflow.
Definition: json_parser.h:171
json_parse_start
int json_parse_start(jobj_t *jobj, char *js, size_t js_len)
Start JSON Parsing.
jobj_t::cur
jsontok_t * cur
Pointer to current active JSON token.
Definition: json_parser.h:201
json_get_val_str
int json_get_val_str(jobj_t *jobj, char *key, char *value, int max_len)
Get JSON string value.
WM_E_JSON_INVALID_JARRAY
@ WM_E_JSON_INVALID_JARRAY
Invalid search array provided.
Definition: json_parser.h:167
json_get_val_int
int json_get_val_int(jobj_t *jobj, char *key, int *value)
Get JSON integer value.
json_get_val_int64
int json_get_val_int64(jobj_t *jobj, char *key, int64_t *value)
Get 64bit JSON integer value.
json_array_release_array_object
int json_array_release_array_object(jobj_t *jobj)
Release a JSON array object from an array.
json_array_get_array_object
int json_array_get_array_object(jobj_t *jobj, uint16_t index, int *num_elements)
Get JSON array object from array.
json_release_composite_object
int json_release_composite_object(jobj_t *jobj)
Release a JSON composite object.
jsmnindex_t
int jsmnindex_t
Definition: json_parser.h:141
json_get_composite_object
int json_get_composite_object(jobj_t *jobj, char *key)
Get JSON composite object.
json_init
int json_init(jobj_t *jobj, jsontok_t *tokens, int num_tokens, char *js, size_t js_len)
Initialize the JSON Parser and parse the given JSON string.
json_get_val_float
int json_get_val_float(jobj_t *jobj, char *key, float *value)
Get JSON float value.
json_release_array_object
int json_release_array_object(jobj_t *jobj)
Release a JSON array object.
jobj_t::tokens
jsontok_t * tokens
Pointer to JSON tokens array.
Definition: json_parser.h:199
MOD_ERROR_START
#define MOD_ERROR_START(x)
Definition: json_parser.h:135
WM_E_JSON_NOMEM
@ WM_E_JSON_NOMEM
Insufficient memory to hold the results.
Definition: json_parser.h:153
json_array_release_composite_object
int json_array_release_composite_object(jobj_t *jobj)
Release a JSON composite object from an array.
json_array_get_str_len
int json_array_get_str_len(jobj_t *jobj, uint16_t index, int *len)
Get JSON string length from array.
WM_E_JSON_NOT_FOUND
@ WM_E_JSON_NOT_FOUND
JSON element not found.
Definition: json_parser.h:161
json_array_get_num_elements
int json_array_get_num_elements(jobj_t *jobj)
Get number of elements in an array.
WM_E_JSON_INVSTART
@ WM_E_JSON_INVSTART
Invalid json start.
Definition: json_parser.h:169
json_get_array_object
int json_get_array_object(jobj_t *jobj, char *key, int *num_elements)
Get JSON array object.
json_array_get_int64
int json_array_get_int64(jobj_t *jobj, uint16_t index, int64_t *value)
Get 64bit JSON integer value from array.