The JSON encoder/decoder (C library) is available on GitHub. This page provides information on how to use the C library and how to use JSON for real time IoT communication.
Learn how to use JSON for real time communication and how to use our free JSON C library.
JSON has become a popular inter-process communication (IPC) data interchange format for a variety of computer languages. It is important to understand that JSON is not a protocol, but an encoding format. JSON enables structured data to be serialized into a text format, which is then sent over the wire to the receiving end.
JSON is typically used together with IoT protocols that do not provide native support for data structure serialization such as HTTP/Rest, WebSockets, MQTT, and the SMQ IoT Protocol.
Most IoT protocols utilize TCP/IP as the transport mechanism. TCP/IP is a stream based protocol which does not include any framing information when used for sending messages across the wire. IoT protocols add framing information on top of TCP/IP when transmitting data, making it easy to send packets across the wire. For example, the WebSocket protocol adds a size header to the data, and a WebSocket stack provides a packet based API for the application designer using the stack. Pub/Sub protocols, such as MQTT and SMQ, also provide a packet based API.
Since IoT protocols provide a packet based API, any JSON encoder/decoder can be used for serializing and deserializing structured data sent across the wire. However, in some cases an IoT protocol is overkill and may add unnecessary memory and processing overhead. An IoT product designer may select to directly use TCP/IP as the transport layer for sending structured data across the wire. A standard JSON encoder/decoder cannot be used when using a non-frame based transport layer.
Our open source C JSON library simplifies converting C to JSON and JSON to C. The JSON library includes easy to use APIs for serializing C structures and for deserializing C structures. The JSON data can be sent directly on a non-frame based data stream such as TCP/IP. By eliminating the IoT protocol and directly using TCP/IP, the code size can be as small as 1Kb ROM.
Our C JSON parser is designed to parse data on a non-frame based stream and can correctly parse JSON packets as they trickle in on a raw data stream.
Unlike protocols based on Remote Procedure Calls (RPC), such as HTTP and CoAP, structured data can be sent asynchronously in both directions at any time. This is in stark contrast to HTTP/REST based applications, which require polling for updates. Polling for updates is specifically inefficient when security and encryption are required. To understand why this is such a bad idea, read The scalability problem with using HTTPS/REST for IoT tutorial.
Security can easily be added by using a TLS (SSL) protocol stack. The TLS layer is added in between the JSON encoder/decoder and the TCP/IP stack. Adding security increases the code size, and the size added depends on the TLS stack being used. With SharkSSL, it's possible to limit this code size to just below 20Kb.
If you are interested in building network programs in C/C++ that sends structured data across a raw TCP/IP connection, read the detailed explanation on how to use our JSON encoder/decoder. See the JSON encoder/decoder documentation for details.
For a quick, visual comparison of the two, check out our "too long; didn't read" chart:
HTTP | TCP-JSON |
---|---|
Persistent | |
Partly | Yes |
Duplex | |
Half | Full |
Message Pattern | |
Request-Response | Bi-Directional |
Service Push | |
Not supported | Core feature |
Overhead | |
Moderate to large for micro controllers | Minimal overhead per TCP-JSON message |
You may have an application where AJAX is the preferred communication model. TCP/IP enables us to multiplex data on the same connection, thus we can easily implement an AJAX library on top of TCP/IP and still use the same TCP connection for bi-directional real-time data transfer. See our AJAX over WebSockets tutorial if you are interested in learning more about how to use this JSON C library for AJAX communication.
The GitHub download includes several examples for Windows and Linux. Test the continuously parsing JSON parser using the ready to use IoT example using Linux as follows:
When the m2m example runs, navigate to: realtimelogic.info/IoT/led/json/ and click the link to your connected device.
Check out the free noobs friendly embedded IDE if you are interested in learning how to use this JSON library for encoding and decoding IoT messages.
The JSON library is also used in the Minnow Server reference example, which uses an Embedded WebSocket Server Library and JSON for communication. The Minnow Server example shows how to use the JSON parser in stream mode as WebSocket data trickles into the device. The example also shows how to use a static (non dynamic) allocator with the JSON parser and encoder. The Minnow Server reference example is available on GitHub. The following video shows how the Minnow Server reference example works and how JSON is used for the messaging:
The SMQ publish and subscribe C++ example shows how to use the JSON library for real time communication. The following video shows how to run the example bundle using three terminals on Linux. You would use the same startup sequence on Windows using three console windows.