# jsoncpp **Repository Path**: disenIT/jsoncpp ## Basic Information - **Project Name**: jsoncpp - **Description**: 自研的json之c++解析与生成跨平台动态库 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-08-13 - **Last Updated**: 2026-08-13 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # JSON 应用库 (json) 一个无第三方依赖的 C++17 JSON 解析与应用库,支持生成动态库(DLL / SO),可在 **Windows** 与 **Linux (POSIX)** 下编译。 ## 功能特性 - **完整 JSON 支持**:null / boolean / number / string / array / object 六种类型 - **递归下降解析器**:支持字符串转义、Unicode(`\uXXXX` 含代理对)、数字(整数/小数/指数) - **递归序列化器**:紧凑格式与 pretty print(缩进 + 换行) - **生成器**:`JsonBuilder` 链式调用、初始化列表快速创建 - **JSON Path 查询**:`$.users[0].name`、`[*]` 通配符 - **公共 API**:`parse / parseFile / stringify / pretty / minify / saveFile / query` - **命令行工具**:`json_tool`(格式化 / 查询 / 输出文件) - **单元测试**:覆盖全部模块,通过 `ctest` 运行 ## 目录结构 ``` codes/ ├── CMakeLists.txt # 顶层构建脚本 ├── include/json/ # 公共头文件 │ ├── json.hpp # 聚合头文件, 推荐 #include │ ├── export.hpp # 跨平台导出宏 │ ├── exception.hpp # 异常体系 │ ├── value.hpp # JsonValue 核心类 │ ├── parser.hpp # JsonParser │ ├── serializer.hpp # JsonSerializer │ ├── builder.hpp # JsonBuilder │ ├── json_path.hpp # JsonPath │ └── api.hpp # 公共 API ├── src/ # 动态库实现 ├── tools/ # json_tool 命令行工具 ├── tests/ # 单元测试 └── examples/ # 示例程序 ``` ## 构建 ### 环境要求 - CMake ≥ 3.14 - 支持 C++17 的编译器: - Windows: MSVC (VS2019+) / MinGW-w64 (GCC 11+) - Linux: GCC 8+ / Clang 8+ ### 构建步骤 ```bash cd codes # 配置 (Release 默认) cmake -S . -B build # 编译 cmake --build build -j # 运行测试 ctest --test-dir build --output-on-failure ``` 构建产物输出到 `build/bin`(动态库与可执行文件)和 `build/lib`(导入库/静态库)。 ### 构建选项 | 选项 | 默认 | 说明 | |------|------|------| | `JSON_BUILD_SHARED` | ON | 生成动态库(OFF 时生成静态库) | | `JSON_BUILD_TOOL` | ON | 编译命令行工具 | | `JSON_BUILD_TESTS` | ON | 编译单元测试 | | `JSON_BUILD_EXAMPLES` | ON | 编译示例 | ### Windows (MSVC) ```bat cmake -S . -B build -G "Visual Studio 17 2022" -A x64 cmake --build build --config Release ``` ### Linux / macOS ```bash cmake -S . -B build cmake --build build -j ``` ### 安装 ```bash cmake --install build --prefix /usr/local ``` 安装后可通过 `find_package` / pkg-config 使用: ```cmake find_package(json REQUIRED) target_link_libraries(your_app PRIVATE json::json) ``` ## 快速上手 ```cpp #include #include int main() { // 1. 解析 auto value = json::parse(R"({"name":"alice","age":18,"scores":[90,85]})"); std::cout << value["name"].asString() << std::endl; // alice std::cout << value["scores"][1].asInt() << std::endl; // 85 // 2. 序列化 std::cout << json::pretty(value) << std::endl; // 3. Builder 生成 auto user = json::JsonBuilder::object() .set("id", 1001) .set("tags", json::JsonBuilder::array().add("cpp").add("json").build()) .build(); // 4. JSON Path 查询 auto title = json::query(value, "$.name"); std::cout << title.asString() << std::endl; // 5. 文件读写 json::saveFile("out.json", user, /* pretty */ true); auto loaded = json::parseFile("out.json"); return 0; } ``` ## 命令行工具 ```bash # 美化输出 json_tool -p input.json # JSON Path 查询 json_tool -q '$.users[0].name' input.json # 输出到文件 json_tool -p -o output.json input.json # 从标准输入读取 cat input.json | json_tool -p - ``` ## 异常体系 | 异常 | 触发场景 | |------|----------| | `json::JsonParseError` | JSON 语法错误 | | `json::JsonTypeError` | 类型访问不匹配(如对数字调用 `asString`) | | `json::JsonPathError` | JSON Path 表达式错误或查询未命中 | | `json::JsonException` | 文件读写失败等(以上异常的基类) | ## 许可证 本库仅供学习交流使用。