modbusd
modbus master daemon
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
json.c File Reference

cJSON helper functions More...

#include <stdio.h>
#include <stdlib.h>
#include "json.h"
Include dependency graph for json.c:

Functions

char * json_get_char (cJSON *inJson, const char *key)
 Get char string via key from cJSON object. More...
 
int json_get_int (cJSON *inJson, const char *key)
 Get integer value via key from cJSON object. More...
 
void json_set_int (cJSON *inJson, const char *key, int value)
 Set integer value via key to existed cJSON object. More...
 
double json_get_double (cJSON *inJson, const char *key)
 Get double integer value via key from cJSON object. More...
 
void json_set_double (cJSON *inJson, const char *key, double value)
 Set double integer value via key to existed cJSON object. More...
 
long json_get_long (cJSON *inJson, const char *key)
 Get long integer value via key from cJSON object. More...
 
int file_to_json (const char *fname, cJSON **outJson)
 Load JSON file to cJSON object. More...
 
int json_to_file (const char *fname, cJSON *inJson)
 Save cJSON object to JSON file. More...
 

Detailed Description

cJSON helper functions

Author
Taka Wang

Function Documentation

int file_to_json ( const char *  fname,
cJSON **  outJson 
)

Load JSON file to cJSON object.

Parameters
fnameFile name string.
outJsonPointer to cJSON output object.
Returns
Success or not.

References cJSON_Parse().

43 {
44  FILE *fPtr = fopen (fname,"rb");
45  if (fPtr)
46  {
47  fseek(fPtr, 0, SEEK_END);
48  long len = ftell (fPtr);
49  fseek(fPtr, 0, SEEK_SET);
50  char *data = (char*) malloc (len+1);
51  if (data != NULL)
52  {
53  int _ = fread (data, 1, len, fPtr); _=_;
54  fclose (fPtr);
55  *outJson = cJSON_Parse (data);
56  free (data);
57  return outJson ? 0 : 1;
58  }
59  else
60  {
61  return -1;
62  }
63 
64  }
65  else
66  {
67  return -1;
68  }
69 }
cJSON * cJSON_Parse(const char *value)
Definition: cJSON.c:343

Here is the call graph for this function:

char* json_get_char ( cJSON inJson,
const char *  key 
)

Get char string via key from cJSON object.

Parameters
inJsoncJSON object.
keyJson key.
Returns
C char string.

References cJSON_GetObjectItem(), and cJSON::valuestring.

Referenced by main(), mbtcp_cmd_hanlder(), mbtcp_multi_write_req_fn(), mbtcp_read_bit_req_fn(), mbtcp_read_reg_req_fn(), and mbtcp_single_write_req_fn().

12 {
13  return cJSON_GetObjectItem (inJson, key)->valuestring;
14 }
cJSON * cJSON_GetObjectItem(cJSON *object, const char *string)
Definition: cJSON.c:666
void * key
Definition: uthash.h:1069
char * valuestring
Definition: cJSON.h:51

Here is the call graph for this function:

Here is the caller graph for this function:

double json_get_double ( cJSON inJson,
const char *  key 
)

Get double integer value via key from cJSON object.

Parameters
inJsoncJSON object.
keyJson key.
Returns
Double integer value.

References cJSON_GetObjectItem(), and cJSON::valuedouble.

27 {
28  return cJSON_GetObjectItem (inJson, key)->valuedouble;
29 }
cJSON * cJSON_GetObjectItem(cJSON *object, const char *string)
Definition: cJSON.c:666
void * key
Definition: uthash.h:1069
double valuedouble
Definition: cJSON.h:58

Here is the call graph for this function:

int json_get_int ( cJSON inJson,
const char *  key 
)

Get integer value via key from cJSON object.

Parameters
inJsoncJSON object.
keyJson key.
Returns
Integer.

References cJSON_GetObjectItem(), and cJSON::valueint.

Referenced by main(), mbtcp_cmd_hanlder(), mbtcp_multi_write_req_fn(), mbtcp_read_bit_req_fn(), mbtcp_read_reg_req_fn(), and mbtcp_single_write_req_fn().

17 {
18  return cJSON_GetObjectItem (inJson, key)->valueint;
19 }
cJSON * cJSON_GetObjectItem(cJSON *object, const char *string)
Definition: cJSON.c:666
void * key
Definition: uthash.h:1069
int valueint
Definition: cJSON.h:57

Here is the call graph for this function:

Here is the caller graph for this function:

long json_get_long ( cJSON inJson,
const char *  key 
)

Get long integer value via key from cJSON object.

Parameters
inJsoncJSON object.
keyJson key.
Returns
Long.

References cJSON_GetObjectItem(), and cJSON::valuedouble.

Referenced by main().

37 {
38  // convert double float to long integer
39  return (long)cJSON_GetObjectItem(inJson, key)->valuedouble;
40 }
cJSON * cJSON_GetObjectItem(cJSON *object, const char *string)
Definition: cJSON.c:666
void * key
Definition: uthash.h:1069
double valuedouble
Definition: cJSON.h:58

Here is the call graph for this function:

Here is the caller graph for this function:

void json_set_double ( cJSON inJson,
const char *  key,
double  value 
)

Set double integer value via key to existed cJSON object.

Parameters
inJsoncJSON object.
keyJson key.
doubleDouble integer value.
Returns
Void.

References cJSON_GetObjectItem(), and cJSON::valuedouble.

32 {
33  cJSON_GetObjectItem (inJson, key)->valuedouble = value;
34 }
cJSON * cJSON_GetObjectItem(cJSON *object, const char *string)
Definition: cJSON.c:666
void * key
Definition: uthash.h:1069
double valuedouble
Definition: cJSON.h:58

Here is the call graph for this function:

void json_set_int ( cJSON inJson,
const char *  key,
int  value 
)

Set integer value via key to existed cJSON object.

Parameters
inJsoncJSON object.
keyJson key.
valueInteger value.
Returns
Void.

References cJSON_GetObjectItem(), and cJSON::valueint.

22 {
23  cJSON_GetObjectItem (inJson, key)->valueint = value;
24 }
cJSON * cJSON_GetObjectItem(cJSON *object, const char *string)
Definition: cJSON.c:666
void * key
Definition: uthash.h:1069
int valueint
Definition: cJSON.h:57

Here is the call graph for this function:

int json_to_file ( const char *  fname,
cJSON inJson 
)

Save cJSON object to JSON file.

Parameters
fnameFile name string.
inJsoncJSON input object.
Returns
Success or not.

References cJSON_Print().

72 {
73  FILE *fPtr = fopen(fname, "w");
74  if (fPtr)
75  {
76  fprintf (fPtr, "%s", cJSON_Print (inJson));
77  fclose (fPtr);
78  return 0;
79  }
80  else
81  {
82  return -1;
83  }
84 }
char * cJSON_Print(cJSON *item)
Definition: cJSON.c:346

Here is the call graph for this function: