Skip to content

Commit d9d43b2

Browse files
author
root
committed
up JunkBox_Lib
1 parent 12da435 commit d9d43b2

File tree

4 files changed

+132
-55
lines changed

4 files changed

+132
-55
lines changed

BasicLib/Lib/tjson.cpp

Lines changed: 106 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
22
/**
33
@brief Tiny JSON ライブラリ
44
@file tjson.c
@@ -24,7 +24,6 @@
2424
#include "jbxl_state.h"
2525

2626

27-
2827
//////////////////////////////////////////////////////////////////////////////////////////////////////
2928
// Parser
3029
//
@@ -34,6 +33,8 @@ tJson* json_parse(char* pp, int num)
3433
3534
文字列のJSONデータを解釈して,tJsonのツリーを生成する.
3635
ツリーのトップは JSON_ANCHOR_NODE となる.
36+
シーケンス処理で書いたので,だらだら.
37+
あまり複雑なものはパースできない.たぶん.
3738
3839
@param pp 文字列の JSONデータへのポインタ.
3940
@param num 0 配列を処理しない.高速.@n
@@ -57,10 +58,6 @@ tJson* json_parse(char* pp, int num)
5758
if (*pp=='[') state = JBXL_JSON_ARRAY;
5859

5960
tJson* json = new_json_anchor_node(); // アンカー
60-
//json->ldat.id = JSON_ANCHOR_NODE;
61-
//json->ldat.lv = JSON_VALUE_OBJ;
62-
//json->state = JBXL_STATE_ANCHOR;
63-
//json->depth = -1;
6461

6562
// パース
6663
tJson* node = json_parse_prop(json, pp, num);
@@ -96,7 +93,7 @@ tJson* json_parse_prop(tJson* json, char* pp, int num)
9693
9794
JSON Main パーサ.@n
9895
先頭に姉妹ノードがない場合は json にNULLを指定しても可.@n
99-
処理に json->ctrl を使用(書き換えられる).@n
96+
処理に json->ctrl を使用(分割シーケンス処理用.プログラム中で書き換えられる).@n
10097
10198
@param json JSONデータへのポインタ.NULLでない場合は,このデータの後に結果が付加される.@n
10299
@param pp パースする文字列.
@@ -110,39 +107,41 @@ tJson* json_parse_prop(tJson* json, char* pp, int num)
110107
{
111108
char* pt = NULL;
112109
tJson* node = NULL;
110+
tJson* trgt = NULL;
111+
int valflg = OFF;
113112

114113
if (json==NULL) {
115-
json = new_json_node();
116-
json->ldat.id = JSON_TEMP_NODE;
114+
json = new_json_anchor_node();
117115
}
118116

119117
while (*pp!='\0') {
120118
//
121119
if (*pp=='{') {
120+
//print_message("open { \n");
122121
pp++;
122+
//
123123
if (json->ctrl!=JBXL_JSON_NODE_OPENED) {
124-
// open {
125-
if (json->prev==NULL) {
124+
if (valflg==OFF || json->depth<0) {
126125
node = new_json_node();
127126
node->ldat.id = JSON_BRACKET_NODE;
128127
node->ldat.lv = JSON_VALUE_OBJ;
129128
json = add_tTree_node(json, node);
130129
}
131130
else {
132-
if (json->yngr!=NULL) {
133-
json = json->yngr;
134-
json->ldat.lv = JSON_VALUE_OBJ;
135-
}
131+
json = json->yngr;
132+
json->ldat.lv = JSON_VALUE_OBJ;
136133
}
134+
valflg = OFF;
137135
}
138136
json->ctrl = JBXL_NONE;
139137

140-
// 最初の \", \', } を見つける
138+
// 次の \", \', {, } を見つける
141139
pt = pp;
140+
//while (*pt!='\0' && *pt!='\'' && *pt!='\"' && *pt!='{' && *pt!='}') pt++;
142141
while(*pt!='\0') {
143142
while(*pt=='\\') pt += 2;
144143
if (*pt!='\0') {
145-
if (*pt=='\'' || *pt=='\"' || *pt=='}') break;
144+
if (*pt=='\'' || *pt=='\"' || *pt=='{' || *pt=='}') break;
146145
pt++;
147146
}
148147
}
@@ -151,12 +150,11 @@ tJson* json_parse_prop(tJson* json, char* pp, int num)
151150
json->ctrl = JBXL_JSON_NODE_OPENED;
152151
return json;
153152
}
154-
//
155153

156154
pp = pt;
157-
if (*pp!='}') { // \" or \'
155+
if (*pp=='\"' || *pp=='\'') {
158156
char ch = '\"';
159-
if (*pt=='\'') ch = '\'';
157+
if (*pp=='\'') ch = '\'';
160158
//
161159
pt = pp + 1;
162160
while(*pt!='\0') {
@@ -177,7 +175,7 @@ tJson* json_parse_prop(tJson* json, char* pp, int num)
177175
node->ldat.key = set_Buffer(pp + 1, len);
178176
node->ldat.id = JSON_DATA_NODE;
179177
node->ldat.lv = JSON_VALUE_NULL;
180-
add_tTree_node(json, node);
178+
trgt = add_tTree_node(json, node);
181179

182180
pt = pt + 1;
183181
while(*pt!=',' && *pt!=':' && *pt!='}' && *pt!='\0') pt++;
@@ -191,29 +189,31 @@ tJson* json_parse_prop(tJson* json, char* pp, int num)
191189

192190
//
193191
else if (*pp=='[') {
192+
//print_message("open [ \n");
194193
pt = skip_char_pair(pp, '[', ']');
195194
if (*pt=='\0') {
196195
json = _json_parse_term(json, pp, pt, "[");
197196
return json;
198197
}
199-
200-
if (json->next==NULL) { // アンカーのみ
198+
if (valflg==OFF || json->depth<0) { // アンカーのみ
201199
node = new_json_node();
202200
node->ldat.id = JSON_ARRAY_NODE;
203201
node->ldat.lv = JSON_VALUE_ARRAY;
204202
add_tTree_node(json, node);
205203
}
204+
if (json->yngr!=NULL) {
205+
int len = (int)(pt - pp) + 1;
206+
Buffer temp = set_Buffer(pp, len);
207+
json->yngr->ldat.val = pack_Buffer(temp, '\0');
208+
json->yngr->ldat.lv = JSON_VALUE_ARRAY;
209+
free_Buffer(&temp);
210+
}
211+
valflg = OFF;
206212

207-
int len = (int)(pt - pp) + 1;
208-
Buffer temp = set_Buffer(pp, len);
209-
json->yngr->ldat.val = pack_Buffer(temp, '\0');
210-
json->yngr->ldat.lv = JSON_VALUE_ARRAY;
211-
free_Buffer(&temp);
212-
213-
if (num>0) _json_array_parse(json->yngr, num-1);
213+
if (num>0 && json->yngr!=NULL) _json_array_parse(json->yngr, num-1);
214214

215215
pt++;
216-
while(*pt!=',' && *pt!='}' && *pt!='{' && *pt!='[' && *pt!='\0') pt++;
216+
while(*pt!=',' && *pt!='}' && *pt!='{' && *pt!='[' && *pt!='\0') pt++;
217217
if (*pt=='\0') {
218218
if (json->depth>0) json->state = JBXL_JSON_PARSE_TERM;
219219
return json;
@@ -224,12 +224,14 @@ tJson* json_parse_prop(tJson* json, char* pp, int num)
224224

225225
//
226226
else if (*pp==',') {
227-
// 最初の \", \', } を見つける
228-
pt = pp;
227+
//print_message("next , \n");
228+
pt = pp + 1;
229+
// 次の \", \', {, } を見つける
230+
//while (*pt!='\0' && *pt!='\'' && *pt!='\"' && *pt!='{' && *pt!='}' && *pt!='[') pt++;
229231
while(*pt!='\0') {
230232
while(*pt=='\\') pt += 2;
231233
if (*pt!='\0') {
232-
if (*pt=='\'' || *pt=='\"' || *pt=='}') break;
234+
if (*pt=='\'' || *pt=='\"' || *pt=='{' || *pt=='}' || *pt=='[') break;
233235
pt++;
234236
}
235237
}
@@ -239,7 +241,7 @@ tJson* json_parse_prop(tJson* json, char* pp, int num)
239241
}
240242

241243
pp = pt;
242-
if (*pp!='}') { // \" or \'
244+
if (*pp=='\"' || *pp=='\'') {
243245
char ch = '\"';
244246
if (*pt=='\'') ch = '\'';
245247
//
@@ -261,7 +263,7 @@ tJson* json_parse_prop(tJson* json, char* pp, int num)
261263
node->ldat.key = set_Buffer(pp + 1, len);
262264
node->ldat.id = JSON_DATA_NODE;
263265
node->ldat.lv = JSON_VALUE_NULL;
264-
add_tTree_node(json, node);
266+
trgt = add_tTree_node(json, node);
265267

266268
pt = pt + 1;
267269
while(*pt!=',' && *pt!=':' && *pt!='}' && *pt!='\0') pt++;
@@ -275,8 +277,9 @@ tJson* json_parse_prop(tJson* json, char* pp, int num)
275277

276278
//
277279
else if (*pp==':') {
280+
//print_message("next : \n");
278281
pt = pp + 1;
279-
while (*pt==' ' && *pt!='\0') pt++;
282+
while (*pt==' ') pt++;
280283
if (*pt=='\0') {
281284
json = _json_parse_term(json, NULL, NULL, ":");
282285
return json;
@@ -291,7 +294,7 @@ tJson* json_parse_prop(tJson* json, char* pp, int num)
291294
pt = skip_string_end(pt);
292295
if (*pt!='\0') pt++;
293296
}
294-
while (*pt!=',' && *pt!='}' && *pt!='\0') pt++;
297+
while (*pt!=',' && *pt!='{' && *pt!='}' && *pt!='\0') pt++;
295298

296299
if (*pt=='\0') {
297300
json = _json_parse_term(json, pp, pt, ":");
@@ -302,30 +305,38 @@ tJson* json_parse_prop(tJson* json, char* pp, int num)
302305

303306
int len = (int)(pt - pp) + 1 ;
304307
Buffer temp = set_Buffer(pp, len);
305-
json->yngr->ldat.val = pack_Buffer(temp, '\0');
308+
if (trgt==NULL) trgt = json->yngr;
309+
trgt->ldat.val = pack_Buffer(temp, '\0');
306310
free_Buffer(&temp);
307311
//
308-
if (*pp=='\"' || *pp=='\'') json->yngr->ldat.lv = JSON_VALUE_STR;
312+
if (*pp=='\"' || *pp=='\'') trgt->ldat.lv = JSON_VALUE_STR;
309313
else {
310-
const char* val = (const char*)json->yngr->ldat.val.buf;
311-
if (!strcasecmp("true", val) || !strcasecmp("false", val)) json->yngr->ldat.lv = JSON_VALUE_BOOL;
312-
else json->yngr->ldat.lv = JSON_VALUE_UNRESOLV;
314+
const char* val = (const char*)trgt->ldat.val.buf;
315+
if (!strcasecmp("true", val) || !strcasecmp("false", val)) trgt->ldat.lv = JSON_VALUE_BOOL;
316+
else {
317+
int num = is_number((unsigned char*)val);
318+
if (num==1) trgt->ldat.lv = JSON_VALUE_INT;
319+
else if (num==2) trgt->ldat.lv = JSON_VALUE_REAL;
320+
else trgt->ldat.lv = JSON_VALUE_UNRESOLV;
321+
}
313322
}
314323

315324
pt++;
316325
while (*pt==' ') pt++;
317326
pp = pt;
318327
}
328+
else {
329+
valflg = ON; // ':{' or ':['
330+
}
319331
}
320332

321333
//
322334
else if (*pp=='}') {
323-
if (json->prev!=NULL) {
324-
json = json->prev;
325-
}
335+
//print_message("close } \n");
336+
if (json->prev!=NULL) json = json->prev;
337+
//
326338
pt = pp = pp + 1;
327-
//while (*pt!=',' && *pt!='}' && *pt!='{' && *pt!='\0') pt++;
328-
while (*pt!='}' && *pt!='{' && *pt!='\0') pt++;
339+
while (*pt!=',' && *pt!='}' && *pt!='{' && *pt!='\0') pt++;
329340
if (*pt=='\0' && json->depth>0) {
330341
json->state = JBXL_JSON_PARSE_TERM;
331342
return json;
@@ -401,13 +412,43 @@ tJson* json_array_parse(tJson* json, char* pp, int num)
401412
}
402413

403414
//
415+
int depth = 1;
404416
pp++;
405417
while (*pp!='\0') {
406418
while (*pp==' ') pp++;
407419
if (*pp!='\0') while (*pp=='\\') pp += 2;
408420

409421
//
410-
if (*pp==']') break;
422+
if (*pp=='[') {
423+
pt = skip_char_pair(pp, '[', ']');
424+
425+
tJson* node = new_json_node();
426+
node->ldat.id = JSON_ARRAY_NODE;
427+
node->ldat.lv = JSON_VALUE_ARRAY;
428+
add_tTree_node(json, node);
429+
//
430+
if (json->yngr!=NULL) {
431+
int len = (int)(pt - pp) + 1;
432+
Buffer temp = set_Buffer(pp, len);
433+
json->yngr->ldat.val = pack_Buffer(temp, '\0');
434+
json->yngr->ldat.lv = JSON_VALUE_ARRAY;
435+
free_Buffer(&temp);
436+
}
437+
if (num>0 && json->yngr!=NULL) _json_array_parse(json->yngr, num-1);
438+
439+
pt++;
440+
while(*pt!=',' && *pt!='}' && *pt!='{' && *pt!='[' && *pt!='\0') pt++;
441+
442+
depth++;
443+
pp = pt + 1;
444+
}
445+
446+
//
447+
else if (*pp==']') {
448+
depth--;
449+
if (depth==0) break;
450+
pp++;
451+
}
411452

412453
//
413454
else if (*pp=='\'' || *pp=='\"') {
@@ -460,10 +501,12 @@ tJson* json_array_parse(tJson* json, char* pp, int num)
460501
pp = pt + 1;
461502
}
462503

504+
//
463505
else if (*pp==',') {
464506
pp++;
465507
}
466508

509+
//
467510
else {
468511
pt = skip_chars(pp, ",}]");
469512

@@ -472,10 +515,21 @@ tJson* json_array_parse(tJson* json, char* pp, int num)
472515
Buffer temp = set_Buffer(pp, len);
473516
node->ldat.val = pack_Buffer(temp, '\0');
474517
node->ldat.id = JSON_ARRAY_VALUE_NODE;
475-
node->ldat.lv = JSON_VALUE_UNRESOLV;
518+
519+
if (*pp=='\"' || *pp=='\'') node->ldat.lv = JSON_VALUE_STR;
520+
else {
521+
const char* val = (const char*)node->ldat.val.buf;
522+
if (!strcasecmp("true", val) || !strcasecmp("false", val)) node->ldat.lv = JSON_VALUE_BOOL;
523+
else {
524+
int num = is_number((unsigned char*)val);
525+
if (num==1) node->ldat.lv = JSON_VALUE_INT;
526+
else if (num==2) node->ldat.lv = JSON_VALUE_REAL;
527+
else node->ldat.lv = JSON_VALUE_UNRESOLV;
528+
}
529+
}
530+
476531
free_Buffer(&temp);
477532
add_tTree_node(json, node);
478-
479533
pp = pt;
480534
}
481535
}
@@ -686,7 +740,7 @@ void _json_to_Buffer(tJson* pp, Buffer* buf, const char* crlf, const char* spac
686740
cat_s2Buffer(pp->ldat.key.buf, buf);
687741
cat_s2Buffer("\": ", buf);
688742
}
689-
cat_s2Buffer("[", buf);
743+
cat_s2Buffer("[", buf);
690744
if (pp->next!=NULL) {
691745
if (crlf[0]!='\0') cat_s2Buffer(crlf, buf);
692746
_json_to_Buffer(pp->next, buf, crlf, space);
@@ -1248,7 +1302,6 @@ int _json_check_node_bykey(tJson* pp, char* key, int needval, int nn)
12481302
}
12491303

12501304

1251-
12521305
/**
12531306
tList* search_all_node_strval_json(tJson* pp, char* name, char* val)
12541307

BasicLib/Lib/tjson.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ typedef tTree tJson;
158158

159159

160160

161-
162161
/////////////////////////////////////////////////////////////////////////////////////////////
163162
// Parser
164163

0 commit comments

Comments
 (0)