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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <jsonparse.h>
void substr_token (char *json, char *temp, jsmntok_t *t) {
memcpy(temp, json+t->start, t->end - t->start);
temp[t->end-t->start] = '\0';
}
void json_parse_array(char *json, void *temp, jsmntok_t *t, int r, char type) {
for (int i=0; i<r; i++) {
if (t[i].type == JSMN_PRIMITIVE) {
char val[32]; substr_token(json, val, t+i);
if (type == 'i') {
int *p = (int*)(intptr_t)temp+sizeof(int)*i;
*p = atoi(val);
}
if (type == 'c') {
int8_t *p = (int8_t*)(intptr_t)temp+sizeof(int8_t)*i;
*p = atoi(val);
}
}
}
}
void json_dump_array (char *str, void *arr, int len, char type) {
int cur = 0;
sprintf(str+cur, "[ "); cur = strlen(str);
for (int i=0; i<len; i++) {
if (type == 'i') {
sprintf(str+cur, "%d", (int)(intptr_t)(arr+i*sizeof(int)));
cur = strlen(str);
}
if (type == 'c') {
printf("%d\n", ((int8_t*)(intptr_t)(arr))[i]);
sprintf(str+cur, "%d", ((int8_t*)(intptr_t)(arr))[i]);
cur = strlen(str);
}
if (i < len-1) {
sprintf(str+cur, ", "); cur = strlen(str);
}
}
sprintf(str+cur, " ]");
}
int json_parse_subtokens (char *json, jsmntok_t *t, int r, int i) {
int rt = 0;
for (int j=i; j<r; j++) {
if (t[j].start < t[i].end) { rt ++; }
else { break; }
}
return rt;
}
/* DEPRECATED TRIALS, delete after backup
// parses a <str, int> pair set with a set of already parsed tokens
int json_parse_dict_ci_init (char *json,
pair_ci *dict, jsmntok_t *t, int r) {
int obj_i = 0, dict_i = -1;
for (int i=0; i<r; i++) {
if (t[i].type == JSMN_OBJECT) {
dict_i ++;
obj_i = 0;
}
if (t[i].type == JSMN_STRING) {
if (obj_i == 0) {
substr_token(json, dict[dict_i].key, t+i);
} else if (obj_i == 1) {
char ts[13]; substr_token(json, ts, t+i);
dict[dict_i].i = atoi(ts);
}
obj_i++;
}
}
return dict_i+1;
}
// parses a <str, int> pair set with a set of already parsed tokens
int json_parse_dict_cf_init (char *json,
pair_cf *dict, jsmntok_t *t, int r) {
int obj_i = 0, dict_i = -1;
for (int i=0; i<r; i++) {
if (t[i].type == JSMN_OBJECT) {
dict_i ++;
obj_i = 0;
}
if (t[i].type == JSMN_STRING) {
if (obj_i == 0) {
substr_token(json, dict[dict_i].key, t+i);
} else if (obj_i == 1) {
char ts[13]; substr_token(json, ts, t+i);
dict[dict_i].i = atof(ts);
}
obj_i++;
}
}
return dict_i+1;
}
// parses a <str, int> pair set
int json_parse_dict_ci (char *json, pair_ci *dict) {
jsmn_parser p; jsmn_init(&p);
jsmntok_t t[MAXTOKENS];
int r = jsmn_parse(&p, json, strlen(json), t, MAXTOKENS);
json_parse_dict_ci_init(json, dict, t, r);
}
// parses a <str, float> pair set
int json_parse_dict_cf (char *json, pair_cf *dict) {
jsmn_parser p; jsmn_init(&p);
jsmntok_t t[MAXTOKENS];
int r = jsmn_parse(&p, json, strlen(json), t, MAXTOKENS);
json_parse_dict_cf_init(json, dict, t, r);
}
// parses a list of objects which are lists of pairs <str, int>
int json_parse_list_ci (char *json, list_ci *list) {
jsmn_parser p; jsmn_init(&p);
jsmntok_t t[MAXTOKENS];
int r = jsmn_parse(&p, json, strlen(json), t, MAXTOKENS);
int list_i = 0;
for (int i=1; i<r; i++) { // i=1: ignore outer []
if (t[i].type == JSMN_ARRAY) {
int rt = 0;
for (int j=i; j<r; j++) {
if (t[j].start < t[i].end) { rt ++; }
else { break; }
}
int len = json_parse_dict_ci_init(
json, list[list_i].pairs, t+i, rt);
list[list_i].len = len;
list_i ++;
i += rt-1;
}
}
return list_i;
}
// parses a list of objects which are lists of pairs <str, float>
int json_parse_list_cf (char *json, list_cf *list) {
jsmn_parser p; jsmn_init(&p);
jsmntok_t t[MAXTOKENS];
int r = jsmn_parse(&p, json, strlen(json), t, MAXTOKENS);
int list_i = 0;
for (int i=1; i<r; i++) { // i=1: ignore outer []
if (t[i].type == JSMN_ARRAY) {
int rt = 0;
for (int j=i; j<r; j++) {
if (t[j].start < t[i].end) { rt ++; }
else { break; }
}
int len = json_parse_dict_cf_init(
json, list[list_i].pairs, t+i, rt);
list[list_i].len = len;
list_i ++;
i += rt-1;
}
}
return list_i;
}
*/
|