Exjson 正在参加 2021 年度 OSC 中国开源项目评选,请投票支持!
Exjson 在 2021 年度 OSC 中国开源项目评选 中已获得 {{ projectVoteCount }} 票,请投票支持!
2021 年度 OSC 中国开源项目评选 正在火热进行中,快来投票支持你喜欢的开源项目!
2021 年度 OSC 中国开源项目评选 >>> 中场回顾
Exjson 获得 2021 年度 OSC 中国开源项目评选「最佳人气项目」 !
授权协议 LGPL
开发语言 C/C++
操作系统 跨平台
软件类型 开源软件
开源组织
地区 国产
适用人群 未知
收录时间 2019-05-10

软件简介

C语言JSON解析库:Exjson

什么是Exjson?

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

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);

示例

#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;
}
展开阅读全文

代码

的 Gitee 指数为
超过 的项目

评论

点击加入讨论🔥(11) 发布并加入讨论🔥
暂无内容
发表了博客
{{o.pubDate | formatDate}}

{{formatAllHtml(o.title)}}

{{parseInt(o.replyCount) | bigNumberTransform}}
{{parseInt(o.viewCount) | bigNumberTransform}}
没有更多内容
暂无内容
发表了问答
{{o.pubDate | formatDate}}

{{formatAllHtml(o.title)}}

{{parseInt(o.replyCount) | bigNumberTransform}}
{{parseInt(o.viewCount) | bigNumberTransform}}
没有更多内容
暂无内容
暂无内容
11 评论
59 收藏
分享
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部