{"id":23529512,"url":"https://github.com/cctyl/qtjson","last_synced_at":"2025-06-25T19:02:49.308Z","repository":{"id":250676701,"uuid":"835114842","full_name":"cctyl/qtjson","owner":"cctyl","description":" dependency-free, header-only, ready-to-use library for automatic serialization and deserialization;  无依赖、head-only、开箱即用的半自动qtjson序列化、反序列化库","archived":false,"fork":false,"pushed_at":"2024-10-25T07:11:00.000Z","size":74,"stargazers_count":7,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-22T17:24:24.306Z","etag":null,"topics":["cpp","json","qt","qt6"],"latest_commit_sha":null,"homepage":"https://www.bilibili.com/video/BV1dhvrexESw/","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cctyl.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-07-29T07:32:04.000Z","updated_at":"2025-03-09T02:00:40.000Z","dependencies_parsed_at":"2024-07-29T11:52:20.528Z","dependency_job_id":"5acc5e99-8f69-4531-a7f4-e056c7c60a46","html_url":"https://github.com/cctyl/qtjson","commit_stats":null,"previous_names":["cctyl/qtjson"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/cctyl/qtjson","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cctyl%2Fqtjson","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cctyl%2Fqtjson/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cctyl%2Fqtjson/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cctyl%2Fqtjson/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cctyl","download_url":"https://codeload.github.com/cctyl/qtjson/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cctyl%2Fqtjson/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261937021,"owners_count":23232843,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp","json","qt","qt6"],"created_at":"2024-12-25T21:12:17.191Z","updated_at":"2025-06-25T19:02:49.290Z","avatar_url":"https://github.com/cctyl.png","language":"C++","readme":"# qtjson\n\n\n\n## Introduction\n【[中文](./README_CN.md) 】\n[qtjson] is a dependency-free, header-only, ready-to-use automatic serialization and deserialization library.\nIt is completely based on Qt's own JSON-related objects.\nThis library is modified from https://github.com/archibate/reflect-hpp.\n\n## Features\n**Header-Only**: No need to compile or link any additional libraries.\n\n**Dependency-Free**: Only relies on the Qt framework, making it easy to integrate into existing projects.\n\n**Automatic Serialization/Deserialization**: Automatically converts between C++ objects and JSON format with minimal code.\n\n**Customizable**: Supports custom types and complex data structures.\n\n**Cross-Platform**: Works on all platforms supported by Qt (Windows, macOS, Linux, etc.).\n\n## Getting Started\n\n### Prerequisites\nQt 5.10 or higher\n\n### Installation\nSince qtjson is a header-only library, you just need to include the header files in your project.\n\n### Adding Header Files to Your Project\nqtjson.hpp\nreflect.hpp\n\n\n### Including the Header File\n\n\n```\n#include\"qtjson.hpp\"\n```\n\n### Using the Namespace\n```\nusing namespace qtjson; // You can also skip this declaration\n```\n\n### Custom Types\n#### Member Registration\n\nYou can manually input the member registration, or open the auto-reflect.html file in a browser, enter the property names, and auto-generate the registration text:\n\n\n\u003cp align=\"center\"\u003e\n    \u003cimg src=\"./img/auto-register.png\" \u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n    \u003cimg src=\"./img/auto-register-show.png\" \u003e\n\u003c/p\u003e\n\n\n```\n\nclass OsInfo {\npublic:\n\n    QString m_type = \"Windows\";\n    QString m_platform = \"win\";\n    QString m_release;\n    QString m_hostname;\n    QString m_arch;\n    double  m_uptime;\n\t\n\t// Member registration\n    REFLECT(m_type,m_platform,m_release,m_hostname,m_arch,m_uptime);\n};\n\n```\n\n\n\n#### Serialization and Deserialization\n\n```\n//Custom type\nvoid testCustom(){\n\n    using namespace qtjson;\n    //1.Convert object to QJsonObject\n    OsInfo * osInfo = new OsInfo();\n    osInfo-\u003em_platform =\"平台\";\n    osInfo-\u003em_arch = \"x86\";\n    osInfo-\u003em_hostname = \"lenovo\";\n    osInfo-\u003em_release = \"win10212H\";\n    osInfo-\u003em_uptime = 12.6;\n    QJsonObject j1= objToJson(*osInfo);\n    for (auto it = j1.begin(); it != j1.end(); ++it) {\n        qDebug() \u003c\u003c \"Key:\" \u003c\u003c it.key() \u003c\u003c \", Value:\" \u003c\u003c it.value().toString();\n    }\n\n\n    //2.Convert QJsonObject to object\n    OsInfo o =  jsonToObj\u003cOsInfo\u003e(j1);\n    qDebug()\u003c\u003c o.m_platform\u003c\u003c\",\"\n             \u003c\u003co.m_arch\u003c\u003c\",\"\n             \u003c\u003co.m_hostname\u003c\u003c\",\"\n             \u003c\u003co.m_release\u003c\u003c\",\"\n             \u003c\u003co.m_uptime;\n\n\n    //Convert object to JSON string\n    QString jsonStr = serialize(*osInfo);\n    /*\n        {\n            \"m_arch\": \"x86\",\n            \"m_hostname\": \"lenovo\",\n            \"m_platform\": \"平台\",\n            \"m_release\": \"win10212H\",\n            \"m_type\": \"Windows\",\n            \"m_uptime\": 12.6\n        }\n     */\n\n    qDebug()\u003c\u003cjsonStr.toUtf8().data();\n\n    //Convert JSON string to object\n    OsInfo o2 =  deserialize\u003cOsInfo\u003e(jsonStr);\n    qDebug()\u003c\u003c o2.m_platform\u003c\u003c\",\"\n             \u003c\u003co2.m_arch\u003c\u003c\",\"\n             \u003c\u003co2.m_hostname\u003c\u003c\",\"\n             \u003c\u003co2.m_release\u003c\u003c\",\"\n             \u003c\u003co2.m_uptime;\n\n\n\n\n\n    delete osInfo;\n}\n\n```\n\n\n\n## Extending Custom Types\n\nFor non-primitive types such as std::map and std::vector, special handling is required.\n\n### Adding Specialized Types\nAt the end of qtjson.hpp, within the qtjson namespace, add the following template class format:\n\n```\n\ntemplate \u003ctypename ...SpecializedTypeGenerics\u003e\nstruct special_traits\u003cSpecializedType\u003e {\n    static constexpr bool value = true;\n\n    static QJsonValue objToJson(SpecializedType const \u0026object) {\n       // This function should return a QJsonValue\n    }\n\n    static SpecializedType jsonToObj(QJsonValue const \u0026root) {\n       // This function should convert QJsonValue to SpecializedType\n    }\n};\n\n\n```\n\n\n### Examples\n```\n\n//1.std::vector\ntemplate \u003cclass T, class Alloc\u003e\nstruct special_traits\u003cstd::vector\u003cT, Alloc\u003e\u003e {\n    static constexpr bool value = true;\n\n    static QJsonValue objToJson(std::vector\u003cT, Alloc\u003e const \u0026object) {\n        QJsonArray root;\n        for (auto const \u0026elem: object) {\n\n            qDebug()\u003c\u003c elem;\n            qDebug()\u003c\u003c  qtjson::objToJson(elem);\n            root.append(qtjson::objToJson(elem));\n        }\n\n        qDebug()\u003c\u003croot;\n        return root;\n    }\n\n    static std::vector\u003cT, Alloc\u003e jsonToObj(QJsonValue const \u0026root) {\n        std::vector\u003cT, Alloc\u003e object;\n        for (auto const \u0026elem : root.toArray()) {\n            object.push_back(qtjson::jsonToObj\u003cT\u003e(elem));\n        }\n        return object;\n    }\n};\n\n//2.std::map\ntemplate \u003cclass K, class V, class Alloc\u003e\nstruct special_traits\u003cstd::map\u003cK, V, Alloc\u003e\u003e {\n    static constexpr bool value = true;\n\n    static QJsonValue objToJson(std::map\u003cK, V, Alloc\u003e const \u0026object) {\n        QJsonObject root;\n        for (auto const \u0026elem: object) {\n            root[elem.first] = qtjson::objToJson(elem.second);\n        }\n        return root;\n    }\n\n    static std::map\u003cK, V, Alloc\u003e jsonToObj(QJsonValue const \u0026root) {\n        std::map\u003cK, V, Alloc\u003e object;\n         QJsonObject  jsonObject = root.toObject();\n\n        for (auto it = jsonObject.begin(); it != jsonObject.end(); ++it) {\n            const QString \u0026key = it.key();\n            const QJsonValue \u0026value = it.value();\n            object[key] = qtjson::jsonToObj\u003cV\u003e(root[key]);\n\n        }\n        return object;\n    }\n};\n\n\n```\n\n\n\n\n\n\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcctyl%2Fqtjson","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcctyl%2Fqtjson","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcctyl%2Fqtjson/lists"}