CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
Loading...
Searching...
No Matches
jsmn.h
Go to the documentation of this file.
1
5#ifndef __JSMN_H_
6#define __JSMN_H_
7
8#include <stddef.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12#include <vector>
13#define JSMN_PARENT_LINKS
14
15#include "Utils.h"
16
17#ifdef __cplusplus
18extern "C" {
19#endif
20
35
36enum jsmnerr {
37 /* Not enough tokens were provided */
39 /* Invalid character inside JSON string */
41 /* The string is not a full JSON packet, more bytes expected */
43};
44
51typedef struct {
53 int start;
54 int end;
55 int size;
56#ifdef JSMN_PARENT_LINKS
57 int parent;
58#endif
59} jsmntok_t;
60
65typedef struct {
66 unsigned int pos; /* offset in the JSON string */
67 unsigned int toknext; /* next token to allocate */
68 int toksuper; /* superior token node, e.g parent object or array */
70
74void jsmn_init(jsmn_parser *parser);
75
80int jsmn_parse(jsmn_parser *parser, const char *js, size_t len,
81 jsmntok_t *tokens, unsigned int num_tokens);
82
83#ifdef __cplusplus
84}
85#endif
86
87
88int GetJSONTokenLevel(jsmntok_t *tokens, int startToken);
89
90const char* GetJSONValue(jsmntok_t* tokens, int tokenCount, const char* json, int token, int &valLength);
91std::string GetJSONValueString(jsmntok_t* tokens, int tokenCount, const char* json, int token);
92int64 GetJSONValueInt64(jsmntok_t* tokens, int tokenCount, const char* json, int token);
93uint64 GetJSONValueUint64(jsmntok_t* tokens, int tokenCount, const char* json, int token);
94float64 GetJSONValueFloat64(jsmntok_t* tokens, int tokenCount, const char* json, int token);
95
96int GetJSONToken(jsmntok_t* tokens, int tokenCount, const char* json, const char* key, int parent, jsmntype_t type = JSMN_UNDEFINED);
97
98const char* GetJSONValue(jsmntok_t* tokens, int tokenCount, const char* json, const char* key, jsmntype_t type, int &valLength, int level = 0, int instance = 0);
99std::string GetJSONValueString(jsmntok_t* tokens, int tokenCount, const char* json, const char* key, jsmntype_t type = JSMN_UNDEFINED, int level = 0, int instance = 0);
100int64 GetJSONValueInt64(jsmntok_t* tokens, int tokenCount, const char* json, const char* key, jsmntype_t type = JSMN_UNDEFINED, int level = 0, int instance = 0);
101uint64 GetJSONValueUint64(jsmntok_t* tokens, int tokenCount, const char* json, const char* key, jsmntype_t type = JSMN_UNDEFINED, int level = 0, int instance = 0);
102float64 GetJSONValueFloat64(jsmntok_t* tokens, int tokenCount, const char* json, const char* key, jsmntype_t type = JSMN_UNDEFINED, int level = 0, int instance = 0);
103
104const char* GetJSONChildValue(jsmntok_t* tokens, int tokenCount, const char* json, const char* key, int parent, jsmntype_t type, int &valLength);
105std::string GetJSONChildValueString(jsmntok_t* tokens, int tokenCount, const char* json, const char* key, int parent, jsmntype_t type = JSMN_UNDEFINED);
106int64 GetJSONChildValueInt64(jsmntok_t* tokens, int tokenCount, const char* json, const char* key, int parent, jsmntype_t type = JSMN_UNDEFINED);
107uint64 GetJSONChildValueUint64(jsmntok_t* tokens, int tokenCount, const char* json, const char* key, int parent, jsmntype_t type = JSMN_UNDEFINED);
108float64 GetJSONChildValueFloat64(jsmntok_t* tokens, int tokenCount, const char* json, const char* key, int parent, jsmntype_t type = JSMN_UNDEFINED);
109
110std::vector<int> GetJSONArrayIndexes(jsmntok_t* tokens, int tokenCount, const char* json, const char* key, jsmntype_t type = JSMN_UNDEFINED, int level = 0, int instance = 0);
111std::vector<int> GetJSONChildArrayIndexes(jsmntok_t* tokens, int tokenCount, const char* json, const char* key, int parent, jsmntype_t type = JSMN_UNDEFINED);
112std::vector<int> GetJSONChildArrayIndexes(jsmntok_t* tokens, int tokenCount, const char* json, int parent, jsmntype_t type = JSMN_UNDEFINED);
113
114std::string AddToJSONRoot(const char* json, const char* subJSON);
115
116
117#endif /* __JSMN_H_ */
118
119// Example
120//
121//#include <stdio.h>
122//#include <stdlib.h>
123//#include <string.h>
124//#include "../jsmn.h"
125//
127// * A small example of jsmn parsing when JSON structure is known and number of
128// * tokens is predictable.
129// */
130//
131//const char *JSON_STRING =
132// "{\"user\": \"johndoe\", \"admin\": false, \"uid\": 1000,\n "
133// "\"groups\": [\"users\", \"wheel\", \"audio\", \"video\"]}";
134//
135//static int jsoneq(const char *json, jsmntok_t *tok, const char *s) {
136// if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start &&
137// strncmp(json + tok->start, s, tok->end - tok->start) == 0) {
138// return 0;
139// }
140// return -1;
141//}
142//
143//int main() {
144// int i;
145// int r;
146// jsmn_parser p;
147// jsmntok_t t[128]; /* We expect no more than 128 tokens */
148//
149// jsmn_init(&p);
150// r = jsmn_parse(&p, JSON_STRING, strlen(JSON_STRING), t, sizeof(t)/sizeof(t[0]));
151// if (r < 0) {
152// printf("Failed to parse JSON: %d\n", r);
153// return 1;
154// }
155//
156// /* Assume the top-level element is an object */
157// if (r < 1 || t[0].type != JSMN_OBJECT) {
158// printf("Object expected\n");
159// return 1;
160// }
161//
162// /* Loop over all keys of the root object */
163// for (i = 1; i < r; i++) {
164// if (jsoneq(JSON_STRING, &t[i], "user") == 0) {
165// /* We may use strndup() to fetch string value */
166// printf("- User: %.*s\n", t[i+1].end-t[i+1].start,
167// JSON_STRING + t[i+1].start);
168// i++;
169// } else if (jsoneq(JSON_STRING, &t[i], "admin") == 0) {
170// /* We may additionally check if the value is either "true" or "false" */
171// printf("- Admin: %.*s\n", t[i+1].end-t[i+1].start,
172// JSON_STRING + t[i+1].start);
173// i++;
174// } else if (jsoneq(JSON_STRING, &t[i], "uid") == 0) {
175// /* We may want to do strtol() here to get numeric value */
176// printf("- UID: %.*s\n", t[i+1].end-t[i+1].start,
177// JSON_STRING + t[i+1].start);
178// i++;
179// } else if (jsoneq(JSON_STRING, &t[i], "groups") == 0) {
180// int j;
181// printf("- Groups:\n");
182// if (t[i+1].type != JSMN_ARRAY) {
183// continue; /* We expect groups to be an array of strings */
184// }
185// for (j = 0; j < t[i+1].size; j++) {
186// jsmntok_t *g = &t[i+j+2];
187// printf(" * %.*s\n", g->end - g->start, JSON_STRING + g->start);
188// }
189// i += t[i+1].size + 1;
190// } else {
191// printf("Unexpected key: %.*s\n", t[i].end-t[i].start,
192// JSON_STRING + t[i].start);
193// }
194// }
195// return 0;
196//}
Cross-platform utility toolbox for CMSDK: threading, synchronization, shared memory,...
const char * GetJSONValue(jsmntok_t *tokens, int tokenCount, const char *json, int token, int &valLength)
Definition jsmn.cpp:347
jsmntype_t
JSON type identifier.
Definition jsmn.h:28
@ JSMN_PRIMITIVE
Definition jsmn.h:33
@ JSMN_OBJECT
Definition jsmn.h:30
@ JSMN_UNDEFINED
Definition jsmn.h:29
@ JSMN_ARRAY
Definition jsmn.h:31
@ JSMN_STRING
Definition jsmn.h:32
std::string GetJSONChildValueString(jsmntok_t *tokens, int tokenCount, const char *json, const char *key, int parent, jsmntype_t type=JSMN_UNDEFINED)
Definition jsmn.cpp:441
const char * GetJSONChildValue(jsmntok_t *tokens, int tokenCount, const char *json, const char *key, int parent, jsmntype_t type, int &valLength)
Definition jsmn.cpp:398
std::vector< int > GetJSONChildArrayIndexes(jsmntok_t *tokens, int tokenCount, const char *json, const char *key, int parent, jsmntype_t type=JSMN_UNDEFINED)
Definition jsmn.cpp:492
std::string GetJSONValueString(jsmntok_t *tokens, int tokenCount, const char *json, int token)
Definition jsmn.cpp:354
int GetJSONTokenLevel(jsmntok_t *tokens, int startToken)
Definition jsmn.cpp:317
uint64 GetJSONValueUint64(jsmntok_t *tokens, int tokenCount, const char *json, int token)
Definition jsmn.cpp:366
int jsmn_parse(jsmn_parser *parser, const char *js, size_t len, jsmntok_t *tokens, unsigned int num_tokens)
Run JSON parser.
Definition jsmn.cpp:156
std::vector< int > GetJSONArrayIndexes(jsmntok_t *tokens, int tokenCount, const char *json, const char *key, jsmntype_t type=JSMN_UNDEFINED, int level=0, int instance=0)
Definition jsmn.cpp:464
uint64 GetJSONChildValueUint64(jsmntok_t *tokens, int tokenCount, const char *json, const char *key, int parent, jsmntype_t type=JSMN_UNDEFINED)
Definition jsmn.cpp:453
void jsmn_init(jsmn_parser *parser)
Create JSON parser over an array of tokens.
Definition jsmn.cpp:311
std::string AddToJSONRoot(const char *json, const char *subJSON)
Definition jsmn.cpp:539
float64 GetJSONChildValueFloat64(jsmntok_t *tokens, int tokenCount, const char *json, const char *key, int parent, jsmntype_t type=JSMN_UNDEFINED)
Definition jsmn.cpp:457
int64 GetJSONChildValueInt64(jsmntok_t *tokens, int tokenCount, const char *json, const char *key, int parent, jsmntype_t type=JSMN_UNDEFINED)
Definition jsmn.cpp:449
int64 GetJSONValueInt64(jsmntok_t *tokens, int tokenCount, const char *json, int token)
Definition jsmn.cpp:362
float64 GetJSONValueFloat64(jsmntok_t *tokens, int tokenCount, const char *json, int token)
Definition jsmn.cpp:370
int GetJSONToken(jsmntok_t *tokens, int tokenCount, const char *json, const char *key, int parent, jsmntype_t type=JSMN_UNDEFINED)
Definition jsmn.cpp:328
jsmnerr
Definition jsmn.h:36
@ JSMN_ERROR_INVAL
Definition jsmn.h:40
@ JSMN_ERROR_PART
Definition jsmn.h:42
@ JSMN_ERROR_NOMEM
Definition jsmn.h:38
JSON parser.
Definition jsmn.h:65
unsigned int pos
Definition jsmn.h:66
int toksuper
Definition jsmn.h:68
unsigned int toknext
Definition jsmn.h:67
JSON token description.
Definition jsmn.h:51
int start
Definition jsmn.h:53
int parent
Definition jsmn.h:57
int size
Definition jsmn.h:55
int end
Definition jsmn.h:54
jsmntype_t type
Definition jsmn.h:52