Skip to content
This repository has been archived by the owner on Aug 23, 2022. It is now read-only.
/ Json2Qt Public archive

A Qt code generator to automatically translate JSON to Qt class type.

License

Notifications You must be signed in to change notification settings

Loukei/Json2Qt

Repository files navigation

Json2Qt

A Qt code generator to automatically translate JSON to Qt class type.

Introduction

JSON is a popular data format in many areas.

Reading and writing the JSON file then convert to C++ struct/class is very painful and waste time, especially when JSON Data has dozens of attributes.

There were some tool in C++ to manage this task like json2cpp.py,autojsoncxx,quicktype,QtJsonSerializer ... ,but its either not use Qt API or hard to install.

How to use

python Json2Qt.py -i user.json

or try Json2Qt.exe:

Json2Qt -i user.json

The json file:

img

will create to user.h:

img

and user.cpp:

img

  1. Add the generated file user.h and user.cpp to your project.
  2. Use QJsonObject parseJsonFile(const QString& filename) function to turn file into QJsonObject.
  3. Use User object construct by QJsonObject.
  • Turn object to QJsonObject, use QJsonObject toQJsonObject() const function.
  • Save User to JSON file, use QJsonDocument(user.toQJsonObject()).toJson() QJsonDocument
#include "user.h"

QJsonObject jsonObj = parseJsonFile("user.json");
auto user = User(jsonObj);
qDebug() << "User:" << user.toQJsonObject();

Dependency

  • Qt 5.0(or higher) for generated file.
  • Python 3

Check Pipfile if you like pipenv

Bugs

I didn't process the variable name from the JSON file. So the variable name like {"image/jpeg":["application/vnd.google-apps.document"]} will turn to QString image/jpeg;.

Relate work

Qt Code Generator:

Template Engine:

Using the string-based method to generate code is inflexible and hard to read.

Reference

JSON Support in Qt

About

A Qt code generator to automatically translate JSON to Qt class type.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published