1 Star 0 Fork 36

开发团队 / Exjson

forked from Josin / Exjson 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
BSD-3-Clause

C语言JSON解析库:Exjson

什么是Exjson?

Exjson是一款高性能的基于Flex & Bison的 JSON 解析器,Xserver引擎自带,其他需要使用的只需要引入几个头文件和源文件即可

Exjson特殊符号解析规则

文本符号 符号含义 Exjson解析结果 反序列化输出
true 布尔值: 1
false 布尔值: 0
null 0

Exjson适应场合

Exjson适合于在需要在底层C语言或者C++语言层面使用 JSON 功能的场景。目前大部分接口使用的都是 JSON 格式传输,后面增加 XML解析库

注意

Exjson支持注释,Exjson中注释以 # 或者// 开头,一直延续到行尾,如下是合格的 Exjson格式

{
    "name": "Exjson", // 名字:Exjson
    "version": "1.0", # 版本号
    "platform": [ "MacOSX", "Linux", "Windows" ]
}

APIs

// 生成一个EXJSON对象
EXJSON_API EXJSON * INIT_EXJSON();

// 生成一个 string: long int 的键值对
EXJSON_API int add_object_int(EXJSON *exjson, char *key, long val);

// 生成一个 string: double 的键值对
EXJSON_API int add_object_double(EXJSON *exjson, char *key, double val);

// 生成一个 string : string 的键值对
EXJSON_API int add_object_string(EXJSON *exjson, char *key, char *val);

// 生成一个 string: {} 的键值对
EXJSON_API int add_object_object(EXJSON *exjson, char *key, void *val);

// 生成一个 string: [] 的键值对
EXJSON_API int add_object_array(EXJSON *exjson, char *key, void *val);
// 上面添加方法的一个通用函数
EXJSON_API int add_object_ptr(EXJSON *exjson, char *key, void *val, unsigned char val_type);

// 生成一个 long int 的一个数组元素
EXJSON_API int add_array_int(EXJSON *exjson, long val);

// 生成一个 double 的一个数组元素
EXJSON_API int add_array_double(EXJSON *exjson, double val);

// 生成一个 string 的一个数组元素
EXJSON_API int add_array_string(EXJSON *exjson, char *val);

// 生成一个 对象{} 的一个数组元素
EXJSON_API int add_array_object(EXJSON *exjson, void *val);

// 生成一个 数组的 的一个数组元素
EXJSON_API int add_array_array(EXJSON *exjson, void *val);
// 上面方法的通用函数
EXJSON_API int add_array_ptr(EXJSON *exjson, void *val, unsigned char val_type);

// 打印 EXJSON 信息
PRINT_EXJSON(exjson);

// 从 EXJSON结构中获取数据
EXJSON_API void *exjson_get_val_from_key(EXJSON *exjson, char *key);
EXJSON_API void *exjson_get_val_from_index(EXJSON *exjson, int index);

// 编码JSON字符串为EXJSON结构
EXJSON_API extern EXJSON *decode_json(char *json_string);
// EXJSON结构解码为JSON字符串
EXJSON_API char *encode_json(EXJSON *exjson);
// 使用完毕后,需要释放内存
EXJSON_API void destroy_exjson(EXJSON *exjson);

示例-编码Exjson

#include <stdio.h>
#include "exjson.h"


int main(int argc, char *argv[])
{
    EXJSON *exjson = INIT_EXJSON();
    
    EXJSON *array = INIT_EXJSON();
    add_array_string(array, "Exjson");
    add_array_string(array, "1.0.0");
    add_array_string(array, "Very fast");
    
    add_object_array(exjson, "exjson", array);
    
    char *str = encode_json(exjson);
    printf("%s", str);
    free(str);
    destroy_exjson(exjson);
    destroy_exjson(array);
    
    // 输出如下
    // {"exjson":["Exjson","1.0.0","Very fast"]}
    return 0;
}

示例-解码Exjson

#include <stdio.h>
#include "exjson.h"


int main(int argc, char *argv[])
{
    EXJSON *v = decode_json("{\n"
                            "    \"b\": 100, # 这个是注释\n"
                            "    \"a\":{\n"
                            "        \"a\": \"b\"\n"
                            "    }\n"
                            "}");
	// 返回的字符串因为分配了内存,需要手动释放
    char *str = encode_json(v);
    printf("%s", str);
    free(str);
    destroy_exjson(v);
    return 0;
    
    // 输出如下:
    // {"b":100,"a":{"a":"b"}}
}
BSD 3-Clause License Copyright (c) 2019, Exjson All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

简介

C语言开发的JSON解析库 展开 收起
C
BSD-3-Clause
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
C
1
https://gitee.com/OAGroup/Exjson.git
git@gitee.com:OAGroup/Exjson.git
OAGroup
Exjson
Exjson
master

搜索帮助