1 Star 0 Fork 0

PGZXB / PGJson

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

PGJson介绍

  • PGJson是一个使用C++11编写的Json解析器,现阶段只提供DOM风格解析。
    • 支持g++、MSVC,编译器仅依赖于处理程序的生前死后上(before-main & after-main)。
    • 使用C++11编写,类型架构分明,代码风格良好。
    • 只使用了STL的极小部分(只使用了std::vector做解析时的buffer,还有可通过宏开关关闭使用的对外std::string接口),不依赖任何第三方库、系统API,暂时使用编译器扩展处理生前死后(无法之法),使用CMake管理项目,跨平台跨编译器。
    • 尽量在降低头文件污染和inline提速上寻找平衡。
    • 使用时链接一个静态库即可,使用方便。
    • 使用高效的对象/内存池分配管理Json对象。
    • 优化基础组件提高效率,减少不必要的复制开销。
    • 支持标准Json并支持一部分扩展功能。
    • 解析、格式化Json均使用输入输出流,支持定制扩展。
    • 提供对用户使用体验良好的API并且同时不完全封闭内部函数,方便用户实现更精细的扩展。
    • 利用C++11变参模板做了更舒服的API(还未完全完成)。
    • 良好的ParseError的处理(未实现,会加入错误栈等更精细的错误信息处理)
    • 基于对象的编程风格,未使用虚函数,提高效率。
    • 使用RAII管理对象。
    • 解析和字符串处理均使用字节流而非字符流,为以后增加编码支持提供基础。
    • 未来会实现更多的扩展和对内存和错误处理的更精细的控制。
    • 未来会实现大数和高精度数的存储和处理。
    • 未来会提供UTF8、GBK、Unicode支持。
  • PGJson仓库 : PGJson·Gitee

PGJson安装

  • 由于本项目使用CMake管理,安装简便。
  • clone本仓库到本地
git clone https://gitee.com/pgzxb/pgjson.git
  • 进入pgjson目录,创建build目录并进入
cd pgjson
mkdir build
cd build
  • 执行cmake
cmake ..
  • 生成

    • 如果是MSVC就打开ALL_BUILD项目生成即可(注意如果MSVC生成Release版将无法使用断言)
    • 如果是MinGW或MacOS/Linux,执行mingw32-makemake
  • 生成静态库文件链接使用即可。

  • MinGW64下的全过程

git clone https://gitee.com/pgzxb/pgjson.git
cd pgjson
mkdir build
cd build
cmake .. -G "MinGW Makefiles"
mingw32-make
# 最终在pgjson/build中有libPGJson.a静态库文件
  • 最终生成PGJson和PGTest两个项目,PGTest现无实质内容。

Hello PGJson

  • PGJson入门示例
#include <iostream>
#include <PGJson/Document.h>

using namespace pg::base::json;

int main() {
    // Only when PGJSON_DEBUG was defined, can assertion be used in PGJson.
    // Plenty of assertions are used in PGJson to DEBUG.
    // Recommend you define the macro.

    const char json[] = "{\n"
                        "   \"From\" : {\"name\" : \"PGZXB\", \"age\" : 19, \"GPA\" : 4.14},\n"
                        "   \"To\" : \"PGJson\",\n"
                        "   \"sites\": [\n"
                        "       { \"name\":\"CSDN\" , \"url\": \"https://blog.csdn.net/PGZXB?spm=1011.2124.3001.5343\" },\n"
                        "       { \"name\":\"Gitee\" , \"url\":\"https://gitee.com/pgzxb\" },\n"
                        "       { \"name\":\"Zhihu\" , \"url\":\"https://www.zhihu.com/people/geek-81-44\" },\n"
                        "   ]\n"
                        "}\n";  // The Json-String that will be parsed
                        
    // Create Document, which can parse from string/file/stream and stringify DOM to file/string/stream
    Document document;

    document.parseFromString(json);  // parse json form string

    Node & DOM = document.d();  // get DOM-Root
    
    // add "msg" : "Hello PGJson!" to DOM-Tree-Root-Object
    DOM.addMember("msg")->value.setString("Hello PGJson!");
    
    // stringify the json-tree, default : format-open
    std::cout << document.stringify() << "\n\n";
    
    // stringify the json-tree, format-close
    std::cout << document.stringify(false) << "\n\n";
    return 0;
}
  • g++编译
# 在pgjson目录下
g++ hello_pgjson.cpp -L build -lPGJson -o hello_pgjson -I include -DPGJSON_DEBUG -Wall
  • 运行结果
{
        "From" : {
                "name" : "PGZXB",
                "age" : 19,
                "GPA" : 4.140000
        },
        "To" : "PGJson",
        "sites" : [
                {
                        "name" : "CSDN",
                        "url" : "https://blog.csdn.net/PGZXB?spm=1011.2124.3001.5343"
                },
                {
                        "name" : "Gitee",
                        "url" : "https://gitee.com/pgzxb"
                },
                {
                        "name" : "Zhihu",
                        "url" : "https://www.zhihu.com/people/geek-81-44"
                }
        ],
        "msg" : "Hello PGJson!"
}

{"From" : {"name" : "PGZXB","age" : 19,"GPA" : 4.140000},"To" : "PGJson","sites" : [{"name" : "CSDN","url" : "https://blog.csdn.net/PGZXB?spm=1011.2124.
3001.5343"},{"name" : "Gitee","url" : "https://gitee.com/pgzxb"},{"name" : "Zhihu","url" : "https://www.zhihu.com/people/geek-81-44"}],"msg" : "Hello PG
Json!"}

现支持的扩展

  • 单行注释//
  • Object最后一个字段后可加,
  • Array最后一个Item后可加,

更多功能、代码示例期待后续---

MIT License Copyright (c) 2021 PGZXB Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 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 AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

JSON Parser written in C++11. 展开 收起
C++ 等 2 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
C++
1
https://gitee.com/pgzxb/pgjson.git
git@gitee.com:pgzxb/pgjson.git
pgzxb
pgjson
PGJson
master

搜索帮助