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.hpp File Reference

Go to the source code of this file.

Data Structures

struct  json_t
 JSON struct which holds json string in a buffer. More...
 

Enumerations

enum  json_ops_t {
  DEINIT, INIT, START_ARRAY, START_OBJECT,
  ADD_DATA, END_OBJECT, END_ARRAY
}
 Enums to keep track of last JSON Operation completed. More...
 

Functions

json_tappd_iot_json_init (void)
 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...
 
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 key:value pair to json object with value as double 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...
 

Enumeration Type Documentation

◆ json_ops_t

enum json_ops_t

Enums to keep track of last JSON Operation completed.

Enumerator
DEINIT 
INIT 
START_ARRAY 
START_OBJECT 
ADD_DATA 
END_OBJECT 
END_ARRAY 
26 {
27  DEINIT,
28  INIT,
31  ADD_DATA,
32  END_OBJECT,
33  END_ARRAY
34 } json_ops_t;
Definition: json_serializer.hpp:30
Definition: json_serializer.hpp:28
Definition: json_serializer.hpp:33
Definition: json_serializer.hpp:29
Definition: json_serializer.hpp:31
Definition: json_serializer.hpp:27
Definition: json_serializer.hpp:32
json_ops_t
Enums to keep track of last JSON Operation completed.
Definition: json_serializer.hpp:25

Function Documentation

◆ 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 '{' or json array '...
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 '{' or json array '...
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 '}' or ']'.
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 '}' or ']'.
Definition: json_serializer.cpp:169
#define END_ARRAY_CHAR
Definition: json_serializer.cpp:32

◆ 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 key:value pair to json object with value as double

Parameters
jsonstruct which contains the json buf
keycontains string representing key
doublevalcontains double representing value
Returns
appd_iot_error_code_t indicating function execution status

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