# mod_utils **Repository Path**: codedoggg/mod_utils ## Basic Information - **Project Name**: mod_utils - **Description**: 常用功能集合模块 包括json解析 yaml解析 常规时间处理等 不考虑性能只以功能实现为主 跨平台可用linux、windows - **Primary Language**: C++ - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2026-11-02 - **Last Updated**: 2026-11-12 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # mod_util 跨平台 C++17 工具动态库,当前包含 YAML、JSON(cJSON)、字符串和文件操作接口。 ## 目录 ```text mod_util/ ├── CMakeLists.txt ├── include/mod_util/ │ ├── export.h │ ├── yaml.h │ ├── json.h │ ├── string_util.h │ ├── file_util.h │ └── mod_util.h └── src/ ├── yaml.cpp ├── json.cpp ├── string_util.cpp └── file_util.cpp ``` ## 依赖 - C++17 - cJSON(只用于 JSON;YAML 使用项目现有的轻量解析器) 父工程可以这样组织: ```cmake add_subdirectory(3rdparty/cJSON) add_subdirectory(mod_util) target_link_libraries(your_app PRIVATE mod_util) ``` 如果 cJSON 已安装到系统,也可以直接配置 `CJSON_INCLUDE_DIR` 和 `CJSON_LIBRARY`。 ## 使用示例 ```cpp #include #include int main() { Yaml yaml; if (!yaml.loadFile("./config/config.yaml")) { std::cerr << yaml.error() << std::endl; return 1; } const auto& children = yaml.root()["process"]["children"]; for (const auto& child : children.asSequence()) { std::cout << child["cid"].as() << '\n'; } // 也支持 process.children.0.cid 这种路径 std::string firstCid = yaml.get("process.children.0.cid", ""); Json root = Json::object(); root.add("name", "monitor"); root.add("port", 18080); Json arr = root.addArray("items"); Json item = arr.pushObject(); item.add("id", 1); item.add("enabled", true); const std::string jsonText = root.dump(true); Json parsed; std::string error; if (!Json::parse(jsonText, parsed, &error)) { std::cerr << error << std::endl; } std::cout << parsed.get("name").asString() << std::endl; } ``` ## 注意 - 当前 YAML 解析器是轻量子集实现,支持常见缩进式 map/sequence、单双引号标量和行尾注释,不等同于完整 YAML 1.2。 - cJSON 的数字底层使用 `double`。`int64_t` 大于 `2^53` 时不能保证精确;例如数据库 BIGINT ID 建议按字符串写入 JSON。 - `Json::get()`、`at()`、`addObject()`、`pushObject()` 返回共享根对象生命周期的子节点视图,根对象销毁后视图也随之失效。