JunkBox_Lib  1.10.2
tjson.h
Go to the documentation of this file.
1 #ifndef __JBXL_TINY_JSON_H_
2 #define __JBXL_TINY_JSON_H_
3 
21 #include "tools.h"
22 #include "ttree.h"
23 #include "jbxl_state.h"
24 
25 
26 // ノード種別 (ldat.id)
27 #define JSON_ANCHOR_NODE JBXL_STATE_ANCHOR
28 #define JSON_TEMP_NODE -1
29 #define JSON_BRACKET_NODE 1
30 #define JSON_DATA_NODE 2
31 #define JSON_ARRAY_NODE 3
32 #define JSON_ARRAY_VALUE_NODE 4
33 
34 // 属性値の形式 (ldat.lv)
35 #define JSON_VALUE_UNRESOLV -1
36 #define JSON_VALUE_NULL 0
37 #define JSON_VALUE_INT 1
38 #define JSON_VALUE_REAL 2
39 #define JSON_VALUE_BOOL 3
40 #define JSON_VALUE_STR 4
41 #define JSON_VALUE_OBJ 5
42 #define JSON_VALUE_ARRAY 6
43 
44 #define JSON_ONELINE_FORMAT 0
45 #define JSON_CRLF_FORMAT 1
46 #define JSON_INDENT_FORMAT 2
47 
48 
50 // JSON Functions
51 
52 #define new_json_node() new_tTree_node()
53 #define new_json_anchor_node() new_tTree_anchor_node()
54 #define new_json() new_tTree_node()
55 #define new_json_anchor() new_tTree_anchor_node()
56 #define new_json_anchor_node() new_tTree_anchor_node()
57 #define del_json_anchor(t) del_tTree_anchor_node((t))
58 #define del_json_anchor_node(t) del_tTree_anchor_node((t))
59 
60 #define del_json(j) del_tTree((j))
61 #define del_all_json(j) del_all_tTree((j))
62 #define del_json_node(j) del_tTree_node((j))
63 #define free_json(j) free_tTree_node((j))
64 #define free_json_node(j) free_tTree_node((j))
65 
66 
157 typedef tTree tJson;
158 
159 
161 // Parser
162 
163 tJson* json_parse(const char* str, int num);
164 tJson* json_parse_prop (tJson* json, const char* str, int num);
165 tJson* json_parse_seq (tJson* json, const char* str, int num);
166 tJson* json_array_parse(tJson* json, const char* str, int num);
167 
168 tJson* _json_array_parse(tJson* json, int num);
169 tJson* _json_parse_term (tJson* json, const char* st, const char* ed, const char* com);
170 
171 
173 // 逆 Parser
174 
175 Buffer json_inverse_parse(tJson* pp, int mode);
176 Buffer json_inverse_parse_opt(tJson* pp, const char* crlf, const char* space);
177 void print_json(FILE* fp, tJson* json, int mode);
178 void print_json_opt(FILE* fp, tJson* json, const char* crlf, const char* space);
179 
180 void _json_to_Buffer(tJson* pp, Buffer* buf, const char* crlf, const char* space);
181 
182 #define print_tJson(f, j) print_tTree((f), (j))
183 #define print_tJson_tree(f, j, s) print_tTree_tree((f), (j), (s))
184 
185 
187 // Tools
188 
189 tJson* json_parse_file(const char* fn, int num);
190 
191 void json_set_str_val(tJson* json, const char* val);
192 void json_set_int_val(tJson* json, int val);
193 void json_set_real_val(tJson* json, float val);
194 
195 void json_copy_val (tJson* f_json, tJson* t_json);
196 void json_copy_data(tJson* f_json, tJson* t_json);
197 
198 tJson* json_insert_child(tJson* parent, tJson* child);
199 tJson* json_insert_parse(tJson* parent, const char* str);
200 
201 tJson* json_append_obj_key(tJson* json, const char* key);
202 tJson* json_append_array_key(tJson* json, const char* key);
203 
204 void json_append_obj_str_val (tJson* json, const char* key, const char* val);
205 void json_append_obj_int_val (tJson* json, const char* key, int val);
206 void json_append_obj_real_val(tJson* json, const char* key, float val);
207 
208 void json_append_array_str_val (tJson* json, const char* val);
209 void json_append_array_int_val (tJson* json, int val);
210 void json_append_array_real_val(tJson* json, float val);
211 //
212 tJson* join_json(tJson* parent, tJson** child);
213 #define dup_merge_json(p, c) dup_merge_tTree((p), (c))
214 
215 
217 // Search
218 
219 #define find_top_json(p, n) search_top_bracket_json((p), (n))
220 #define find_sister_json(p, n) search_sister_json((p), (n))
221 #define find_key_json(p, k) search_key_json((p), (k), FALSE, 1)
222 #define find_key_child_json(p, k) search_key_child_json( (p), (k), FALSE)
223 #define find_key_sister_json(p, k) search_key_sister_json((p), (k), FALSE)
224 #define find_key_json_obj(p, k) search_key_json_obj((p), (k))
225 #define find_double_key_json(p, k1, k2) search_double_key_json((p), (k1), (k2), FALSE)
226 
227 tJson* search_top_bracket_json(tJson* pp, int nn);
228 tJson* search_sister_json(tJson* pp, int nn);
229 
230 tJson* search_key_json(tJson* pp, const char* key, int needval, int nn);
231 tJson* search_key_child_json(tJson* pp, const char* key, int needval);
232 tJson* search_key_sister_json(tJson* pp, const char* key, int needval);
233 tJson* search_key_json_obj(tJson* pp, const char* key, int nn);
234 tJson* search_double_key_json(tJson* pp, const char* key1, const char* key2, int needval);
235 
236 tJson* _search_key_json(tJson* pp, const char* key, int need, int* nn);
237 tJson* _search_key_json_obj(tJson* pp, const char* key, int* nn);
238 int _json_check_node_bykey(tJson* pp, const char* key, int needval, int nn);
239 
240 Buffer get_json_val(tJson* json);
241 Buffer get_key_json_val(tJson* pp, const char* key, int nn);
242 Buffer get_key_sister_json_val(tJson* pp, const char* key);
243 Buffer get_double_key_json_val(tJson* pp, const char* key1, const char* key2);
244 
246 char* get_string_from_json(tJson* json);
247 
248 //
249 tList* search_all_node_strval_json(tJson* json, const char* name, const char* val);
250 tList* _search_all_node_strval_json(tList* list, tJson* pp, const char* name, const char* val);
251 // ex.) search_all_node_strval_json(json, "ARRAY_VALUE", "jupyterhub-j20000xx");
252 
253 
254 #endif // __JBXL_TINY_JSON_H_
255 
JunkBox_Lib 状態ヘッダ
unsigned char ** buf
Definition: jpeg_tool.h:96
Definition: buffer.h:35
tJson * json_parse_prop(tJson *json, const char *str, int num)
JSON Main Parser.json が NULL の場合は ANCHOR付き.
Definition: tjson.c:107
void json_append_array_str_val(tJson *json, const char *val)
配列 [] の要素として 文字列 val を追加する.
Definition: tjson.c:1232
Buffer get_Buffer_from_json(tJson *json)
Definition: tjson.c:1795
tJson * search_key_json(tJson *pp, const char *key, int needval, int nn)
名前(属性名)が key である nn番目のノードへのポインタを返す
Definition: tjson.c:1383
void print_json(FILE *fp, tJson *json, int mode)
tJsonデータをmodeに従って fp に出力する.
Definition: tjson.c:818
void json_append_obj_str_val(tJson *json, const char *key, const char *val)
{} の要素として "key":val(val は文字列)を追加する.
Definition: tjson.c:1155
tJson * search_top_bracket_json(tJson *pp, int nn)
ツリーが複数のルート(TOP)を持つ場合(JBXL_JSON_MULTI_ROOT),指定されたTOPへのポインタを返す.
Definition: tjson.c:1343
Buffer json_inverse_parse(tJson *pp, int mode)
tJsonデータをmodeに従って元の書式に戻して Bufferに格納する.
Definition: tjson.c:616
tJson * _search_key_json_obj(tJson *pp, const char *key, int *nn)
search_key_json_obj() の補助関数
Definition: tjson.c:1598
tJson * search_key_sister_json(tJson *pp, const char *key, int needval)
姉妹ノードで名前(属性名)が key である nn番目のノードへのポインタを返す.
Definition: tjson.c:1470
Buffer get_key_sister_json_val(tJson *pp, const char *key)
姉妹ノードで名前(属性名)が key である nn番目のノードの属性値を返す.
Definition: tjson.c:1760
tJson * json_append_obj_key(tJson *json, const char *key)
属性値(value)なしのリストデータ "key":{} を追加する.
Definition: tjson.c:1083
tJson * _search_key_json(tJson *pp, const char *key, int need, int *nn)
search_key_json() の補助関数
Definition: tjson.c:1559
int _json_check_node_bykey(tJson *pp, const char *key, int needval, int nn)
search 系関数の補助関数
Definition: tjson.c:1632
tJson * join_json(tJson *parent, tJson **child)
parent の子として child そのものを 直接繋げる.
Definition: tjson.c:1314
tJson * search_key_json_obj(tJson *pp, const char *key, int nn)
名前(属性名)が key である nn番目のオブジェクトノード(JSON_VALUE_OBJ)へのポインタを返す.ex.) "key":{}
Definition: tjson.c:1497
tJson * _json_parse_term(tJson *json, const char *st, const char *ed, const char *com)
json_parse_prop() の補助関数.断片的な入力データ用.
Definition: tjson.c:551
void json_copy_data(tJson *f_json, tJson *t_json)
f_json から t_json へ属性名(key)と属性値(value)をコピーする.
Definition: tjson.c:984
Buffer get_json_val(tJson *json)
Definition: tjson.c:1708
tJson * search_sister_json(tJson *pp, int nn)
nn個先の sister ノードを返す.正数の場合は younger sister. 負数の場合は elder sister.
Definition: tjson.c:1412
tJson * json_append_array_key(tJson *json, const char *key)
値(value)なしの配列 "key":[] を追加する.
Definition: tjson.c:1120
void _json_to_Buffer(tJson *pp, Buffer *buf, const char *crlf, const char *space)
tJsonデータを元の書式に戻して Bufferに格納する.補助関数.
Definition: tjson.c:688
tJson * json_parse(const char *str, int num)
文字列のJSONデータを解釈して,tJsonのツリーを生成する.ANCHOR付き.
Definition: tjson.c:51
void json_append_array_real_val(tJson *json, float val)
配列 [] の要素として 実数 val を追加する.
Definition: tjson.c:1285
tTree tJson
Definition: tjson.h:157
void json_append_obj_int_val(tJson *json, const char *key, int val)
{} の要素として "key":val(val は整数)を追加する.
Definition: tjson.c:1186
void json_append_obj_real_val(tJson *json, const char *key, float val)
{} の要素として "key":val(va lは実数)を追加する.
Definition: tjson.c:1209
void json_append_array_int_val(tJson *json, int val)
配列 [] の要素として 整数 val を追加する.
Definition: tjson.c:1262
tJson * search_key_child_json(tJson *pp, const char *key, int needval)
子の姉妹ノードで名前(属性名)が key である nn番目のノードへのポインタを返す.
Definition: tjson.c:1447
void json_set_real_val(tJson *json, float val)
json ノード "key":val に実数の属性値(value)を設定する.
Definition: tjson.c:941
void json_copy_val(tJson *f_json, tJson *t_json)
f_json から t_json へ属性値(value)をコピーする.
Definition: tjson.c:960
void json_set_str_val(tJson *json, const char *val)
json ノード "key":val に文字列の属性値(value)を設定する.
Definition: tjson.c:888
tList * search_all_node_strval_json(tJson *json, const char *name, const char *val)
指定した条件に合う全てのノードへのポインタを,リストに格納して返す.
Definition: tjson.c:1662
tJson * json_insert_child(tJson *parent, tJson *child)
parent に dict または array の child を繋げる.
Definition: tjson.c:1024
tJson * json_array_parse(tJson *json, const char *str, int num)
JSONデータの 配列ノードの値(配列データ)を処理する.
Definition: tjson.c:401
Buffer json_inverse_parse_opt(tJson *pp, const char *crlf, const char *space)
tJsonデータを指定に従って元の書式に戻して Bufferに格納する.
Definition: tjson.c:656
void print_json_opt(FILE *fp, tJson *json, const char *crlf, const char *space)
tJsonデータを指定に従って fp に出力する.
Definition: tjson.c:840
Buffer get_double_key_json_val(tJson *pp, const char *key1, const char *key2)
key1 -> key2 の親子関係を持つ,key2ノードの属性値を返す.
Definition: tjson.c:1780
Buffer get_key_json_val(tJson *pp, const char *key, int nn)
名前(属性名)が key である nn番目のノードの属性値を返す.
Definition: tjson.c:1739
tJson * _json_array_parse(tJson *json, int num)
json_parse_prop() の補助関数.配列処理用.
Definition: tjson.c:375
void json_set_int_val(tJson *json, int val)
json ノード "key":val に整数の属性値(value)を設定する.
Definition: tjson.c:922
tJson * json_parse_seq(tJson *json, const char *str, int num)
断片化した JSONデータを読み込んで処理する.
Definition: tjson.c:581
tList * _search_all_node_strval_json(tList *list, tJson *pp, const char *name, const char *val)
search_all_node_strval_json() の補助関数
Definition: tjson.c:1674
tJson * json_parse_file(const char *fn, int num)
JSONデータをファイルから読み込んで,パースする.
Definition: tjson.c:868
char * get_string_from_json(tJson *json)
要 free()
Definition: tjson.c:1830
tJson * json_insert_parse(tJson *parent, const char *str)
str をパースして繋げる.str は { または [ で始まる必要がある.
Definition: tjson.c:1058
tJson * search_double_key_json(tJson *pp, const char *key1, const char *key2, int needval)
属性名が key1 -> key2 の親子関係を持つ,key2ノードのポインタを返す.
Definition: tjson.c:1529
汎用ツールヘッダ
Tiny Tree Graph 構造ライブラリヘッダ