Microchip® Advanced Software Framework

json.c File Reference

JSON parser.

Copyright (c) 2016-2018 Microchip Technology Inc. and its subsidiaries.

#include "iot/json.h"
#include <string.h>
#include <stdio.h>
#include <errno.h>

Enumerations

enum  json_cmd {
  JSON_CMD_NONE = 0,
  JSON_CMD_ENTER_OBJECT,
  JSON_CMD_EXIT_OBJECT,
  JSON_CMD_ENTER_ARRAY,
  JSON_CMD_EXIT_ARRAY
}
 

Functions

static void _json_parse (char *data, char *ptr, enum json_cmd cmd, struct json_obj *out)
 
static char * _json_read_token (char *buffer, uint32_t buffer_size, char *dest, uint32_t dest_size, enum json_cmd *cmd)
 is pair of name : value. More...
 
int json_create (struct json_obj *obj, const char *data, int data_len)
 Create the JSON data from the string buffer. More...
 
int json_find (struct json_obj *obj, const char *name, struct json_obj *out)
 Find data from the JSON object. More...
 
int json_get_child (struct json_obj *obj, int index, struct json_obj *out)
 Get child data in the JSON object. More...
 
int json_get_child_count (struct json_obj *obj)
 Get child count in the JSON object. More...
 

enum json_cmd
Enumerator
JSON_CMD_NONE 
JSON_CMD_ENTER_OBJECT 
JSON_CMD_EXIT_OBJECT 
JSON_CMD_ENTER_ARRAY 
JSON_CMD_EXIT_ARRAY 

static char* _json_read_token ( char *  buffer,
uint32_t  buffer_size,
char *  dest,
uint32_t  dest_size,
enum json_cmd cmd 
)
static

is pair of name : value.

Parameters
buffer: [in] Read out line
dest: [out] destination buffer
cmd: [out] 0 : none 1: enter object 2: exit object 3: enter array 4: exit array
Returns
: start point of next token

References JSON_CMD_ENTER_ARRAY, JSON_CMD_ENTER_OBJECT, JSON_CMD_EXIT_ARRAY, JSON_CMD_EXIT_OBJECT, JSON_CMD_NONE, and NULL.

Referenced by json_find(), json_get_child(), and json_get_child_count().

int json_create ( struct json_obj obj,
const char *  data,
int  data_len 
)

Create the JSON data from the string buffer.

Parameters
[out]objPointer of JSON token which will be stored json informations.
[in]dataJSON data represented as a string.
[in]data_lenJSON data length.
Returns
0 Success.
otherwise Failed to create.

References json_obj::end_ptr, JSON_TYPE_OBJECT, json_obj::name, NULL, json_obj::o, json_obj::type, and json_obj::value.

Referenced by http_client_callback().

int json_find ( struct json_obj obj,
const char *  name,
struct json_obj out 
)

Find data from the JSON object.

The input should be an object or array. This function supported colon separated search.

If JSON data is as follows {"obj1":{"data1":"value1","data2":"value2","obj2":{"data3":"value3"}}} You can be found value of data3 using the following name variable. "obj1:obj2:data3"

Parameters
[in]objPointer of the parent JSON data.
[in]nameThe name of the item you are looking for.
[in]outPointer of JSON token which will be stored child JSON informations.
Returns
0 Success.
otherwise Failed to create.

References _json_parse(), _json_read_token(), json_obj::end_ptr, JSON_CMD_ENTER_ARRAY, JSON_CMD_ENTER_OBJECT, JSON_CMD_EXIT_ARRAY, JSON_CMD_EXIT_OBJECT, JSON_CMD_NONE, JSON_MAX_TOKEN_SIZE, JSON_TYPE_ARRAY, JSON_TYPE_OBJECT, json_obj::name, NULL, json_obj::o, json_obj::type, and json_obj::value.

Referenced by http_client_callback().

int json_get_child ( struct json_obj obj,
int  index,
struct json_obj out 
)

Get child data in the JSON object.

The input should be an object or array.

Parameters
[in]objPointer of the parent JSON data.
[in]indexIndex which is located in the parent object.
[in]outPointer of JSON token which will be stored child JSON informations.
Returns
0 Success.
otherwise Failed to create.

References _json_parse(), _json_read_token(), json_obj::end_ptr, JSON_CMD_ENTER_ARRAY, JSON_CMD_ENTER_OBJECT, JSON_CMD_EXIT_ARRAY, JSON_CMD_EXIT_OBJECT, JSON_CMD_NONE, JSON_MAX_TOKEN_SIZE, JSON_TYPE_ARRAY, JSON_TYPE_OBJECT, NULL, json_obj::o, json_obj::type, and json_obj::value.

int json_get_child_count ( struct json_obj obj)

Get child count in the JSON object.

The input should be an object or array.

Parameters
[in]objPointer of the parent JSON data.
Returns
0 Success.
otherwise Failed to create.

References _json_read_token(), json_obj::end_ptr, JSON_CMD_ENTER_ARRAY, JSON_CMD_ENTER_OBJECT, JSON_CMD_EXIT_ARRAY, JSON_CMD_EXIT_OBJECT, JSON_CMD_NONE, JSON_TYPE_ARRAY, JSON_TYPE_OBJECT, NULL, json_obj::o, json_obj::type, and json_obj::value.