{"id":23289349,"url":"https://github.com/linxmouse/jsonserializer","last_synced_at":"2025-10-16T04:59:39.513Z","repository":{"id":264766013,"uuid":"869872280","full_name":"linxmouse/JsonSerializer","owner":"linxmouse","description":"Supporting serialization for primitive types, Qt containers, standard containers, and custom objects that inherit from the JsonSerializable base class.","archived":false,"fork":false,"pushed_at":"2024-12-17T03:04:31.000Z","size":18,"stargazers_count":7,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-12-17T04:19:22.251Z","etag":null,"topics":["json","qt","template"],"latest_commit_sha":null,"homepage":"","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/linxmouse.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","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}},"created_at":"2024-10-09T03:29:34.000Z","updated_at":"2024-12-17T03:04:35.000Z","dependencies_parsed_at":"2024-11-26T08:40:20.145Z","dependency_job_id":null,"html_url":"https://github.com/linxmouse/JsonSerializer","commit_stats":null,"previous_names":["linxmouse/jsonserializer"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/linxmouse%2FJsonSerializer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/linxmouse%2FJsonSerializer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/linxmouse%2FJsonSerializer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/linxmouse%2FJsonSerializer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/linxmouse","download_url":"https://codeload.github.com/linxmouse/JsonSerializer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230532452,"owners_count":18240792,"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":["json","qt","template"],"created_at":"2024-12-20T04:13:57.559Z","updated_at":"2025-09-22T22:15:58.389Z","avatar_url":"https://github.com/linxmouse.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"### Overview\nThe `JsonSerializer` framework provides a convenient way to serialize and deserialize C++ objects into JSON format using Qt's `QJsonDocument`, `QJsonObject`, `QJsonArray`, and `QJsonValue`. The system is flexible, supporting serialization for primitive types, Qt containers, standard containers, and custom objects that inherit from the `JsonSerializable` base class.\n\n### Features\n- **Serialization**: Convert various data types (primitives, Qt containers, standard containers, custom classes) into `QJsonValue`.\n- **Deserialization**: Rebuild objects from JSON format, making it easy to read and store complex data.\n- **Qt Meta-Object System Integration**: Custom classes can easily be made serializable by inheriting from `JsonSerializable` and using macros like `JSON_PROPERTY`.\n- **Support for Custom Types**: Extensible to support new data types by implementing or specializing the `Serializer` template.\n\n### Requirements\n- Qt 5.0 or later.\n- C++11 or higher for template meta-programming.\n\n### Key Components\n\n1. **Serializer**: A template-based system that handles the conversion of various data types to and from JSON. It supports:\n    - **Primitive types**: `int`, `double`, `bool`, `QString`, etc.\n    - **Qt containers**: `QList`, `QVector`, `QMap`, `QHash`.\n    - **Standard containers**: `std::vector`, `std::map`.\n    - **Custom types**: Custom classes inheriting from `JsonSerializable`.\n  \n2. **JsonSerializable**: A base class that facilitates the integration with Qt's meta-object system. It provides:\n    - `toJson()`: Converts an object to a `QJsonObject`.\n    - `fromJson()`: Rebuilds an object from a `QJsonObject`.\n    - `toRawJson()`: Returns a `QByteArray` representation of the object in JSON format.\n\n3. **Macros**:\n    - `JSON_SERIALIZABLE`: Marks a class as serializable.\n    - `JSON_PROPERTY`: Declares a JSON property for a class, providing getter and setter methods that serialize/deserialize the property.\n\n### Example Classes\n\n1. **TestPerson**: A simple class representing a person with a name, age, and hobbies.\n    ```cpp\n    class TestPerson final: public JsonSerializable\n    {\n        Q_GADGET\n        JSON_SERIALIZABLE\n    public:\n        JSON_PROPERTY(QString, name)\n        JSON_PROPERTY(int, age)\n        JSON_PROPERTY(QList\u003cQString\u003e, hobbies)\n    };\n    ```\n\n2. **TestPageInfo**: A class representing pagination information.\n    ```cpp\n    class TestPageInfo final: public JsonSerializable\n    {\n        Q_GADGET\n        JSON_SERIALIZABLE\n    public:\n        JSON_PROPERTY(int, totalNumber)\n        JSON_PROPERTY(int, totalPage)\n        JSON_PROPERTY(int, pageSize)\n        JSON_PROPERTY(int, currentPage)\n    };\n    ```\n\n3. **TestPagedPerson**: A paginated list of persons.\n    ```cpp\n    class TestPagedPerson final: public JsonSerializable\n    {\n        Q_GADGET\n        JSON_SERIALIZABLE\n    public:\n        JSON_PROPERTY(TestPageInfo, page)\n        JSON_PROPERTY(std::vector\u003cTestPerson\u003e, persons)\n    };\n    ```\n\n### Usage Example\n\n#### Serialization\nIn the `main.cpp` file, we demonstrate how to serialize and write a `TestPagedPerson` object to a file:\n```cpp\nTestPagedPerson pagedPerson;\nTestPageInfo page;\npage.set_totalNumber(80);\npage.set_totalPage(4);\npage.set_currentPage(1);\npage.set_pageSize(20);\npagedPerson.set_page(page);\n\nTestPerson person1;\nperson1.set_age(18);\nperson1.set_name(\"A\");\nperson1.set_hobbies({ \"running\", \"TV\" });\n\npagedPerson.set_persons({ person1 });\nauto rawJson = pagedPerson.toRawJson();\nqDebug().noquote() \u003c\u003c rawJson;\n\nQFile wf(\"example.json\");\nif (!wf.open(QIODevice::WriteOnly | QIODevice::Text)) {\n    qCritical() \u003c\u003c \"Can not open file to write!\";\n}\nQTextStream wts(\u0026wf);\nwts \u003c\u003c rawJson;\nwf.close();\n```\nThis will create a JSON file (`example.json`) containing the serialized data.\n\n#### Deserialization\nTo read the JSON file and convert it back to a `TestPagedPerson` object:\n```cpp\nQFile rf(\"example.json\");\nif (!rf.open(QIODevice::ReadOnly | QIODevice::Text)) {\n    qCritical() \u003c\u003c \"Can not open file to read!\";\n}\nQTextStream rts(\u0026rf);\nauto jsonStr = rts.readAll();\nrf.close();\n\nTestPagedPerson newPagedPerson;\nnewPagedPerson.fromJson(jsonStr.toUtf8());\n```\n\n### How to Extend\n\nTo add serialization support for new types:\n1. Specialize the `Serializer` template for the type.\n2. Implement `toJson()` and `fromJson()` methods for converting between the type and `QJsonValue`.\n\nFor example, adding support for a new container type:\n```cpp\ntemplate\u003ctypename T\u003e\nstruct Serializer\u003cMyContainer\u003cT\u003e\u003e {\n    static QJsonValue toJson(const MyContainer\u003cT\u003e\u0026 container) {\n        QJsonArray array;\n        for (const auto\u0026 item : container) {\n            array.append(Serializer\u003cT\u003e::toJson(item));\n        }\n        return array;\n    }\n\n    static MyContainer\u003cT\u003e fromJson(const QJsonValue\u0026 json) {\n        MyContainer\u003cT\u003e result;\n        if (json.isArray()) {\n            QJsonArray array = json.toArray();\n            for (const auto\u0026 item : array) {\n                result.add(Serializer\u003cT\u003e::fromJson(item));\n            }\n        }\n        return result;\n    }\n};\n```\n\n### License\nThis code is provided as-is. For any questions or issues, feel free to contact the author at linxmouse@gmail.com.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flinxmouse%2Fjsonserializer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flinxmouse%2Fjsonserializer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flinxmouse%2Fjsonserializer/lists"}