A Simple JSON Parser developed by Pure C
(Under Development)
SeeJson is a json tool library realized with C language (C89 standard), including analyzer and builder to solve standard JSON string, supporting cross-platform and cross-compiler; the project adopts TDD for dsign including 20 APIs which can satisfy the basic use demand of JSON.
SeeJson是用纯C语言实现的一个json工具库,包含能够处理标准JSON字符串的解析器/生成器,支持跨平台和跨编译器;封装了多个常用APIs,它们可以满足基本的JSON使用需求
技术要点 Keypoint:
- 纯C语言
- 使用函数指针实现结构体成员方法,模拟类似class的效果,增强易用性(OOC 面向对象C语言)
- 递归下降分析法
- Test-Driven Development(测试驱动开发)
- 防御性编程
- 跨平台
(占位待续)
(0) 导入方法
导入SeeJSON的方法主要有两种:
- 直接引入.c和.h
- 编译为链接库
(1) 多种访问方法 (占位)
- 直接访问 (不推荐)
- getXXX系列方法
- 访问器访问 (未实现)
(2) JSON字符串解码为JSON节点(getXXX系列方法)
const char* json = "{\"name\":\"SeeJSON\"}"; /* 创建JSON字符串 */
json_node node; /* 创建JSON初始节点 */
json_init(&node); /* 初始化 */
json_decode(&node,json); /* 将字符串转换为JSON_NODE结构 */
printf("name:%s\n",getString(&node,"name")); /* 使用getValue系列方法获取键"name"对应的值 */
运行效果:
name:SeeJSON
更复杂的应用(getXXX系列方法)
有如下JSON文档,名字为"test.json"
{
"Name":"SeeJSON",
"Date":"3/16/2018",
"Object":{
"Number":123,
"String":"hello,json",
"Boolean":"True",
"Null":null,
"Array":["a1",2333,null]
}
}
使用read_json_from_file(path)读取这个文件,可以直接返回一个json_node结构,而无需再手动设置
json_node node = read_json_from_file("test.json");
按照以下方法可以访问此JSON文件的相关数据:
/* getString方法用于访问字符串类型的节点 */
/* 此系列方法还有: getNumber, getNull, getBoolean, getArray, getObject */
const char* name = getString(&node,"Name");
const char* date = getString(&node,"Date");
printf("Name:%s\n",name);
printf("Date:%s\n",date);
/* getObject方法用于访问JSON对象类型的节点 */
const json_node object = getObject(&node,"Object");
const double _number = getNumber(&object,"Number");
const int _boolean = getBoolean(&object,"Boolean");
const char* _null = getNull(&object,"Null");
const char* _str = getString(&object,"String");
printf("Number:%lf\n",_number);
printf("String:%s\n",_str);
printf("Null:%s\n",_null);
printf("Boolean:%d\n",_boolean);
/* getArray方法用于访问数组类型的节点 */
const json_node* arr = getArray(&object,"Array");
printf("Array_String:%s\n",getString(&arr[0],""));
printf("Array_Number:%lf\n",getNumber(&arr[1],""));
printf("Array_Null:%s\n",getNull(&arr[2],""));
运行效果
Name:SeeJSON
Date:3/16/2018
Number:123.000000
String:hello,json
Null:null
Boolean:0
Array_String:a1
Array_Number:2333.000000
Array_Null:null
(3) JSON节点编码为JSON字符串
/* 本例使用的节点是上面编码的那一个node */
size_t length; /* 用于记录生成的字符串的长度 */
char* str = json_encode(&node,&length); /* 解码 */
printf("string:%s\n",str); /* 输出JSON字符串 */
printf("length:%d\n",length); /* 输出字符串长度 */
运行效果:
string:{"name":"SeeJSON"}
length:18
(4) 数据结构和支持的数据类型
SeeJSON内置的数据类型有:布尔型(true,false)、空值(null)、数值型(number)、字符串型(string)、数组(array)、对象(object)
核心数据结构由结构体json_node定义:
struct json_node{
/* Value of JSON Node */
union{
/* Object */
struct{ json_member* member; size_t size; }object;
/* Array */
struct{ json_node* element; size_t size; }array;
/* String */
struct{ char* value; size_t length; }string;
/* Number */
double number;
}value;
/* Type of JSON Node */
json_type type;
/* Visit JSON Structure */
void* (*getValue)(json_node this,char* k);
};
struct json_member{
char* key;
size_t key_len;
json_node node;
};