GithubHelp home page GithubHelp logo

qjsonutil's Introduction

qjsonutil

  • configkey.h
    c++结构体与json字符串相互转换工具类

json字符串:

{
    "name": "class1",
    "room": 1,
    "courses": ["math", "english", "physics", "chemistry", "biology"],
    "teacher": {
        "name": "Tony",
        "score": 99.9
    },
    "students": [
        {
            "name": "Alice",
            "age": 12,
            "score_avg": 90.4,
            "adept": ["math", "english"]
        },
        {
            "name": "Bob",
            "age": 13,
            "score_avg": 86.1,
            "adept": ["physics", "chemistry"]
        }
    ]
}

定义模型:

#include "include/configkey.h"

using namespace QJsonUtil;

struct Student : JsonDumpInterface {

    CONFIG_KEY(QString, name);

    CONFIG_KEY(int, age);

    //字段与key名称不一致时不使用宏
    ConfigKey<double> scoreAvg{"score_avg"};

    CONFIG_KEY(QStringList, adept);

    QList<JsonReadInterface *> prop() override {
        return {&name, &age, &scoreAvg, &adept};
    }
};

struct Teacher : JsonDumpInterface {

    CONFIG_KEY(QString, name);

    CONFIG_KEY(double, score);

    QList<JsonReadInterface *> prop() override {
        return {&name, &score};
    }
};

struct Classes : JsonDumpInterface {

    CONFIG_KEY(QString, name);

    CONFIG_KEY(int, room);

    CONFIG_KEY(QStringList, courses);

    CONFIG_KEY(Teacher, teacher);

    CONFIG_KEY(QList<Student>, students);

    QList<JsonReadInterface *> prop() override {
        return {&name, &room, &courses, &teacher, &students};
    }
};

转换:

auto doc = QJsonDocument::fromJson(jsonStr);
if (!doc.isNull()) {
  auto object = doc.object();

  Classes classes;
  classes.fromJson(object);

  auto json = classes.dumpToJson();
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.