JSON Decoder for PIC® and AVR® Devices

TB3239

JSON Decoder for PIC? and AVR? Devices

Introduction

Author: Alexandru Niculae, Microchip Technology Inc.

This document describes the use and implementation of a JSON subset decoder, aimed for embedded devices such as PIC? and AVR? microcontrollers. The decoder translates the string format of a JSON object into a C data structure representation. This way programmers can access the key-value pairs. Using JSON objects makes it easy to interconnect applications. The JSON Decoder code is available on GitHub.

? 2020 Microchip Technology Inc.

Technical Brief

DS90003239A-page 1

TB3239

Table of Contents

Introduction.....................................................................................................................................................1 1. Overview................................................................................................................................................. 3 2. API.......................................................................................................................................................... 4 3. Implementation........................................................................................................................................7

3.1. Input Buffer...................................................................................................................................7 3.2. Decoding...................................................................................................................................... 7 3.3. Internal Representation................................................................................................................ 8 3.4. Encoding...................................................................................................................................... 9 4. Demo: Controlling the Four LEDs on AVR-IoT from the PC..................................................................10 4.1. Instructions................................................................................................................................. 10 5. Demo: Generic Cloud Communication with an IoT Node..................................................................... 15 6. Appendix............................................................................................................................................... 16 The Microchip Website.................................................................................................................................18 Product Change Notification Service............................................................................................................18 Customer Support........................................................................................................................................ 18 Microchip Devices Code Protection Feature................................................................................................ 18 Legal Notice................................................................................................................................................. 18 Trademarks.................................................................................................................................................. 19 Quality Management System....................................................................................................................... 19 Worldwide Sales and Service.......................................................................................................................20

? 2020 Microchip Technology Inc.

Technical Brief

DS90003239A-page 2

TB3239

Overview

1. Overview

JSON is the most used data serialization format on the web and on desktop applications. Since it has an implementation in virtually all modern programming language, it is the perfect tool for data interchange between applications. It has a text format that is easy for humans to read and write and easy for machines to parse and generate. A JSON object is a set of key-value pairs. While keys can only be strings, values can be a lot of things. This decoder implementation supports string, number, and object values. Note: To keep this library suited for low-memory devices, only a subset of features was selected to be implemented, therefore, array, boolean, and null values are not supported. The following block of code shows an example usage of this decoder. Section 2. API describes the API functions in detail.

#define TEST_JSON "{\"main\":{\"key\" : 10,\"foo\":\"bar\"}, \"alt\":2}" ... jsonNode_t *root, *objmain; char str[64], foo[10]; int alt; memcpy(str, TEST_JSON, sizeof(TEST_JSON)); JSON_DECODER_fromString(str) JSON_DECODER_getRoot(&root); JSON_DECODER_getObject(root, "main", &objmain); JSON_DECODER_getNumber(root, "alt", &alt) JSON_DECODER_getString(objmain, "foo", sizeof(foo), foo)

Note: The outermost object in a JSON is called the root.

? 2020 Microchip Technology Inc.

Technical Brief

DS90003239A-page 3

TB3239

API

2. API

Refer to this section for detailed explanation of the public functions in the JSON Decoder.

jsonDecoderStatus_t JSON_DECODER_fromString(char *str)

Description Parses a JSON string into a C representation. It supports string, number, and object values.

Parameters Name str

Description

Pointer to an existing string representation of a JSON object. After parsing, the string will have been altered.

Return Value Name JSON_DECODER_OK JSON_DECODER_BAD_FORMAT

Description Decoding was successful The input is invalid

JSON_DECODER_getRoot(jsonNode_t **pNode)

Description Finds the outermost object in the JSON, called the root. This function must not be called before JSON_DECODER_fromString.

Parameters Name pNode

Description Pointer to a jsonNode_t

Return Value Name JSON_DECODER_OK

Description pNode now points to the root

jsonDecoderStatus_t JSON_DECODER_getObject(jsonNode_t *current, char *key, jsonNode_t **pNode)

Description Finds a JSON object by key in another JSON object. This function must not be called before JSON_DECODER_fromString.

Parameters Name current

Description Pointer to a jsonNode_t

? 2020 Microchip Technology Inc.

Technical Brief

DS90003239A-page 4

...........continued Name key pNode

Return Value Name JSON_DECODER_OK JSON_DECODER_KEY_NOT_FOUND

Description The key of the object to find Pointer to the object, if found

TB3239

API

Description Object was found and pNode now points to it The specified key does not exist

jsonDecoderStatus_t JSON_DECODER_getString(jsonNode_t *current, char *key, uint8_t size, char *pVal)

Description Finds a string by key in a JSON object. This function must not be called before JSON_DECODER_fromString.

Parameters Name current key size

pVal

Description The JSON object to search into The key of the string to find Maximum size of the string. Should not be bigger than the length of the pVal buffer Pointer to the string, if found

Return Value Name JSON_DECODER_OK JSON_DECODER_KEY_NOT_FOUND

Description String was found and pVal now points to it The specified key does not exist

jsonDecoderStatus_t JSON_DECODER_getNumber(jsonNode_t *current, char *key, int *pVal)

Description Finds a number by key in a JSON object. This function must not be called before JSON_DECODER_fromString.

Parameters Name current key pVal

Description The JSON object to search into The key of the string to find Pointer to the number, if found

? 2020 Microchip Technology Inc.

Technical Brief

DS90003239A-page 5

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download