-
Notifications
You must be signed in to change notification settings - Fork 0
/
cjson.h
74 lines (64 loc) · 2.08 KB
/
cjson.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
#ifndef __CJSON_H_
#define __CJSON_H_
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include <float.h>
#include <math.h>
#include <limits.h>
//标志节点类型
typedef enum NodeType {
NodeType_NUMBER = 10,
NodeType_ARRAY,
NodeType_OBJECT,
NodeType_STRING,
NodeType_TRUE,
NodeType_FALSE,
NodeType_NULL,
NOTYPE = 0
} nodetype_t;
//记录数据
typedef union DataValue{
int intNum;
double doubleNum;
char* complex;
} datavalue_t;
//json节点类型
typedef struct _cjson{
struct _cjson *prev, *next;
struct _cjson *child;
nodetype_t nodeType; //节点类型
datavalue_t value; //值
bool isReference; //是否是引用
char *keyName; //建值
bool isInt; //表示是不是整数
} Cjson;
typedef struct {
void* (*malloc_fn) (size_t size);
void (*free_fn) (void *);
} NewHook;
extern void new_hook(NewHook *hook); //初始化allocator
extern Cjson* create_simple_type_node(nodetype_t nodeType, const char * cpString); //添加除了array和object,number外其他节点的数据
extern Cjson* create_new_node(nodetype_t nodeType); //创建节点
extern Cjson* cjson_parse(const char *); //解析json函数
extern Cjson* add_next(Cjson* cur, Cjson* next); //添加下个节点
extern Cjson* deleteCjson(Cjson* out); //删除Cjson对象
extern const char* print_json(const Cjson* out); //输出json格式
//创建各种类型的节点
#define create_null_node() create_simple_type_node(NodeType_NULL, "null") //创建null节点
#define create_true_node() create_simple_type_node(NodeType_TRUE, "true") //创建true节点
#define create_false_node() create_simple_type_node(NodeType_FALSE, "false") //创建false节点
#define create_object_node() create_new_node(NodeType_OBJECT) //创建object节点
#define create_array_node() create_new_node(NodeType_ARRAY) //创建array节点
#define create_string_node() create_new_node(NodeType_STRING) //创建string节点
#define create_number_node() create_new_node(NodeType_NUMBER) //number节点
#ifdef __cplusplus
}
#endif
#endif