AppDynamics IoT C++ SDK
AppDynamics IoT C++ library contains code that facilitates capturing availability, usage, network performance and errors of an IoT Application.
json_serializer.cpp File Reference
#include <stdlib.h>
#include <string>
#include <inttypes.h>
#include "json_serializer.hpp"
#include "log.hpp"

Macros

#define __STDC_FORMAT_MACROS
 
#define INITIAL_JSON_SIZE   1024
 
#define START_OBJECT_CHAR   '{'
 
#define START_ARRAY_CHAR   '['
 
#define END_OBJECT_CHAR   '}'
 
#define END_ARRAY_CHAR   ']'
 
#define JSON_DELIMITER   ','
 

Functions

static appd_iot_error_code_t appd_iot_check_and_expand_json_buf_size (json_t *json, size_t len)
 Checks and expands the size of json buf allocated. More...
 
static appd_iot_error_code_t appd_iot_json_start (json_t *json, char begin, const char *name)
 starts json blob by adding the begin character. Typically it could be json object '{' or json array '['. If a name is given it will be "name":{ or "name":[ More...
 
static appd_iot_error_code_t appd_iot_json_end (json_t *json, char end)
 ends json object with end character. Typically it is either '}' or ']'. More...
 
static appd_iot_error_code_t appd_iot_json_add_key_value (json_t *json, const char *key, const void *value, appd_iot_data_types_t type)
 adds key:value pair to json object More...
 
static appd_iot_error_code_t appd_iot_json_add_value (json_t *json, const void *value, appd_iot_data_types_t type)
 adds value to json object. This function is typically used to add string, integer, double, boolean values to JSON arrays More...
 
json_tappd_iot_json_init ()
 Creates, Initializes and returns a new json struct. More...
 
appd_iot_error_code_t appd_iot_json_start_array (json_t *json, const char *array_name)
 starts json array by adding char '[' to json buf. If a name is given it will be "name":[ More...
 
appd_iot_error_code_t appd_iot_json_start_object (json_t *json, const char *object_name)
 starts json object by adding char '{' to json buf. If a name is given it will be "name":{ More...
 
appd_iot_error_code_t appd_iot_json_end_object (json_t *json)
 ends json object by adding char '}' to json buf. More...
 
appd_iot_error_code_t appd_iot_json_end_array (json_t *json)
 ends json object by adding char ']' to json buf. More...
 
std::string appd_iot_add_escape_char (const void *value)
 adds escape character to the string if applicable. Unicode characters are currently not handled. More...
 
appd_iot_error_code_t appd_iot_convert_to_string (char *buf, size_t bufsize, const void *value, appd_iot_data_types_t type)
 convert integer, double and boolean to string More...
 
appd_iot_error_code_t appd_iot_json_add_string_key_value (json_t *json, const char *key, const char *strval)
 adds key:value pair to json object with value as string More...
 
appd_iot_error_code_t appd_iot_json_add_integer_key_value (json_t *json, const char *key, int64_t intval)
 adds key:value pair to json object with value as 64 bit integer More...
 
appd_iot_error_code_t appd_iot_json_add_double_key_value (json_t *json, const char *key, double doubleval)
 adds double value to json object More...
 
appd_iot_error_code_t appd_iot_json_add_boolean_key_value (json_t *json, const char *key, bool boolval)
 adds key:value pair to json object with value as boolean More...
 
appd_iot_error_code_t appd_iot_json_add_string_value (json_t *json, const char *value)
 adds value to json object. This function is typically used to add string values to JSON arrays More...
 
appd_iot_error_code_t appd_iot_json_add_integer_value (json_t *json, int64_t intval)
 adds value to json object. This function is typically used to add integer values to JSON arrays More...
 
appd_iot_error_code_t appd_iot_json_add_double_value (json_t *json, double doubleval)
 adds value to json object. This function is typically used to add double values to JSON arrays More...
 
appd_iot_error_code_t appd_iot_json_add_boolean_value (json_t *json, bool boolval)
 adds value to json object. This function is typically used to add boolean values to JSON arrays More...
 
const char * appd_iot_json_get_string (json_t *json)
 returns the json string constructed so far. More...
 
const char * appd_iot_json_pretty_print (json_t *json)
 format and returns json string with line breaks, indentiation at start/end of objects/arrays More...
 
void appd_iot_json_free (json_t *json)
 frees json structure More...
 

Variables

static const char * comma = ","
 

Macro Definition Documentation

◆ __STDC_FORMAT_MACROS

#define __STDC_FORMAT_MACROS

◆ INITIAL_JSON_SIZE

#define INITIAL_JSON_SIZE   1024

◆ START_OBJECT_CHAR

#define START_OBJECT_CHAR   '{'

◆ START_ARRAY_CHAR

#define START_ARRAY_CHAR   '['

◆ END_OBJECT_CHAR

#define END_OBJECT_CHAR   '}'

◆ END_ARRAY_CHAR

#define END_ARRAY_CHAR   ']'

◆ JSON_DELIMITER

#define JSON_DELIMITER   ','

Function Documentation

◆ appd_iot_check_and_expand_json_buf_size()

static appd_iot_error_code_t appd_iot_check_and_expand_json_buf_size ( json_t json,
size_t  len 
)
static

Checks and expands the size of json buf allocated.

Parameters
jsonstruct which contains the json buf
lenindicates size of new bytes to be added
Returns
appd_iot_error_code_t indicating function execution status
760 {
761  if (json == NULL)
762  {
763  return APPD_IOT_ERR_NULL_PTR;
764  }
765 
766  size_t new_len = json->len + len;
767 
768  if (new_len >= json->max_len)
769  {
770  size_t newsize = json->max_len * 2;
771 
772  while (newsize < new_len)
773  {
774  newsize = newsize * 2;
775  }
776 
777  char* tmp = (char*)realloc(json->buf, newsize);
778 
779  if (tmp == NULL)
780  {
781  appd_iot_log(APPD_IOT_LOG_ERROR, "Realloc Failed when expanding json buf size");
782  return APPD_IOT_ERR_NULL_PTR;
783  }
784 
785  json->buf = tmp;
786  memset(json->buf + json->max_len, 0, (newsize - json->max_len));
787  json->max_len = newsize;
788  }
789 
790  return APPD_IOT_SUCCESS;
791 }
char * buf
Definition: json_serializer.hpp:43
Definition: appd_iot_def.h:44
void appd_iot_log(appd_iot_log_level_t log_level, const char *format,...)
Reads log message, appends log header and triggers log write callback function.
Definition: log.cpp:89
size_t len
Definition: json_serializer.hpp:41
size_t max_len
Definition: json_serializer.hpp:42
Definition: appd_iot_def.h:30
Definition: appd_iot_def.h:99

◆ appd_iot_json_start()

static appd_iot_error_code_t appd_iot_json_start ( json_t json,
char  begin,
const char *  name 
)
static

starts json blob by adding the begin character. Typically it could be json object '{' or json array '['. If a name is given it will be "name":{ or "name":[

Parameters
begincontains the opening character
namecontains the key to json object or array
jsonstruct which contains the json buf
Returns
appd_iot_error_code_t indicating function execution status
82 {
83  if (json == NULL)
84  {
85  return APPD_IOT_ERR_NULL_PTR;
86  }
87 
88  if (begin != START_OBJECT_CHAR && begin != START_ARRAY_CHAR)
89  {
90  appd_iot_log(APPD_IOT_LOG_ERROR, "Unexpected begin character:%c", begin);
92  }
93 
94  size_t len = 1; //there will be atleast 1 char and a terminating null character
95  const char* eol = &comma[1]; //initialized to null character '/0'
96 
97  if (name != NULL)
98  {
99  len = len + strlen(name) + 3; //2 double quotes and 1 colon "name:"begin
100  }
101 
102  //add comma at the end if last operation is not start
103  if (json->last_op != START_OBJECT && json->last_op != START_ARRAY && json->last_op != INIT)
104  {
105  len = len + 1;
106  eol = &comma[0];
107  }
108 
110 
111  if (retcode != APPD_IOT_SUCCESS)
112  {
113  return retcode;
114  }
115 
116  if (name != NULL)
117  {
118  snprintf(json->buf + json->len, len + 1, "%s\"%s\":%c", eol, name, begin);
119  }
120  else
121  {
122  snprintf(json->buf + json->len, len + 1, "%s%c", eol, begin);
123  }
124 
125  json->len = json->len + len;
126  json->buf[json->len] = '\0';
127 
128  if (begin == START_ARRAY_CHAR)
129  {
130  json->last_op = START_ARRAY;
131  }
132  else if (begin == START_OBJECT_CHAR)
133  {
134  json->last_op = START_OBJECT;
135  }
136 
137  return retcode;
138 }
char * buf
Definition: json_serializer.hpp:43
Definition: appd_iot_def.h:44
void appd_iot_log(appd_iot_log_level_t log_level, const char *format,...)
Reads log message, appends log header and triggers log write callback function.
Definition: log.cpp:89
size_t len
Definition: json_serializer.hpp:41
Definition: json_serializer.hpp:30
Definition: json_serializer.hpp:28
appd_iot_error_code_t
Error Code Enums for SDK log.
Definition: appd_iot_def.h:27
Definition: json_serializer.hpp:29
json_ops_t last_op
Definition: json_serializer.hpp:45
#define START_ARRAY_CHAR
Definition: json_serializer.cpp:30
Definition: appd_iot_def.h:32
static const char * comma
Definition: json_serializer.cpp:43
#define START_OBJECT_CHAR
Definition: json_serializer.cpp:29
Definition: appd_iot_def.h:30
Definition: appd_iot_def.h:99
static appd_iot_error_code_t appd_iot_check_and_expand_json_buf_size(json_t *json, size_t len)
Checks and expands the size of json buf allocated.
Definition: json_serializer.cpp:759

◆ appd_iot_json_end()

static appd_iot_error_code_t appd_iot_json_end ( json_t json,
char  end 
)
static

ends json object with end character. Typically it is either '}' or ']'.

Parameters
jsoncontains the json struct to which object end is added
Returns
appd_iot_error_code_t indicating function execution status
170 {
171  if (json == NULL)
172  {
173  return APPD_IOT_ERR_NULL_PTR;
174  }
175 
176  if (end != END_OBJECT_CHAR && end != END_ARRAY_CHAR)
177  {
178  appd_iot_log(APPD_IOT_LOG_ERROR, "Unexpected end character:%c", end);
180  }
181 
182  //check if space available for end character of size 1 byte
184 
185  if (retcode != APPD_IOT_SUCCESS)
186  {
187  return retcode;
188  }
189 
190  json->buf[json->len] = end;
191  json->len++;
192 
193  if (end == END_OBJECT_CHAR)
194  {
195  json->last_op = END_OBJECT;
196  }
197  else if (end == END_ARRAY_CHAR)
198  {
199  json->last_op = END_ARRAY;
200  }
201 
202  return APPD_IOT_SUCCESS;
203 }
char * buf
Definition: json_serializer.hpp:43
Definition: appd_iot_def.h:44
void appd_iot_log(appd_iot_log_level_t log_level, const char *format,...)
Reads log message, appends log header and triggers log write callback function.
Definition: log.cpp:89
size_t len
Definition: json_serializer.hpp:41
#define END_ARRAY_CHAR
Definition: json_serializer.cpp:32
appd_iot_error_code_t
Error Code Enums for SDK log.
Definition: appd_iot_def.h:27
Definition: json_serializer.hpp:33
json_ops_t last_op
Definition: json_serializer.hpp:45
#define END_OBJECT_CHAR
Definition: json_serializer.cpp:31
Definition: json_serializer.hpp:32
Definition: appd_iot_def.h:32
Definition: appd_iot_def.h:30
Definition: appd_iot_def.h:99
static appd_iot_error_code_t appd_iot_check_and_expand_json_buf_size(json_t *json, size_t len)
Checks and expands the size of json buf allocated.
Definition: json_serializer.cpp:759

◆ appd_iot_json_add_key_value()

static appd_iot_error_code_t appd_iot_json_add_key_value ( json_t json,
const char *  key,
const void *  value,
appd_iot_data_types_t  type 
)
static

adds key:value pair to json object

Parameters
jsonstruct which contains the json buf
keyto be added
valueto be added
typeindicates if the value is string, integer, double or boolean data type
Returns
appd_iot_error_code_t indicating function execution status
340 {
341  if ((json == NULL) || (key == NULL) || (value == NULL))
342  {
343  return APPD_IOT_ERR_NULL_PTR;
344  }
345 
346  char* strval;
347  size_t value_size = 256; //default size for integer, double and boolean types
348 
349  //for string, set size to twice the original length to accomodate any escape characters.
350  if (type == APPD_IOT_STRING)
351  {
352  value_size = 2 * strlen((char*)value);
353  }
354 
355  strval = (char*)calloc(1, value_size);
356 
357  if (strval == NULL)
358  {
359  appd_iot_log(APPD_IOT_LOG_ERROR, "Failed to allocate memory while adding key-value to json");
360  return APPD_IOT_ERR_NULL_PTR;
361  }
362 
363  appd_iot_error_code_t retcode = appd_iot_convert_to_string(strval, value_size, value, type);
364 
365  if (retcode != APPD_IOT_SUCCESS)
366  {
367  free(strval);
368  return retcode;
369  }
370 
371  size_t len = strlen(key) + strlen(strval);
372 
373  //add space for extra characters doublequote(") and colon(:)
374  if (type == APPD_IOT_STRING)
375  {
376  len = len + 5; //4 doublequotes and 1 colon "key":"value"
377  }
378  else
379  {
380  len = len + 3; //2 doublequotes and 1 colon "key":value
381  }
382 
383  const char* eol = &comma[1]; //initialized to null character '/0'
384 
385  //add comma at the end if last operation is not start
386  if (json->last_op != START_OBJECT && json->last_op != START_ARRAY && json->last_op != INIT)
387  {
388  len = len + 1;
389  eol = &comma[0];
390  }
391 
392  retcode = appd_iot_check_and_expand_json_buf_size(json, len);
393 
394  if (retcode != APPD_IOT_SUCCESS)
395  {
396  free(strval);
397  return retcode;
398  }
399 
400  switch (type)
401  {
402  case APPD_IOT_STRING:
403  //snprintf copies string of size 'len' into buffer
404  snprintf(json->buf + json->len, len + 1, "%s\"%s\":\"%s\"", eol, key, strval);
405  break;
406 
407  case APPD_IOT_INTEGER:
408  case APPD_IOT_DOUBLE:
409  case APPD_IOT_BOOLEAN:
410  snprintf(json->buf + json->len, len + 1, "%s\"%s\":%s", eol, key, strval);
411  break;
412 
413  default:
414  appd_iot_log(APPD_IOT_LOG_WARN, "Invalid Data Type");
415  free(strval);
417 
418  }
419 
420  json->len = json->len + len;
421  json->buf[json->len] = '\0';
422  json->last_op = ADD_DATA;
423 
424  free(strval);
425 
426  return retcode;
427 }
char * buf
Definition: json_serializer.hpp:43
Definition: appd_iot_def.h:44
void appd_iot_log(appd_iot_log_level_t log_level, const char *format,...)
Reads log message, appends log header and triggers log write callback function.
Definition: log.cpp:89
size_t len
Definition: json_serializer.hpp:41
appd_iot_error_code_t appd_iot_convert_to_string(char *buf, size_t bufsize, const void *value, appd_iot_data_types_t type)
convert integer, double and boolean to string
Definition: json_serializer.cpp:295
Definition: appd_iot_def.h:101
Definition: appd_iot_def.h:192
Definition: json_serializer.hpp:30
Definition: json_serializer.hpp:28
appd_iot_error_code_t
Error Code Enums for SDK log.
Definition: appd_iot_def.h:27
Definition: appd_iot_def.h:194
Definition: json_serializer.hpp:29
Definition: json_serializer.hpp:31
json_ops_t last_op
Definition: json_serializer.hpp:45
Definition: appd_iot_def.h:188
Definition: appd_iot_def.h:190
Definition: appd_iot_def.h:32
static const char * comma
Definition: json_serializer.cpp:43
Definition: appd_iot_def.h:30
Definition: appd_iot_def.h:99
static appd_iot_error_code_t appd_iot_check_and_expand_json_buf_size(json_t *json, size_t len)
Checks and expands the size of json buf allocated.
Definition: json_serializer.cpp:759

◆ appd_iot_json_add_value()

static appd_iot_error_code_t appd_iot_json_add_value ( json_t json,
const void *  value,
appd_iot_data_types_t  type 
)
static

adds value to json object. This function is typically used to add string, integer, double, boolean values to JSON arrays

Parameters
jsonstruct which contains the json buf
valueto be added
typeindicates if the value is string, integer, double or boolean data type
Returns
appd_iot_error_code_t indicating function execution status
487 {
488  if ((json == NULL) || (value == NULL))
489  {
490  return APPD_IOT_ERR_NULL_PTR;
491  }
492 
493  char* strval;
494  size_t value_size = 256; //default size for integer, double and boolean types
495 
496  //for string, set size to twice the original length to accomodate any escape characters.
497  if (type == APPD_IOT_STRING)
498  {
499  value_size = 2 * strlen((char*)value);
500  }
501 
502  strval = (char*)calloc(1, value_size);
503 
504  if (strval == NULL)
505  {
506  appd_iot_log(APPD_IOT_LOG_ERROR, "Failed to allocate memory while adding key-value to json");
507  return APPD_IOT_ERR_NULL_PTR;
508  }
509 
510  appd_iot_error_code_t retcode = appd_iot_convert_to_string(strval, value_size, value, type);
511 
512  if (retcode != APPD_IOT_SUCCESS)
513  {
514  free(strval);
515  return retcode;
516  }
517 
518  size_t len = strlen(strval);
519 
520  //add space for extra characters doublequote(")
521  if (type == APPD_IOT_STRING)
522  {
523  len = len + 2; //2 doublequotes "value"
524  }
525 
526  char eol[2];
527  eol[0] = '\0';
528 
529  //add comma at the end if last operation is not start
530  if (json->last_op != START_OBJECT && json->last_op != START_ARRAY && json->last_op != INIT)
531  {
532  len = len + 1;
533  eol[0] = ',';
534  eol[1] = '\0';
535  }
536 
537  retcode = appd_iot_check_and_expand_json_buf_size(json, len);
538 
539  if (retcode != APPD_IOT_SUCCESS)
540  {
541  free(strval);
542  return retcode;
543  }
544 
545  switch (type)
546  {
547  case APPD_IOT_STRING:
548  //snprintf copies string of len into buffer
549  snprintf(json->buf + json->len, len + 1, "%s\"%s\"", eol, strval);
550  break;
551 
552  case APPD_IOT_INTEGER:
553  case APPD_IOT_DOUBLE:
554  case APPD_IOT_BOOLEAN:
555  snprintf(json->buf + json->len, len + 1, "%s\%s", eol, strval);
556  break;
557 
558  default:
559  appd_iot_log(APPD_IOT_LOG_WARN, "Invalid Data Type");
560  free(strval);
562 
563  }
564 
565  json->len = json->len + len;
566  json->buf[json->len] = '\0';
567  json->last_op = ADD_DATA;
568 
569  free(strval);
570 
571  return retcode;
572 }
char * buf
Definition: json_serializer.hpp:43
Definition: appd_iot_def.h:44
void appd_iot_log(appd_iot_log_level_t log_level, const char *format,...)
Reads log message, appends log header and triggers log write callback function.
Definition: log.cpp:89
size_t len
Definition: json_serializer.hpp:41
appd_iot_error_code_t appd_iot_convert_to_string(char *buf, size_t bufsize, const void *value, appd_iot_data_types_t type)
convert integer, double and boolean to string
Definition: json_serializer.cpp:295
Definition: appd_iot_def.h:101
Definition: appd_iot_def.h:192
Definition: json_serializer.hpp:30
Definition: json_serializer.hpp:28
appd_iot_error_code_t
Error Code Enums for SDK log.
Definition: appd_iot_def.h:27
Definition: appd_iot_def.h:194
Definition: json_serializer.hpp:29
Definition: json_serializer.hpp:31
json_ops_t last_op
Definition: json_serializer.hpp:45
Definition: appd_iot_def.h:188
Definition: appd_iot_def.h:190
Definition: appd_iot_def.h:32
Definition: appd_iot_def.h:30
Definition: appd_iot_def.h:99
static appd_iot_error_code_t appd_iot_check_and_expand_json_buf_size(json_t *json, size_t len)
Checks and expands the size of json buf allocated.
Definition: json_serializer.cpp:759

◆ appd_iot_json_init()

json_t* appd_iot_json_init ( void  )

Creates, Initializes and returns a new json struct.

Returns
json_t pointer to the newly created json object
50 {
51  json_t* json = (json_t*)calloc(1, sizeof(json_t));
52 
53  if (json == NULL)
54  {
55  return NULL;
56  }
57 
58  json->buf = (char*)calloc(1, INITIAL_JSON_SIZE + 1);
59 
60  if (json->buf == NULL)
61  {
62  free(json);
63  return NULL;
64  }
65 
66  json->max_len = INITIAL_JSON_SIZE;
67  json->last_op = INIT;
68 
69  return json;
70 }
char * buf
Definition: json_serializer.hpp:43
size_t max_len
Definition: json_serializer.hpp:42
Definition: json_serializer.hpp:28
json_ops_t last_op
Definition: json_serializer.hpp:45
#define INITIAL_JSON_SIZE
Definition: json_serializer.cpp:28
JSON struct which holds json string in a buffer.
Definition: json_serializer.hpp:39

◆ appd_iot_json_start_array()

appd_iot_error_code_t appd_iot_json_start_array ( json_t json,
const char *  array_name 
)

starts json array by adding char '[' to json buf. If a name is given it will be "name":[

Parameters
jsonstruct which contains the json buf
array_namehas the string for the key to json array
Returns
appd_iot_error_code_t indicating function execution status
148 {
149  return appd_iot_json_start(json, START_ARRAY_CHAR, array_name);
150 }
#define START_ARRAY_CHAR
Definition: json_serializer.cpp:30
static appd_iot_error_code_t appd_iot_json_start(json_t *json, char begin, const char *name)
starts json blob by adding the begin character. Typically it could be json object &#39;{&#39; or json array &#39;...
Definition: json_serializer.cpp:81

◆ appd_iot_json_start_object()

appd_iot_error_code_t appd_iot_json_start_object ( json_t json,
const char *  object_name 
)

starts json object by adding char '{' to json buf. If a name is given it will be "name":{

Parameters
jsonstruct which contains the json buf
object_namehas the string for the key to json object
Returns
appd_iot_error_code_t indicating function execution status
160 {
161  return appd_iot_json_start(json, START_OBJECT_CHAR, object_name);
162 }
static appd_iot_error_code_t appd_iot_json_start(json_t *json, char begin, const char *name)
starts json blob by adding the begin character. Typically it could be json object &#39;{&#39; or json array &#39;...
Definition: json_serializer.cpp:81
#define START_OBJECT_CHAR
Definition: json_serializer.cpp:29

◆ appd_iot_json_end_object()

appd_iot_error_code_t appd_iot_json_end_object ( json_t json)

ends json object by adding char '}' to json buf.

Parameters
jsonstruct which contains the json buf
Returns
appd_iot_error_code_t indicating function execution status
211 {
212  return appd_iot_json_end(json, END_OBJECT_CHAR);
213 }
static appd_iot_error_code_t appd_iot_json_end(json_t *json, char end)
ends json object with end character. Typically it is either &#39;}&#39; or &#39;]&#39;.
Definition: json_serializer.cpp:169
#define END_OBJECT_CHAR
Definition: json_serializer.cpp:31

◆ appd_iot_json_end_array()

appd_iot_error_code_t appd_iot_json_end_array ( json_t json)

ends json object by adding char ']' to json buf.

Parameters
jsonstruct which contains the json buf
Returns
appd_iot_error_code_t indicating function execution status
221 {
222  return appd_iot_json_end(json, END_ARRAY_CHAR);
223 }
static appd_iot_error_code_t appd_iot_json_end(json_t *json, char end)
ends json object with end character. Typically it is either &#39;}&#39; or &#39;]&#39;.
Definition: json_serializer.cpp:169
#define END_ARRAY_CHAR
Definition: json_serializer.cpp:32

◆ appd_iot_add_escape_char()

std::string appd_iot_add_escape_char ( const void *  value)

adds escape character to the string if applicable. Unicode characters are currently not handled.

Parameters
valueis the string which is to be escaped.
Returns
std::string contains modified string with escape characters included
232 {
233  char* temp = (char*)value;
234  std::string s = "";
235 
236 
237  for (size_t i = 0; i < strlen(temp); i++)
238  {
239  //Reading only 0 to 127 ascii values. unicode characters are not handled.
240  unsigned char t = (temp[i] & 0x7F);
241 
242  switch (t)
243  {
244  case '\b':
245  s += "\\b";
246  break;
247 
248  case '\n':
249  s += "\\n";
250  break;
251 
252  case '\r':
253  s += "\\r";
254  break;
255 
256  case '\t':
257  s += "\\t";
258  break;
259 
260  case '\f':
261  s += "\\f";
262  break;
263 
264  case '"':
265  s += "\\\"";
266  break;
267 
268  case '\\':
269  s += "\\\\";
270  break;
271 
272  case '/':
273  s += "\\/";
274  break;
275 
276  default:
277  //if escape sequence not detected, retain the original character
278  s.push_back(temp[i]);
279  break;
280  }
281  }
282 
283  return s;
284 }

◆ appd_iot_convert_to_string()

appd_iot_error_code_t appd_iot_convert_to_string ( char *  buf,
size_t  bufsize,
const void *  value,
appd_iot_data_types_t  type 
)

convert integer, double and boolean to string

Parameters
bufto which the converted string is to be written
bufsizeindicates the size allocated for the string
valueis the data to be converted to string
typeindicates if the value is string, integer, double or boolean data type
Returns
appd_iot_error_code_t indicating function execution status
296 {
297  std::string s;
298 
299  switch (type)
300  {
301  case APPD_IOT_STRING:
302  s = appd_iot_add_escape_char(value);
303  strncpy(buf, s.c_str(), bufsize);
304  buf[bufsize - 1] = '\0';
305  break;
306 
307  case APPD_IOT_INTEGER:
308  snprintf(buf, bufsize, "%" PRId64 "", *(int64_t*)value);
309  buf[bufsize - 1] = '\0';
310  break;
311 
312  case APPD_IOT_DOUBLE:
313  snprintf(buf, bufsize, "%lf", *(double*)value);
314  buf[bufsize - 1] = '\0';
315  break;
316 
317  case APPD_IOT_BOOLEAN:
318  snprintf(buf, bufsize, "%s", *(bool*)value ? "true" : "false");
319  buf[bufsize - 1] = '\0';
320  break;
321 
322  default:
323  appd_iot_log(APPD_IOT_LOG_WARN, "Invalid Data Type");
325  }
326 
327  return APPD_IOT_SUCCESS;
328 }
void appd_iot_log(appd_iot_log_level_t log_level, const char *format,...)
Reads log message, appends log header and triggers log write callback function.
Definition: log.cpp:89
std::string appd_iot_add_escape_char(const void *value)
adds escape character to the string if applicable. Unicode characters are currently not handled...
Definition: json_serializer.cpp:231
Definition: appd_iot_def.h:101
Definition: appd_iot_def.h:192
Definition: appd_iot_def.h:194
Definition: appd_iot_def.h:188
Definition: appd_iot_def.h:190
Definition: appd_iot_def.h:32
Definition: appd_iot_def.h:30

◆ appd_iot_json_add_string_key_value()

appd_iot_error_code_t appd_iot_json_add_string_key_value ( json_t json,
const char *  key,
const char *  strval 
)

adds key:value pair to json object with value as string

Parameters
jsonstruct which contains the json buf
keycontains string representing key
strvalcontains string representing value
Returns
appd_iot_error_code_t indicating function execution status
439 {
440  //do escape, get the size
441  return appd_iot_json_add_key_value(json, key, (void*)strval, APPD_IOT_STRING);
442 }
Definition: appd_iot_def.h:192
static appd_iot_error_code_t appd_iot_json_add_key_value(json_t *json, const char *key, const void *value, appd_iot_data_types_t type)
adds key:value pair to json object
Definition: json_serializer.cpp:339

◆ appd_iot_json_add_integer_key_value()

appd_iot_error_code_t appd_iot_json_add_integer_key_value ( json_t json,
const char *  key,
int64_t  intval 
)

adds key:value pair to json object with value as 64 bit integer

Parameters
jsonstruct which contains the json buf
keycontains string representing key
intvalcontains integer representing value
Returns
appd_iot_error_code_t indicating function execution status
452 {
453  return appd_iot_json_add_key_value(json, key, (void*)(&intval), APPD_IOT_INTEGER);
454 }
Definition: appd_iot_def.h:188
static appd_iot_error_code_t appd_iot_json_add_key_value(json_t *json, const char *key, const void *value, appd_iot_data_types_t type)
adds key:value pair to json object
Definition: json_serializer.cpp:339

◆ appd_iot_json_add_double_key_value()

appd_iot_error_code_t appd_iot_json_add_double_key_value ( json_t json,
const char *  key,
double  doubleval 
)

adds double value to json object

adds key:value pair to json object with value as double

Returns
appd_iot_error_code_t indicating function execution status
461 {
462  return appd_iot_json_add_key_value(json, key, (void*)(&doubleval), APPD_IOT_DOUBLE);
463 }
Definition: appd_iot_def.h:190
static appd_iot_error_code_t appd_iot_json_add_key_value(json_t *json, const char *key, const void *value, appd_iot_data_types_t type)
adds key:value pair to json object
Definition: json_serializer.cpp:339

◆ appd_iot_json_add_boolean_key_value()

appd_iot_error_code_t appd_iot_json_add_boolean_key_value ( json_t json,
const char *  key,
bool  boolval 
)

adds key:value pair to json object with value as boolean

Parameters
jsonstruct which contains the json buf
keycontains string representing key
boolvalcontains boolean representing value
Returns
appd_iot_error_code_t indicating function execution status
473 {
474  return appd_iot_json_add_key_value(json, key, (void*)(&boolval), APPD_IOT_BOOLEAN);
475 }
Definition: appd_iot_def.h:194
static appd_iot_error_code_t appd_iot_json_add_key_value(json_t *json, const char *key, const void *value, appd_iot_data_types_t type)
adds key:value pair to json object
Definition: json_serializer.cpp:339

◆ appd_iot_json_add_string_value()

appd_iot_error_code_t appd_iot_json_add_string_value ( json_t json,
const char *  value 
)

adds value to json object. This function is typically used to add string values to JSON arrays

Parameters
jsonstruct which contains the json buf
valuecontains string to be added
Returns
appd_iot_error_code_t indicating function execution status
581 {
582  return appd_iot_json_add_value(json, (void*)value, APPD_IOT_STRING);
583 }
Definition: appd_iot_def.h:192
static appd_iot_error_code_t appd_iot_json_add_value(json_t *json, const void *value, appd_iot_data_types_t type)
adds value to json object. This function is typically used to add string, integer, double, boolean values to JSON arrays
Definition: json_serializer.cpp:486

◆ appd_iot_json_add_integer_value()

appd_iot_error_code_t appd_iot_json_add_integer_value ( json_t json,
int64_t  intval 
)

adds value to json object. This function is typically used to add integer values to JSON arrays

Parameters
jsonstruct which contains the json buf
intvalcontains integer to be added
Returns
appd_iot_error_code_t indicating function execution status
593 {
594  return appd_iot_json_add_value(json, (void*)(&intval), APPD_IOT_INTEGER);
595 }
Definition: appd_iot_def.h:188
static appd_iot_error_code_t appd_iot_json_add_value(json_t *json, const void *value, appd_iot_data_types_t type)
adds value to json object. This function is typically used to add string, integer, double, boolean values to JSON arrays
Definition: json_serializer.cpp:486

◆ appd_iot_json_add_double_value()

appd_iot_error_code_t appd_iot_json_add_double_value ( json_t json,
double  doubleval 
)

adds value to json object. This function is typically used to add double values to JSON arrays

Parameters
jsonstruct which contains the json buf
doublevalcontains integer to be added
Returns
appd_iot_error_code_t indicating function execution status
604 {
605  return appd_iot_json_add_value(json, (void*)(&doubleval), APPD_IOT_DOUBLE);
606 }
Definition: appd_iot_def.h:190
static appd_iot_error_code_t appd_iot_json_add_value(json_t *json, const void *value, appd_iot_data_types_t type)
adds value to json object. This function is typically used to add string, integer, double, boolean values to JSON arrays
Definition: json_serializer.cpp:486

◆ appd_iot_json_add_boolean_value()

appd_iot_error_code_t appd_iot_json_add_boolean_value ( json_t json,
bool  boolval 
)

adds value to json object. This function is typically used to add boolean values to JSON arrays

Parameters
jsonstruct which contains the json buf
boolvalcontains boolean to be added
Returns
appd_iot_error_code_t indicating function execution status
615 {
616  return appd_iot_json_add_value(json, (void*)(&boolval), APPD_IOT_BOOLEAN);
617 }
Definition: appd_iot_def.h:194
static appd_iot_error_code_t appd_iot_json_add_value(json_t *json, const void *value, appd_iot_data_types_t type)
adds value to json object. This function is typically used to add string, integer, double, boolean values to JSON arrays
Definition: json_serializer.cpp:486

◆ appd_iot_json_get_string()

const char* appd_iot_json_get_string ( json_t json)

returns the json string constructed so far.

Parameters
jsonstruct which contains the json buf
Returns
char pointer containing the json string.
625 {
626  return json->buf;
627 }
char * buf
Definition: json_serializer.hpp:43

◆ appd_iot_json_pretty_print()

const char* appd_iot_json_pretty_print ( json_t json)

format and returns json string with line breaks, indentiation at start/end of objects/arrays

Parameters
jsonstruct which contains the json buf
Returns
char pointer containing the json string.
635 {
636  if (json == NULL)
637  {
638  appd_iot_log(APPD_IOT_LOG_ERROR, "JSON Object is Null");
639  return NULL;
640  }
641 
642  if (json->buf == NULL)
643  {
644  appd_iot_log(APPD_IOT_LOG_ERROR, "JSON String Buffer is Null");
645  return NULL;
646  }
647 
648  if (json->printbuf != NULL)
649  {
650  return json->printbuf;
651  }
652 
653  size_t printbuf_size = 2 * json->len;
654  json->printbuf = (char*)calloc(1, printbuf_size);
655  size_t printbuf_len = 0;
656 
657  int width = 0;
658 
659  snprintf(json->printbuf + printbuf_len, 2, "\n");
660  printbuf_len += 1;
661 
662  for (size_t i = 0; i < json->len; i++)
663  {
664  if (json->buf[i] == START_ARRAY_CHAR)
665  {
666  width += 2; //for indentation
667  }
668 
669  if (json->buf[i] == START_OBJECT_CHAR)
670  {
671  width += 2; //for indentation
672  }
673 
674  if (json->buf[i] == END_ARRAY_CHAR)
675  {
676  width -= 2; //remove indentiation
677  }
678 
679  if (json->buf[i] == END_OBJECT_CHAR)
680  {
681  width -= 2; //remove indentiation
682  }
683 
684  if ((printbuf_len + width + 2) > printbuf_size)
685  {
686  json->printbuf = (char*)realloc(json->printbuf, printbuf_size * 2);
687 
688  if (json->printbuf == NULL)
689  {
690  appd_iot_log(APPD_IOT_LOG_ERROR, "Failed to Realloc Memory");
691  free(json->printbuf);
692  return NULL;
693  }
694 
695  printbuf_size = printbuf_size * 2;
696  }
697 
698  //print newline and add indentation before closing
699  if (json->buf[i] == END_ARRAY_CHAR || json->buf[i] == END_OBJECT_CHAR)
700  {
701  if (width == 0)
702  {
703  width = width + 1; //if no indentation, print new line
704  }
705 
706  snprintf(json->printbuf + printbuf_len, width + 1, "\n%-*s", width, "");
707  printbuf_len += width;
708  }
709 
710  snprintf(json->printbuf + printbuf_len, 2, "%c", json->buf[i]);
711  printbuf_len += 1;
712 
713  //print newline and add indentation after opening
714  if (json->buf[i] == START_ARRAY_CHAR || json->buf[i] == START_OBJECT_CHAR ||
715  json->buf[i] == JSON_DELIMITER)
716  {
717  snprintf(json->printbuf + printbuf_len, width + 1, "\n%-*s", width, "");
718  printbuf_len += width;
719  }
720  }
721 
722  json->printbuf[printbuf_len] = '\0';
723 
724  return json->printbuf;
725 }
char * printbuf
Definition: json_serializer.hpp:44
char * buf
Definition: json_serializer.hpp:43
void appd_iot_log(appd_iot_log_level_t log_level, const char *format,...)
Reads log message, appends log header and triggers log write callback function.
Definition: log.cpp:89
size_t len
Definition: json_serializer.hpp:41
#define JSON_DELIMITER
Definition: json_serializer.cpp:33
#define END_ARRAY_CHAR
Definition: json_serializer.cpp:32
#define START_ARRAY_CHAR
Definition: json_serializer.cpp:30
#define END_OBJECT_CHAR
Definition: json_serializer.cpp:31
#define START_OBJECT_CHAR
Definition: json_serializer.cpp:29
Definition: appd_iot_def.h:99

◆ appd_iot_json_free()

void appd_iot_json_free ( json_t json)

frees json structure

Parameters
jsonstruct which contains the json buf
732 {
733  if (json == NULL)
734  {
735  return;
736  }
737 
738  if (json->buf != NULL)
739  {
740  free(json->buf);
741  }
742 
743  if (json->printbuf != NULL)
744  {
745  free(json->printbuf);
746  }
747 
748  free(json);
749 
750  return;
751 }
char * printbuf
Definition: json_serializer.hpp:44
char * buf
Definition: json_serializer.hpp:43

Variable Documentation

◆ comma

const char* comma = ","
static