JSON Parser

From openPicus Wiki
Jump to: navigation, search

Contents

Introduction

The JSON library allows to parse and create JSON strings on Flyport Modules.
JSON or JavaScript Object Notation, is a lightweight text-based open standard designed for human-readable data interchange.

To find out more about JSON please click here

JSON library files

JSON library is composed of cJSON.c and cJSON.h files. The heap.s file is needed to reserve an area of Flyport memory used for dynamic memory allocation at runtime. Please add also this file to your project if requested when compiling your project.

CLICK HERE TO DOWNLOAD JSON LIBRARY FILES



JSON APIs

The main data type to use JSON library is struct cJSON

The following tables describe JSON related APIs

Parsing

Function Description
 cJSON *cJSON_Parse(const char *value) This function parses a JSON string

const char *value - pointer to a cJSON string to be parsed

Returns cJSON *- pointer to the cJSON parsed

NOTE Call cJSON_Delete(cJSON *c) function when the JSON is not longer needed to free the memory.

 void   cJSON_Delete(cJSON *c) This function deletes a JSON to free memory.

cJSON *c – pointer to the cJSON to be deleted

 char  *cJSON_Print(cJSON *item) This function prints a JSON in a string in a formatted style.

cJSON * item - Pointer to a cJSON to be printed

Returns char * - pointer to the string

NOTE Call free(char *) function when the string is no longer needed to free memory.

 char  *cJSON_PrintUnformatted(cJSON *item) This function prints a JSON in a string in a unformatted style.

cJSON * item - Pointer to a cJSON to be printed

Returns char * - pointer to the string

NOTE Call free(char *) function when the string is no longer needed to free memory.

 const char *cJSON_GetErrorPtr(void) This function returns a pointer to a string when an error was encountered during the parsing.

Returns const char* - is the pointer to a error string

NOTE Looking some chars back can give a better help to find the error encountered.



Getting and item

Function Description
 cJSON *cJSON_GetObjectItem(cJSON *object,const char *string) This function returns a pointer to an item in a JSON.

cJSON *object – pointer to a cJSON. const char *string – pointer to the item's name.

Returns cJSON * - pointer to the item found

 int cJSON_GetArraySize(cJSON *array) This function returns the size of an array in a JSON

cJSON * array – pointer to an array.

Returns int item - size of the array

 cJSON *cJSON_GetArrayItem(cJSON *array,int item) This function returns a pointer to an item in an array.

cJSON * array – pointer to an array. int item – number of the item in the array

Returns cJSON * - pointer to the item in the array.



Deleting/Detaching an item

Function Description
 void cJSON_DeleteItemFromObject(cJSON *object,const char *string) This function deletes an item from JSON

cJSON * object – pointer to the cJSON const char *string – pointer to the item to be deleted

 cJSON *cJSON_DetachItemFromObject(cJSON *object,const char *string) This function detaches an item from JSON.

cJSON * object – pointer to the cJSON. const char *string – pointer to the item to be detached.

Returns cJSON * - pointer to the item detached.



Creating and adding a JSON object/array

Name Function Description
 cJSON *cJSON_CreateObject() This function creates a JSON

Returns cJSON * - pointer to the cJSON created

 cJSON *cJSON_CreateArray() This function creates an array.

Returns cJSON * - pointer to the array created



Examples

Examples are available on library download repo

Personal tools
Namespaces

Variants
Actions
START HERE
DEVELOPMENT
HARDWARE INFO
RESOURCES
PHASED OUT
Toolbox