I recently had a need to create a command processor in Qt, using C++ only, on a resource-limited system. I decided to use JSON rather than implement a fully-custom protocol.
Rather than pulling a 3rd party library into my Qt application, I ran across this handy overview demonstrating how to use QScriptEngine to do the parsing and validation. Unfortunately, QScriptEngine does not support serialization of data structures to JSON, although generating JSON using QString is pretty trivial.
The Ruby server implementation is very trivial using the JSON gem:
The Qt client uses QTcpSocket to connect to the server and is fully event-driven due to the excellent signals/slots mechanism that Qt brings to C++. I have wired up a few signals from the QTcpSocket to local slots in the Client class to demonstrate a few of the event notifications that QTcpSocket provides. Although the key signal for receipt of JSON data is readyRead(), which is fired whenever the socket receives data.
The constructor does the wiring up of readyRead() to my ProcessRequest() handler:
ProcessRequest() uses QScriptEngine to parse the JSON packet, which returns a QScriptValue object for the root node of the data structure:
QScriptValue::property() allows access to the key/value pairs and also has some nice helper methods for validation of the schema: