QtJsonSerializer 项目使用教程

QtJsonSerializer 项目使用教程

QtJsonSerializerA library to perform generic seralization and deserialization of QObjects from and to JSON and CBOR项目地址:https://gitcode.com/gh_mirrors/qt/QtJsonSerializer

1. 项目的目录结构及介绍

QtJsonSerializer 项目的目录结构如下:

QtJsonSerializer/
├── CMakeLists.txt
├── LICENSE
├── README.md
├── examples/
│   ├── CMakeLists.txt
│   ├── example1/
│   ├── example2/
│   └── ...
├── src/
│   ├── CMakeLists.txt
│   ├── QtJsonSerializer/
│   │   ├── CMakeLists.txt
│   │   ├── json_serializer.cpp
│   │   ├── json_serializer.h
│   │   └── ...
│   └── ...
└── tests/
    ├── CMakeLists.txt
    ├── test1/
    ├── test2/
    └── ...

目录介绍

  • CMakeLists.txt: 顶层CMake配置文件,用于构建整个项目。
  • LICENSE: 项目许可证文件,采用BSD-3-Clause许可证。
  • README.md: 项目说明文档,包含项目概述、安装和使用说明。
  • examples/: 示例代码目录,包含多个示例项目,展示如何使用QtJsonSerializer。
  • src/: 源代码目录,包含QtJsonSerializer库的核心实现。
  • tests/: 测试代码目录,包含多个测试用例,确保库的正确性和稳定性。

2. 项目的启动文件介绍

QtJsonSerializer 项目的启动文件位于 src/QtJsonSerializer/json_serializer.cppsrc/QtJsonSerializer/json_serializer.h

json_serializer.h

#ifndef JSON_SERIALIZER_H
#define JSON_SERIALIZER_H

#include <QObject>
#include <QJsonObject>

class JsonSerializer : public QObject {
    Q_OBJECT
public:
    explicit JsonSerializer(QObject *parent = nullptr);
    QJsonObject serialize(const QObject *object) const;
    void deserialize(const QJsonObject &json, QObject *object) const;
};

#endif // JSON_SERIALIZER_H

json_serializer.cpp

#include "json_serializer.h"
#include <QMetaObject>
#include <QMetaProperty>

JsonSerializer::JsonSerializer(QObject *parent) : QObject(parent) {}

QJsonObject JsonSerializer::serialize(const QObject *object) const {
    QJsonObject json;
    const QMetaObject *metaObject = object->metaObject();
    for (int i = 0; i < metaObject->propertyCount(); ++i) {
        QMetaProperty property = metaObject->property(i);
        if (property.isReadable()) {
            json[property.name()] = QJsonValue::fromVariant(property.read(object));
        }
    }
    return json;
}

void JsonSerializer::deserialize(const QJsonObject &json, QObject *object) const {
    const QMetaObject *metaObject = object->metaObject();
    for (int i = 0; i < metaObject->propertyCount(); ++i) {
        QMetaProperty property = metaObject->property(i);
        if (property.isWritable() && json.contains(property.name())) {
            property.write(object, json[property.name()].toVariant());
        }
    }
}

3. 项目的配置文件介绍

QtJsonSerializer 项目的配置文件主要是 CMakeLists.txt 文件。

顶层 CMakeLists.txt

cmake_minimum_required(VERSION 3.14)
project(QtJsonSerializer)

set(CMAKE_CXX_STANDARD 17)

find_package(Qt5 REQUIRED COMPONENTS Core)

add_subdirectory(src)
add_subdirectory(examples)
add_subdirectory(tests)

src/CMakeLists.txt

add_library(QtJsonSerializer SHARED
    QtJsonSerializer/json_serializer.cpp
    QtJsonSerializer/json_serializer.h
)

target_link_libraries(QtJsonSerializer Qt5::Core)

examples/CMakeLists.txt

add_executable(example1 example1.cpp)
target_link_libraries(example1 QtJsonSerializer Qt5::Core)

add_executable(example

QtJsonSerializerA library to perform generic seralization and deserialization of QObjects from and to JSON and CBOR项目地址:https://gitcode.com/gh_mirrors/qt/QtJsonSerializer

  • 21
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梅昆焕Talia

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值