A Qt code generator to automatically translate JSON to Qt class type.
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.
python Json2Qt.py -i user.jsonor try Json2Qt.exe:
Json2Qt -i user.jsonThe json file:
will create to user.h:
and user.cpp:
- Add the generated file
user.handuser.cppto your project. - Use
QJsonObject parseJsonFile(const QString& filename)function to turn file intoQJsonObject. - Use
Userobject construct byQJsonObject.
- Turn object to
QJsonObject, useQJsonObject toQJsonObject() constfunction. - Save
Userto JSON file, useQJsonDocument(user.toQJsonObject()).toJson()QJsonDocument
#include "user.h"
QJsonObject jsonObj = parseJsonFile("user.json");
auto user = User(jsonObj);
qDebug() << "User:" << user.toQJsonObject();- Qt 5.0(or higher) for generated file.
- Python 3
Check Pipfile if you like pipenv
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;.
Qt Code Generator:
- Qface
- CppCodeGenerator or any other IDL code generator support Qt.
Template Engine:
Using the string-based method to generate code is inflexible and hard to read.


