add new functions for json strings
authorJeonghoon Park <jh1979.park@samsung.com>
Thu, 2 Nov 2017 03:53:27 +0000 (12:53 +0900)
committerJeonghoon Park <jh1979.park@samsung.com>
Thu, 2 Nov 2017 03:53:27 +0000 (12:53 +0900)
Change-Id: I1004ab14a7b8339664ff40d7393f75c0069a6e29

inc/webutil.h
src/webutil.c

index d8c3ae2..2d3e437 100644 (file)
@@ -70,8 +70,14 @@ int web_util_noti_post(const char *resource, const char *json_data);
 
 int web_util_json_init(void);
 int web_util_json_fini(void);
+int web_util_json_begin(void);
+int web_util_json_end(void);
 int web_util_json_data_array_begin(void);
 int web_util_json_data_array_end(void);
+int web_util_json_add_int(const char* key, long long int value);
+int web_util_json_add_double(const char* key, double value);
+int web_util_json_add_boolean(const char* key, bool value);
+int web_util_json_add_string(const char* key, const char *value);
 int web_util_json_add_sensor_data(const char* sensorpi_id, web_util_sensor_data_s *sensor_data);
 char *web_util_get_json_string(void);
 
index d1fed79..ea5ae9b 100644 (file)
@@ -141,7 +141,7 @@ int web_util_json_fini(void)
        return 0;
 }
 
-int web_util_json_data_array_begin(void)
+int web_util_json_begin(void)
 {
        retv_if(Json_h.builder == NULL, -1);
        retv_if(Json_h.is_begin == true, -1);
@@ -149,13 +149,131 @@ int web_util_json_data_array_begin(void)
 
        Json_h.is_begin = true;
 
-       /*
-       {
-               SensorsDataList : [ SensorData ]
+       json_builder_begin_object(Json_h.builder);
+
+       return 0;
+}
+
+int web_util_json_end(void)
+{
+       retv_if(Json_h.builder == NULL, -1);
+       retv_if(Json_h.is_begin == false, -1);
+       retv_if(Json_h.is_end == true, -1);
+
+       json_builder_end_object(Json_h.builder);
+       Json_h.is_end = true;
+
+       return 0;
+}
+
+int web_util_json_add_int(const char* key, long long int value)
+{
+       retv_if(!key, -1);
+
+       if (Json_h.builder == NULL) {
+               _E("Handle for json is not initialized, call web_util_json_init() first");
+               return -1;
        }
-       */
 
-       json_builder_begin_object(Json_h.builder);
+       if (Json_h.is_begin == false) {
+               _E("json object has not begun, call web_util_json_begin() first");
+               return -1;
+       }
+
+       if (Json_h.is_end == true) {
+               _E("json object has already ended, call web_util_json_begin() first");
+               return -1;
+       }
+
+       json_builder_set_member_name(Json_h.builder, key);
+       json_builder_add_int_value(Json_h.builder, value);
+
+       return 0;
+}
+
+int web_util_json_add_double(const char* key, double value)
+{
+       retv_if(!key, -1);
+
+       if (Json_h.builder == NULL) {
+               _E("Handle for json is not initialized, call web_util_json_init() first");
+               return -1;
+       }
+
+       if (Json_h.is_begin == false) {
+               _E("json object has not begun, call web_util_json_begin() first");
+               return -1;
+       }
+
+       if (Json_h.is_end == true) {
+               _E("json object has already ended, call web_util_json_begin() first");
+               return -1;
+       }
+
+       json_builder_set_member_name(Json_h.builder, key);
+       json_builder_add_double_value(Json_h.builder, value);
+
+       return 0;
+}
+
+int web_util_json_add_boolean(const char* key, bool value)
+{
+       retv_if(!key, -1);
+
+       if (Json_h.builder == NULL) {
+               _E("Handle for json is not initialized, call web_util_json_init() first");
+               return -1;
+       }
+
+       if (Json_h.is_begin == false) {
+               _E("json object has not begun, call web_util_json_begin() first");
+               return -1;
+       }
+
+       if (Json_h.is_end == true) {
+               _E("json object has already ended, call web_util_json_begin() first");
+               return -1;
+       }
+
+       json_builder_set_member_name(Json_h.builder, key);
+       json_builder_add_boolean_value(Json_h.builder, value);
+
+       return 0;
+}
+
+int web_util_json_add_string(const char* key, const char *value)
+{
+       retv_if(!key, -1);
+
+       if (Json_h.builder == NULL) {
+               _E("Handle for json is not initialized, call web_util_json_init() first");
+               return -1;
+       }
+
+       if (Json_h.is_begin == false) {
+               _E("json object has not begun, call web_util_json_begin() first");
+               return -1;
+       }
+
+       if (Json_h.is_end == true) {
+               _E("json object has already ended, call web_util_json_begin() first");
+               return -1;
+       }
+
+       json_builder_set_member_name(Json_h.builder, key);
+       json_builder_add_string_value(Json_h.builder, value);
+
+       return 0;
+}
+
+int web_util_json_data_array_begin(void)
+{
+       int ret = 0;
+       retv_if(Json_h.builder == NULL, -1);
+
+       ret = web_util_json_begin();
+       retv_if(ret, -1);
+
        json_builder_set_member_name(Json_h.builder, "SensorDataList");
        json_builder_begin_array(Json_h.builder);
 
@@ -169,8 +287,7 @@ int web_util_json_data_array_end(void)
        retv_if(Json_h.is_end == true, -1);
 
        json_builder_end_array(Json_h.builder);
-       json_builder_end_object(Json_h.builder);
-       Json_h.is_end = true;
+       web_util_json_end();
 
        return 0;
 }