# jsonreader **Repository Path**: PatchLion/jsonreader ## Basic Information - **Project Name**: jsonreader - **Description**: No description available - **Primary Language**: C++ - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-07-06 - **Last Updated**: 2025-07-07 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # JsonElement C++ JSON 智能类型映射库 ## 简介 JsonElement 是一个纯 C++17/14 实现的轻量级 JSON 解析与类型映射工具,基于 jsoncons 库,支持智能类型推断、递归容器、自定义类型、属性宏映射、jsonpath 路径访问等。 - 支持基础类型、std::vector/std::list、std::map、自定义业务类型递归转换 - PROPERTY_DEFINE/PROPERTY_DEFINE_WITH_P1 宏支持属性自动映射和路径占位符 - 兼容 MSVC/Clang/GCC,头文件无重复定义和类型萃取冲突 - VSCode 一键构建、调试、断点、example.json 自动拷贝 ## 主要文件结构 - jsonelement.h / jsonelement.cpp:核心类型与实现 - examples/example_jsonelement.cpp:用例主程序 - examples/student.h / classes.h:业务类型映射示例 - examples/example.json:测试数据 - CMakeLists.txt / .vscode/:构建与调试配置 ## 快速上手 1. 直接 include "jsonelement.h" 2. 通过 `JsonElement root(jsonstr);` 加载 JSON 字符串或文件 3. 使用 `as()` 智能类型推断获取任意类型 4. 用 PROPERTY_DEFINE 宏快速定义属性映射 ```cpp class Student : public JsonElement { public: PROPERTY_DEFINE(std::string, name, "$.name") PROPERTY_DEFINE(int, age, "$.age") }; JsonElement root(jsonstr); Student stu = root.readByPath("$.students[0]").as(); std::string name = stu.name(); ``` ## 复杂类型与容器 - 支持 vector、list、map 等递归类型 - 支持 jsonpath 路径占位符自动替换 ```cpp class Classes : public JsonElement { public: PROPERTY_DEFINE(std::vector, students, "$.students[*]") PROPERTY_DEFINE_WITH_P1(Student, studentByName, "$.students[?(@.name == \"%1\")]" ) }; Classes cls(jsonstr); Student s = cls.studentByName("张三"); ``` ## 兼容性与扩展 - 兼容 C++14/17,避免 void_t/type_traits 重复定义 - 支持自定义异常处理、国际化、自动测试等扩展 ## 贡献与许可 - 基于 MIT 协议,欢迎二次开发与贡献 - 依赖 jsoncons(https://github.com/danielaparker/jsoncons) ## 示例数据 example.json ```json { "name": "高三一班", "students": [ { "name": "张三", "age": 18, "score": 95.5, "id": "S001" }, { "name": "李四", "age": 17, "score": 88.0, "id": "S002" }, { "name": "王五", "age": 18, "score": 91.0, "id": "S003" } ] } ``` ## 完整 main() 示例 ```cpp #include "../jsonelement.h" #include "classes.h" #include "student.h" #include #include #include #include #include using namespace std; int main() { // Print current working directory for debug std::cout << "Current working directory: " << std::filesystem::current_path().string() << std::endl; // Try open example.json in current directory std::ifstream fin("example.json"); if (!fin.is_open()) { std::cout << "Failed to open example.json in current directory." << std::endl; return 1; } std::string jsonData((std::istreambuf_iterator(fin)), std::istreambuf_iterator()); fin.close(); // 支持两种结构:单个学生 或 班级(含学生数组) // 尝试解析为 Classes Classes cls(jsonData); vector studentNames = cls.studentNames(); if (!studentNames.empty()) { for (size_t i = 0; i < studentNames.size(); i++) { std::cout << " Student Name: " << studentNames[i] << std::endl; } } Student studentByName = cls.studentByName("张三"); if (!studentByName.name().empty()) { std::cout << "Student found by name: " << studentByName.name() << std::endl; } else { std::cout << "No student found with the name 张三." << std::endl; } vector students = cls.students(); if (!students.empty()) { std::cout << "Students: " << std::endl; for (auto stu : students) { std::cout << " Name: " << stu.name() << ", Age: " << stu.age() << std::endl; } std::cout << std::endl; } return 0; } ```