-
Notifications
You must be signed in to change notification settings - Fork 0
/
bsjson.h
84 lines (71 loc) · 2.34 KB
/
bsjson.h
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
/* Simple JSON implementation
*
* Copyright (C) 2014 Borislav Sapundzhiev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or (at
* your option) any later version.
*
*/
#ifndef __BSJSON_H
#define __BSJSON_H
#include "array.h"
#ifdef _WIN32
#define strdup _strdup
#endif
//#define DEBUG_JSON
#ifdef DEBUG_JSON
#define DEBUG_PRINT printf
#else
#define DEBUG_PRINT(...)
#endif
enum {JSON_NOK, JSON_OK };
enum {JSON_NONE, JSON_ROOT, JSON_OBJ, JSON_ARRAY } eNodeTypes;
#define NAME_ANON NULL
#define JSON_IS_OBJ(node)\
(node->m_type == JSON_OBJ || node->m_type == JSON_ROOT)
#define JSON_IS_ARRAY(node)\
(node->m_type == JSON_ARRAY)
struct JsonNode;
typedef char * String;
typedef struct JsonNode JsonNode;
typedef struct JsonParser JsonParser;
typedef struct JsonPair JsonPair;
struct JsonPair {
String key;
String value;
};
struct JsonNode {
int m_type;
String m_name;
JsonNode * m_parent;
cpo_array_t *m_pairs;
cpo_array_t *m_childs;
};
struct JsonParser {
JsonNode *m_root;
cpo_array_t *m_nodeStack;
String m_errorString;
};
JsonNode *JsonParser_parse(JsonParser *parser, const char * json);
JsonNode *JsonParser_parseFile(JsonParser *parser, const char *fileName);
String JsonParser_getErrorString(JsonParser *parser);
//Create root node
JsonNode *JsonNode_Create();
JsonNode *JsonNode_createChild(JsonNode *node, String name, int type);
JsonNode *JsonNode_createObject(JsonNode * node, String name);
JsonNode *JsonNode_createArray(JsonNode * node, String name);
JsonNode * JsonNode_findChild(JsonNode *node, const String name, int type);
JsonPair * JsonNode_findPair(JsonNode *node, const String key);
void JsonNode_setPair(JsonNode *node, const String key, const String value );
asize_t JsonNode_getChildCount(JsonNode * node);
asize_t JsonNode_getPairCount(JsonNode *node);
JsonNode * JsonNode_getChild(JsonNode *node, asize_t index);
JsonPair * JsonNode_getPair(JsonNode *node, asize_t index);
String JsonNode_getPairValue(JsonNode *node, const String key);
int JsonNode_getPairValueInt(JsonNode *node, const String key);
void JsonNode_delete(JsonNode *node);
void JsonNode_deleteTree(JsonNode *root);
String JsonNode_getJSON(JsonNode *node);
#endif //__BSJSON_H