12 Star 25 Fork 8

丁宁 / ajson

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

ajson

0.2.0版本要求C++11的编译器

a utility for serialize C++ and json.

动机:

为什么要设计ajson?

在工作中经常有这样的需求,获得一段json编码的文本,将它解析为dom对象(比如rapidjson)。

然后读取各节点的值,为了方便,经常将其保存到某个数据结构。

然后这个dom就不需要了,这里dom的解析和构建就感觉到了浪费。

也有反过来的情况,将一个C++结构体,序列化到dom,然后将dom再次序列化到文本。

再次,感觉到了dom的浪费,能跳过dom这层吗?

这时,我希望能有一个像javascript那样方便的将json文本到数据结构直接方便的转换。

于是ajson就诞生了。

简介

------------------------------------------------------------------

struct Person
{

  std::string  Name;
  
  int          Age;
  
};

AJSON(Person , Name , Age)

Person obj;

char * json= "{	\"Name\" : \"Boo\",	\"Age\" : 28}";

ajson::load_from_buff(obj,json);

以上代码解释了什么是AJSON,AJSON还支持stl的顺序容器。

再来个复杂点的例子

enum PhoneType

{

  MOBILE = 0,

  HOME = 1,

  WORK = 2

};

struct PhoneNumber

{
  std::string number;

  PhoneType   type = MOBILE;

  PhoneNumber(std::string const& number_  = "", PhoneType type_ = MOBILE)

    :number(number_),type(type_)

  {}

};

struct Person

{

  std::string name;

  int32_t     id;

  std::string mail;

  std::vector<PhoneNumber> phones;

  Person(std::string name_  = "" , int32_t id_  = 0, std::string mail_ = "")

    :name(name_),id(id_),mail(mail_)

  {}

};

struct AddressBook

{

  std::vector<Person> peoples;

};

AJSON(PhoneNumber, number, type)

AJSON(Person, name, id, mail, phones)

AJSON(AddressBook, peoples)

结构体支持嵌套,stl容器也支持嵌套。


特点

ajson反序列化扫描的时候,直接将字面量类型同时解析,并将结果直接存入对应的数据结构字段。

如此一来就不需要临时的DOM,减少了中间处理的工作以及内存的申请释放,大大提升了性能。

使用方式简单,只要定义相应的宏AJSON,便可方便的序列化/反序列化操作。

依赖小,完全不依赖第三方库,只有一个头文件,省去了编译的麻烦。

要点

如果json内部附带了转义符,ajson会改动原始文本来避免新申请内存。

如果不希望原始文本被修改,请创建一份新的拷贝。

由于ajson全是内存操作,文件读取操作时,将会全部读入内存,所以不适合大文件操作。

其他

为什么要选择json而不是xml?

因为json的数据描述更符合C++的数据描述, 举个例子,xml有Node和Attribute两种方式, 给出一个C++的Struct的Member field, 应该对应那种类型呢,如果是写入, 那么写成Node呢还是Attribute呢, 这里面对一个二义性问题。


AJSON完全免费,没有任何限制,使用boost的授权方式。

0.2.2版本

修改原先的内存修改策略,改为不修改源数据内存, 老的方式重新声明文件为ajson_msd.hpp, 习惯老方式的用户可以沿用, 新的方式会有轻微的性能降低,但是使用方式的限制取消了

0.2.1版本

去掉了宏定义中的v.语法,感谢奏之章的建议 注意:由于该改动,ajson不能使用在final修饰的class上

0.2.0版本

相对于0.1.x版本,ajson没有功能上的增加,主要的改动是去掉了对rapidjson和boost的依赖,完全独立了。

Boost Software License - Version 1.0 - August 17th, 2003 Permission is hereby granted, free of charge, to any person or organization obtaining a copy of the software and accompanying documentation covered by this license (the "Software") to use, reproduce, display, distribute, execute, and transmit the Software, and to prepare derivative works of the Software, and to permit third-parties to whom the Software is furnished to do so, all subject to the following: The copyright notices in the Software and this entire statement, including the above license grant, this restriction and the following disclaimer, must be included in all copies of the Software, in whole or in part, and all derivative works of the Software, unless such copies or derivative works are solely in the form of machine-executable object code generated by a source language processor. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

a utility for orm C++ and json dom。 展开 收起
C++
BSL-1.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
C++
1
https://gitee.com/lordoffox/ajson.git
git@gitee.com:lordoffox/ajson.git
lordoffox
ajson
ajson
master

搜索帮助