JSON + Qt :: Blog
JSON + Qt
(posted at 2008-09-28 17:35:22 UTC)
Due to a mixture of only hearing about JSON recently, and being curious about how much of my compiler design course I remembered, I made a pure-Qt parser for JSON; it can either produce a full tree-structure using a mixture of QVariants, QVariantLists, and QVariantMaps, or, if you've got flat JSON, it can directly write properties on a QObject - as a contrived example:
JsonQt::JsonToProperties parser;
parser.parse("{\"objectName\" : \"badger badger badger\"}", this);
QASSERT(objectName() == "badger badger badger");
The code is in my git repository; construction of JSON strings from QObject properties or QVariant structures is on my todo list.
Comments
oooh ... could use this in plasma!
Posted at 2008-09-29 17:20:35 UTC by "Aaron Seigo"
We're seeing more and more web facing DataEngines; this could be some very useful code to have kicking around. Do you have release plans for this yet? Not timelines, per se, but "where I would like this to eventually live" type stuff. Or is it more of a toy project for fun?
How can I add a library to my Qt project
Posted at 2008-11-18 17:44:09 UTC by "HH"
I'm somewhat ashamed that I can't figure out how to add this library to my project. Or any lib to a Qt project. I'd like it to be in the project-tree. Thanks in advance. And thanks for writing this lib.
How does this work?
Posted at 2009-04-02 10:29:30 UTC by "SneakyWho_am_i"
This may be obvious to intelligent people who are not HH but how do I add this to my project? Is there some shorthand I can do to my .pro project file like setting the include directory? (tip: that didn't seem to help) Can I specifically include just one file? (my money's on JsonRpc.cpp) Or do I have to include every single file that I might possibly use? (in which case I should back up and overwrite my project file and then merge) I haven't even so much as tried to figure it out for myself yet. Probably and hopefully it will be easy, but: - it would be nice if it were spelled out in English for morons like myself ;) - that CMake txt file sitting in there is scary, I know nothing about CMake but I'm guessing it's not something I should care about. When I get this working (or an equivalent) it will be awesome - although the official SOAP client is a strong contender... Keep up the good work anyway.
How could it be anything but beer?
Posted at 2009-04-02 10:51:53 UTC by "SneakyWho_am_i"
Ok, maybe I can use HTML here: notifier.h: #ifndef NOTIFIER_H #define NOTIFIER_H #include "ui_notifier.h" class evilNotifier : public QMainWindow, private Ui::MainWindow { Q_OBJECT public: evilNotifier(QMainWindow *parent = 0); QString rate; protected: void closeEvent(QCloseEvent *event); public slots: void showData(); void showPic(); void update(); }; #endif Relevant parts of notifier.cpp: #include "include/notifier.h" #include "include/JsonQt/JsonToProperties.h" // snip... JsonQt::JsonToProperties parser; parser.parse(http->readAll(), this); QMessageBox::information(this, "result",rate); // snip ... Project file: ###################################################################### # Automatically generated by qmake (2.01a) Thu Apr 2 23:30:41 2009 ###################################################################### TEMPLATE = app TARGET = DEPENDPATH += . app include resource include/JsonQt debug release INCLUDEPATH += . include include/JsonQt QT+= network script # Input HEADERS += include/notifier.h include/JsonQt/JsonQtExport.h include/JsonQt/JsonRpc.h include/JsonQt/JsonRpcAdaptor.h include/JsonQt/JsonRpcAdaptorPrivate.h include/JsonQt/JsonToProperties.h include/JsonQt/JsonToVariant.h include/JsonQt/ParseException.h include/JsonQt/VariantToJson.h FORMS += include/notifier.ui SOURCES += app/main.cpp app/notifier.cpp include/JsonQt/JsonRpc.cpp include/JsonQt/JsonRpcAdaptor.cpp include/JsonQt/JsonRpcAdaptorPrivate.cpp include/JsonQt/JsonToProperties.cpp include/JsonQt/JsonToVariant.cpp include/JsonQt/ParseException.cpp include/JsonQt/VariantToJson.cpp RESOURCES += resource/evil.qrc RC_FILE = resource/notifier.rc The object called "http" in notifier.cpp should be a Qhttp object, this attempt to parse JSON is happening in the slot called by the completed http request. So it reads the response (something like {"rate":"123"}) into the nice JSON parser and I should see the result "123" come up in the message box :) No such luck. It compiles. It runs. I see a messagebox... But the messagebox is empty. So I'm definitely getting close. Oh and the example you gave here didn't work for me; I can't instantiate the parser using your code because JsonQt::JsonToProperties is private in JsonToProperties.h ... Is this something to do with the way I called it? I tried including it as a .cpp and as a .h but neither worked. So for now, while I'm figuring this out, the constructor is public. I'll try including some of the other files, and I'll try casting the result to a QString to see if it's just invisible somehow... But I don't think that I have the skill to use this without a readme yet (shame, because if I did I might have the capability to implement it on my own) I still haven't spent all day on it and it's still fun to play with though. I hope this (is formatted correctly and) gives some clues to HH.
To read my prior comment...
Posted at 2009-04-02 10:53:25 UTC by "SneakyWho_am_i"
Paste my prior comment into a text editor and save it as sneaky.html so that the new lines come up. And try changing the code elements to pre elements if your browser doesn't apply white-space:pre or a similar style to code elements. Sorry.
Re: JsonToProperties
Posted at 2009-05-02 10:30:11 UTC by "Fred"
JsonToProperties calls QObject::setProperty - this means that the value read is only accessible via QObject::getProperty, unless the property was declared with Q_PROPERTY with accessors. It won't just write to member variables. An alternative interface is JsonToVariant.
New Comment
My Blog ▶ 2008 ▶ September ▶ 28 ▶ JSON + Qt