Microsoft Azure IoTHub client libraries

Dependents:   sht15_remote_monitoring RobotArmDemo iothub_client_sample_amqp f767zi_mqtt ... more

This library implements the Microsoft Azure IoTHub client library. The code is replicated from https://github.com/Azure/azure-iot-sdks

Revision:
50:bbc71457b0ea
Parent:
42:448eecc3676e
Child:
56:fdda9c1244e4
--- a/parson.c	Fri Aug 26 12:58:54 2016 -0700
+++ b/parson.c	Fri Sep 09 13:37:43 2016 -0700
@@ -109,7 +109,7 @@
 static JSON_Value * json_value_init_string_no_copy(char *string);
 
 /* Parser */
-static void         skip_quotes(const char **string);
+static JSON_Status  skip_quotes(const char **string);
 static int          parse_utf_16(const char **unprocessed, char **processed);
 static char *       process_string(const char *input, size_t len);
 static char *       get_quoted_string(const char **string);
@@ -130,8 +130,9 @@
 /* Various */
 static char * parson_strndup(const char *string, size_t n) {
     char *output_string = (char*)parson_malloc(n + 1);
-    if (!output_string)
+    if (!output_string) {
         return NULL;
+    }
     output_string[n] = '\0';
     strncpy(output_string, string, n);
     return output_string;
@@ -215,13 +216,16 @@
 }
 
 static int is_decimal(const char *string, size_t length) {
-    if (length > 1 && string[0] == '0' && string[1] != '.')
+    if (length > 1 && string[0] == '0' && string[1] != '.') {
         return 0;
-    if (length > 2 && !strncmp(string, "-0", 2) && string[2] != '.')
+    }
+    if (length > 2 && !strncmp(string, "-0", 2) && string[2] != '.') {
         return 0;
+    }
     while (length--)
-        if (strchr("xX", string[length]))
+        if (strchr("xX", string[length])) {
             return 0;
+        }
     return 1;
 }
 
@@ -230,8 +234,9 @@
     size_t file_size;
     long pos;
     char *file_contents;
-    if (!fp)
+    if (!fp) {
         return NULL;
+    }
     fseek(fp, 0L, SEEK_END);
     pos = ftell(fp);
     if (pos < 0) {
@@ -263,8 +268,9 @@
     char *ptr = NULL, current_char;
     size_t start_token_len = strlen(start_token);
     size_t end_token_len = strlen(end_token);
-    if (start_token_len == 0 || end_token_len == 0)
-    	return;
+    if (start_token_len == 0 || end_token_len == 0) {
+        return;
+    }
     while ((current_char = *string) != '\0') {
         if (current_char == '\\' && !escaped) {
             escaped = 1;
@@ -273,15 +279,18 @@
         } else if (current_char == '\"' && !escaped) {
             in_string = !in_string;
         } else if (!in_string && strncmp(string, start_token, start_token_len) == 0) {
-			for(i = 0; i < start_token_len; i++)
+            for(i = 0; i < start_token_len; i++) {
                 string[i] = ' ';
-        	string = string + start_token_len;
+            }
+            string = string + start_token_len;
             ptr = strstr(string, end_token);
-            if (!ptr)
+            if (!ptr) {
                 return;
-            for (i = 0; i < (ptr - string) + end_token_len; i++)
+            }
+            for (i = 0; i < (ptr - string) + end_token_len; i++) {
                 string[i] = ' ';
-          	string = ptr + end_token_len - 1;
+            }
+            string = ptr + end_token_len - 1;
         }
         escaped = 0;
         string++;
@@ -291,8 +300,9 @@
 /* JSON Object */
 static JSON_Object * json_object_init(void) {
     JSON_Object *new_obj = (JSON_Object*)parson_malloc(sizeof(JSON_Object));
-    if (!new_obj)
+    if (!new_obj) {
         return NULL;
+    }
     new_obj->names = (char**)NULL;
     new_obj->values = (JSON_Value**)NULL;
     new_obj->capacity = 0;
@@ -310,15 +320,18 @@
     }
     if (object->count >= object->capacity) {
         size_t new_capacity = MAX(object->capacity * 2, STARTING_CAPACITY);
-        if (new_capacity > OBJECT_MAX_CAPACITY)
+        if (new_capacity > OBJECT_MAX_CAPACITY) {
             return JSONFailure;
-        if (json_object_resize(object, new_capacity) == JSONFailure)
+        }
+        if (json_object_resize(object, new_capacity) == JSONFailure) {
             return JSONFailure;
+        }
     }
     index = object->count;
     object->names[index] = parson_strdup(name);
-    if (object->names[index] == NULL)
+    if (object->names[index] == NULL) {
         return JSONFailure;
+    }
     object->values[index] = value;
     object->count++;
     return JSONSuccess;
@@ -335,8 +348,9 @@
     }
 
     temp_names = (char**)parson_malloc(new_capacity * sizeof(char*));
-    if (temp_names == NULL)
+    if (temp_names == NULL) {
         return JSONFailure;
+    }
 
     temp_values = (JSON_Value**)parson_malloc(new_capacity * sizeof(JSON_Value*));
     if (temp_values == NULL) {
@@ -360,10 +374,12 @@
     size_t i, name_length;
     for (i = 0; i < json_object_get_count(object); i++) {
         name_length = strlen(object->names[i]);
-        if (name_length != n)
+        if (name_length != n) {
             continue;
-        if (strncmp(object->names[i], name, n) == 0)
+        }
+        if (strncmp(object->names[i], name, n) == 0) {
             return object->values[i];
+        }
     }
     return NULL;
 }
@@ -381,8 +397,9 @@
 /* JSON Array */
 static JSON_Array * json_array_init(void) {
     JSON_Array *new_array = (JSON_Array*)parson_malloc(sizeof(JSON_Array));
-    if (!new_array)
+    if (!new_array) {
         return NULL;
+    }
     new_array->items = (JSON_Value**)NULL;
     new_array->capacity = 0;
     new_array->count = 0;
@@ -392,10 +409,12 @@
 static JSON_Status json_array_add(JSON_Array *array, JSON_Value *value) {
     if (array->count >= array->capacity) {
         size_t new_capacity = MAX(array->capacity * 2, STARTING_CAPACITY);
-        if (new_capacity > ARRAY_MAX_CAPACITY)
+        if (new_capacity > ARRAY_MAX_CAPACITY) {
             return JSONFailure;
-        if (json_array_resize(array, new_capacity) == JSONFailure)
+        }
+        if (json_array_resize(array, new_capacity) == JSONFailure) {
             return JSONFailure;
+        }
     }
     array->items[array->count] = value;
     array->count++;
@@ -430,27 +449,33 @@
 /* JSON Value */
 static JSON_Value * json_value_init_string_no_copy(char *string) {
     JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
-    if (!new_value)
+    if (!new_value) {
         return NULL;
+    }
     new_value->type = JSONString;
     new_value->value.string = string;
     return new_value;
 }
 
 /* Parser */
-static void skip_quotes(const char **string) {
+static JSON_Status skip_quotes(const char **string) {
+    if (**string != '\"') {
+        return JSONFailure;
+    }
     SKIP_CHAR(string);
     while (**string != '\"') {
-        if (**string == '\0')
-            return;
-        if (**string == '\\') {
+        if (**string == '\0') {
+            return JSONFailure;
+        } else if (**string == '\\') {
             SKIP_CHAR(string);
-            if (**string == '\0')
-                return;
+            if (**string == '\0') {
+                return JSONFailure;
+            }
         }
         SKIP_CHAR(string);
     }
     SKIP_CHAR(string);
+    return JSONSuccess;
 }
 
 static int parse_utf_16(const char **unprocessed, char **processed) {
@@ -458,8 +483,9 @@
     char *processed_ptr = *processed;
     const char *unprocessed_ptr = *unprocessed;
     unprocessed_ptr++; /* skips u */
-    if (!is_utf16_hex((const unsigned char*)unprocessed_ptr) || sscanf(unprocessed_ptr, "%4x", &cp) == EOF)
-            return JSONFailure;
+    if (!is_utf16_hex((const unsigned char*)unprocessed_ptr) || sscanf(unprocessed_ptr, "%4x", &cp) == EOF) {
+        return JSONFailure;
+    }
     if (cp < 0x80) {
         *processed_ptr = cp; /* 0xxxxxxx */
     } else if (cp < 0x800) {
@@ -473,7 +499,7 @@
         lead = cp;
         unprocessed_ptr += 4; /* should always be within the buffer, otherwise previous sscanf would fail */
         if (*unprocessed_ptr++ != '\\' || *unprocessed_ptr++ != 'u' || /* starts with \u? */
-            !is_utf16_hex((const unsigned char*)unprocessed_ptr)          ||
+            !is_utf16_hex((const unsigned char*)unprocessed_ptr)    ||
             sscanf(unprocessed_ptr, "%4x", &trail) == EOF           ||
             trail < 0xDC00 || trail > 0xDFFF) { /* valid trail surrogate? (0xDC00..0xDFFF) */
                 return JSONFailure;
@@ -515,8 +541,9 @@
                 case 'r':  *output_ptr = '\r'; break;
                 case 't':  *output_ptr = '\t'; break;
                 case 'u':
-                    if (parse_utf_16(&input_ptr, &output_ptr) == JSONFailure)
+                    if (parse_utf_16(&input_ptr, &output_ptr) == JSONFailure) {
                         goto error;
+                    }
                     break;
                 default:
                     goto error;
@@ -532,9 +559,11 @@
     *output_ptr = '\0';
     /* resize to new length */
     final_size = (size_t)(output_ptr-output) + 1;
+    /* todo: don't resize if final_size == initial_size */
     resized_output = (char*)parson_malloc(final_size);
-    if (resized_output == NULL)
+    if (resized_output == NULL) {
         goto error;
+    }
     memcpy(resized_output, output, final_size);
     parson_free(output);
     return resized_output;
@@ -548,14 +577,18 @@
 static char * get_quoted_string(const char **string) {
     const char *string_start = *string;
     size_t string_len = 0;
-    skip_quotes(string);
+    JSON_Status status = skip_quotes(string);
+    if (status != JSONSuccess) {
+        return NULL;
+    }
     string_len = *string - string_start - 2; /* length without quotes */
     return process_string(string_start + 1, string_len);
 }
 
 static JSON_Value * parse_value(const char **string, size_t nesting) {
-    if (nesting > MAX_NESTING)
+    if (nesting > MAX_NESTING) {
         return NULL;
+    }
     SKIP_WHITESPACES(string);
     switch (**string) {
         case '{':
@@ -581,8 +614,9 @@
     JSON_Value *output_value = json_value_init_object(), *new_value = NULL;
     JSON_Object *output_object = json_value_get_object(output_value);
     char *new_key = NULL;
-    if (output_value == NULL)
+    if (output_value == NULL || **string != '{') {
         return NULL;
+    }
     SKIP_CHAR(string);
     SKIP_WHITESPACES(string);
     if (**string == '}') { /* empty object */
@@ -611,8 +645,9 @@
         }
         parson_free(new_key);
         SKIP_WHITESPACES(string);
-        if (**string != ',')
+        if (**string != ',') {
             break;
+        }
         SKIP_CHAR(string);
         SKIP_WHITESPACES(string);
     }
@@ -629,8 +664,9 @@
 static JSON_Value * parse_array_value(const char **string, size_t nesting) {
     JSON_Value *output_value = json_value_init_array(), *new_array_value = NULL;
     JSON_Array *output_array = json_value_get_array(output_value);
-    if (!output_value)
+    if (!output_value || **string != '[') {
         return NULL;
+    }
     SKIP_CHAR(string);
     SKIP_WHITESPACES(string);
     if (**string == ']') { /* empty array */
@@ -643,14 +679,15 @@
             json_value_free(output_value);
             return NULL;
         }
-        if(json_array_add(output_array, new_array_value) == JSONFailure) {
+        if (json_array_add(output_array, new_array_value) == JSONFailure) {
             parson_free(new_array_value);
             json_value_free(output_value);
             return NULL;
         }
         SKIP_WHITESPACES(string);
-        if (**string != ',')
+        if (**string != ',') {
             break;
+        }
         SKIP_CHAR(string);
         SKIP_WHITESPACES(string);
     }
@@ -667,8 +704,9 @@
 static JSON_Value * parse_string_value(const char **string) {
     JSON_Value *value = NULL;
     char *new_string = get_quoted_string(string);
-    if (new_string == NULL)
+    if (new_string == NULL) {
         return NULL;
+    }
     value = json_value_init_string_no_copy(new_string);
     if (value == NULL) {
         parson_free(new_string);
@@ -738,89 +776,113 @@
             array = json_value_get_array(value);
             count = json_array_get_count(array);
             APPEND_STRING("[");
-            if (count > 0 && is_pretty)
+            if (count > 0 && is_pretty) {
                 APPEND_STRING("\n");
+            }
             for (i = 0; i < count; i++) {
-                if (is_pretty)
+                if (is_pretty) {
                     APPEND_INDENT(level+1);
+                }
                 temp_value = json_array_get_value(array, i);
                 written = json_serialize_to_buffer_r(temp_value, buf, level+1, is_pretty, num_buf);
-                if (written < 0)
+                if (written < 0) {
                     return -1;
-                if (buf != NULL)
+                }
+                if (buf != NULL) {
                     buf += written;
+                }
                 written_total += written;
-                if (i < (count - 1))
+                if (i < (count - 1)) {
                     APPEND_STRING(",");
-                if (is_pretty)
+                }
+                if (is_pretty) {
                     APPEND_STRING("\n");
+                }
             }
-            if (count > 0 && is_pretty)
+            if (count > 0 && is_pretty) {
                 APPEND_INDENT(level);
+            }
             APPEND_STRING("]");
             return written_total;
         case JSONObject:
             object = json_value_get_object(value);
             count  = json_object_get_count(object);
             APPEND_STRING("{");
-            if (count > 0 && is_pretty)
+            if (count > 0 && is_pretty) {
                 APPEND_STRING("\n");
+            }
             for (i = 0; i < count; i++) {
                 key = json_object_get_name(object, i);
-                if (is_pretty)
+                if (is_pretty) {
                     APPEND_INDENT(level+1);
+                }
                 written = json_serialize_string(key, buf);
-                if (written < 0)
+                if (written < 0) {
                     return -1;
-                if (buf != NULL)
+                }
+                if (buf != NULL) {
                     buf += written;
+                }
                 written_total += written;
                 APPEND_STRING(":");
-                if (is_pretty)
+                if (is_pretty) {
                     APPEND_STRING(" ");
+                }
                 temp_value = json_object_get_value(object, key);
                 written = json_serialize_to_buffer_r(temp_value, buf, level+1, is_pretty, num_buf);
-                if (written < 0)
+                if (written < 0) {
                     return -1;
-                if (buf != NULL)
+                }
+                if (buf != NULL) {
                     buf += written;
+                }
                 written_total += written;
-                if (i < (count - 1))
+                if (i < (count - 1)) {
                     APPEND_STRING(",");
-                if (is_pretty)
+                }
+                if (is_pretty) {
                     APPEND_STRING("\n");
+                }
             }
-            if (count > 0 && is_pretty)
+            if (count > 0 && is_pretty) {
                 APPEND_INDENT(level);
+            }
             APPEND_STRING("}");
             return written_total;
         case JSONString:
             string = json_value_get_string(value);
             written = json_serialize_string(string, buf);
-            if (written < 0)
+            if (written < 0) {
                 return -1;
-            if (buf != NULL)
+            }
+            if (buf != NULL) {
                 buf += written;
+            }
             written_total += written;
             return written_total;
         case JSONBoolean:
-            if (json_value_get_boolean(value))
+            if (json_value_get_boolean(value)) {
                 APPEND_STRING("true");
-            else
+            } else {
                 APPEND_STRING("false");
+            }
             return written_total;
         case JSONNumber:
             num = json_value_get_number(value);
-            if (buf != NULL)
+            if (buf != NULL) {
                 num_buf = buf;
-            if (num == ((double)(int)num)) /*  check if num is integer */
+            }
+            if (num == ((double)(int)num)) { /*  check if num is integer */
                 written = sprintf(num_buf, "%d", (int)num);
-            else
+            } else {
                 written = sprintf(num_buf, DOUBLE_SERIALIZATION_FORMAT, num);
-            if (written < 0)
+            }
+            if (written < 0) {
                 return -1;
-            if (buf != NULL)
+            }
+            if (buf != NULL) {
                 buf += written;
+            }
             written_total += written;
             return written_total;
         case JSONNull:
@@ -885,8 +947,9 @@
 JSON_Value * json_parse_file(const char *filename) {
     char *file_contents = read_file(filename);
     JSON_Value *output_value = NULL;
-    if (file_contents == NULL)
+    if (file_contents == NULL) {
         return NULL;
+    }
     output_value = json_parse_string(file_contents);
     parson_free(file_contents);
     return output_value;
@@ -895,16 +958,18 @@
 JSON_Value * json_parse_file_with_comments(const char *filename) {
     char *file_contents = read_file(filename);
     JSON_Value *output_value = NULL;
-    if (file_contents == NULL)
+    if (file_contents == NULL) {
         return NULL;
+    }
     output_value = json_parse_string_with_comments(file_contents);
     parson_free(file_contents);
     return output_value;
 }
 
 JSON_Value * json_parse_string(const char *string) {
-    if (string == NULL)
+    if (string == NULL) {
         return NULL;
+    }
     return parse_value((const char**)&string, 0);
 }
 
@@ -912,8 +977,9 @@
     JSON_Value *result = NULL;
     char *string_mutable_copy = NULL, *string_mutable_copy_ptr = NULL;
     string_mutable_copy = parson_strdup(string);
-    if (string_mutable_copy == NULL)
+    if (string_mutable_copy == NULL) {
         return NULL;
+    }
     remove_comments(string_mutable_copy, "/*", "*/");
     remove_comments(string_mutable_copy, "//", "\n");
     string_mutable_copy_ptr = string_mutable_copy;
@@ -925,8 +991,9 @@
 /* JSON Object API */
 
 JSON_Value * json_object_get_value(const JSON_Object *object, const char *name) {
-    if (object == NULL || name == NULL)
+    if (object == NULL || name == NULL) {
         return NULL;
+    }
     return json_object_nget_value(object, name, strlen(name));
 }
 
@@ -952,8 +1019,9 @@
 
 JSON_Value * json_object_dotget_value(const JSON_Object *object, const char *name) {
     const char *dot_position = strchr(name, '.');
-    if (!dot_position)
+    if (!dot_position) {
         return json_object_get_value(object, name);
+    }
     object = json_value_get_object(json_object_nget_value(object, name, dot_position - name));
     return json_object_dotget_value(object, dot_position + 1);
 }
@@ -983,21 +1051,42 @@
 }
 
 const char * json_object_get_name(const JSON_Object *object, size_t index) {
-    if (object == NULL || index >= json_object_get_count(object))
+    if (object == NULL || index >= json_object_get_count(object)) {
         return NULL;
+    }
     return object->names[index];
 }
 
 JSON_Value * json_object_get_value_at(const JSON_Object *object, size_t index) {
-    if (object == NULL || index >= json_object_get_count(object))
+    if (object == NULL || index >= json_object_get_count(object)) {
         return NULL;
+    }
     return object->values[index];
 }
 
+int json_object_has_value (const JSON_Object *object, const char *name) {
+    return json_object_get_value(object, name) != NULL;
+}
+
+int json_object_has_value_of_type(const JSON_Object *object, const char *name, JSON_Value_Type type) {
+    JSON_Value *val = json_object_get_value(object, name);
+    return val != NULL && json_value_get_type(val) == type;
+}
+
+int json_object_dothas_value (const JSON_Object *object, const char *name) {
+    return json_object_dotget_value(object, name) != NULL;
+}
+
+int json_object_dothas_value_of_type(const JSON_Object *object, const char *name, JSON_Value_Type type) {
+    JSON_Value *val = json_object_dotget_value(object, name);
+    return val != NULL && json_value_get_type(val) == type;
+}
+
 /* JSON Array API */
 JSON_Value * json_array_get_value(const JSON_Array *array, size_t index) {
-    if (array == NULL || index >= json_array_get_count(array))
+    if (array == NULL || index >= json_array_get_count(array)) {
         return NULL;
+    }
     return array->items[index];
 }
 
@@ -1056,7 +1145,9 @@
             json_object_free(value->value.object);
             break;
         case JSONString:
-            if (value->value.string) { parson_free(value->value.string); }
+            if (value->value.string) {
+                parson_free(value->value.string);
+            }
             break;
         case JSONArray:
             json_array_free(value->value.array);
@@ -1069,8 +1160,9 @@
 
 JSON_Value * json_value_init_object(void) {
     JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
-    if (!new_value)
+    if (!new_value) {
         return NULL;
+    }
     new_value->type = JSONObject;
     new_value->value.object = json_object_init();
     if (!new_value->value.object) {
@@ -1082,8 +1174,9 @@
 
 JSON_Value * json_value_init_array(void) {
     JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
-    if (!new_value)
+    if (!new_value) {
         return NULL;
+    }
     new_value->type = JSONArray;
     new_value->value.array = json_array_init();
     if (!new_value->value.array) {
@@ -1097,24 +1190,29 @@
     char *copy = NULL;
     JSON_Value *value;
     size_t string_len = 0;
-    if (string == NULL)
+    if (string == NULL) {
         return NULL;
+    }
     string_len = strlen(string);
-    if (!is_valid_utf8(string, string_len))
+    if (!is_valid_utf8(string, string_len)) {
         return NULL;
+    }
     copy = parson_strndup(string, string_len);
-    if (copy == NULL)
+    if (copy == NULL) {
         return NULL;
+    }
     value = json_value_init_string_no_copy(copy);
-    if (value == NULL)
+    if (value == NULL) {
         parson_free(copy);
+    }
     return value;
 }
 
 JSON_Value * json_value_init_number(double number) {
     JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
-    if (!new_value)
+    if (!new_value) {
         return NULL;
+    }
     new_value->type = JSONNumber;
     new_value->value.number = number;
     return new_value;
@@ -1122,8 +1220,9 @@
 
 JSON_Value * json_value_init_boolean(int boolean) {
     JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
-    if (!new_value)
+    if (!new_value) {
         return NULL;
+    }
     new_value->type = JSONBoolean;
     new_value->value.boolean = boolean ? 1 : 0;
     return new_value;
@@ -1131,8 +1230,9 @@
 
 JSON_Value * json_value_init_null(void) {
     JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
-    if (!new_value)
+    if (!new_value) {
         return NULL;
+    }
     new_value->type = JSONNull;
     return new_value;
 }
@@ -1149,8 +1249,9 @@
         case JSONArray:
             temp_array = json_value_get_array(value);
             return_value = json_value_init_array();
-            if (return_value == NULL)
+            if (return_value == NULL) {
                 return NULL;
+            }
             temp_array_copy = json_value_get_array(return_value);
             for (i = 0; i < json_array_get_count(temp_array); i++) {
                 temp_value = json_array_get_value(temp_array, i);
@@ -1169,8 +1270,9 @@
         case JSONObject:
             temp_object = json_value_get_object(value);
             return_value = json_value_init_object();
-            if (return_value == NULL)
+            if (return_value == NULL) {
                 return NULL;
+            }
             temp_object_copy = json_value_get_object(return_value);
             for (i = 0; i < json_object_get_count(temp_object); i++) {
                 temp_key = json_object_get_name(temp_object, i);
@@ -1194,11 +1296,13 @@
         case JSONString:
             temp_string = json_value_get_string(value);
             temp_string_copy = parson_strdup(temp_string);
-            if (temp_string_copy == NULL)
+            if (temp_string_copy == NULL) {
                 return NULL;
+            }
             return_value = json_value_init_string_no_copy(temp_string_copy);
-            if (return_value == NULL)
+            if (return_value == NULL) {
                 parson_free(temp_string_copy);
+            }
             return return_value;
         case JSONNull:
             return json_value_init_null();
@@ -1222,8 +1326,9 @@
         return JSONFailure;
     }
     written = json_serialize_to_buffer_r(value, buf, 0, 0, NULL);
-    if (written < 0)
+    if (written < 0) {
         return JSONFailure;
+    }
     return JSONSuccess;
 }
 
@@ -1257,8 +1362,9 @@
         return NULL;
     }
     buf = (char*)parson_malloc(buf_size_bytes);
-    if (buf == NULL)
+    if (buf == NULL) {
         return NULL;
+    }
     serialization_result = json_serialize_to_buffer(value, buf, buf_size_bytes);
     if (serialization_result == JSONFailure) {
         json_free_serialized_string(buf);
@@ -1276,11 +1382,13 @@
 JSON_Status json_serialize_to_buffer_pretty(const JSON_Value *value, char *buf, size_t buf_size_in_bytes) {
     int written = -1;
     size_t needed_size_in_bytes = json_serialization_size_pretty(value);
-    if (needed_size_in_bytes == 0 || buf_size_in_bytes < needed_size_in_bytes)
+    if (needed_size_in_bytes == 0 || buf_size_in_bytes < needed_size_in_bytes) {
         return JSONFailure;
+    }
     written = json_serialize_to_buffer_r(value, buf, 0, 1, NULL);
-    if (written < 0)
+    if (written < 0) {
         return JSONFailure;
+    }
     return JSONSuccess;
 }
 
@@ -1314,8 +1422,9 @@
         return NULL;
     }
     buf = (char*)parson_malloc(buf_size_bytes);
-    if (buf == NULL)
+    if (buf == NULL) {
         return NULL;
+    }
     serialization_result = json_serialize_to_buffer_pretty(value, buf, buf_size_bytes);
     if (serialization_result == JSONFailure) {
         json_free_serialized_string(buf);
@@ -1358,8 +1467,9 @@
 
 JSON_Status json_array_replace_string(JSON_Array *array, size_t i, const char* string) {
     JSON_Value *value = json_value_init_string(string);
-    if (value == NULL)
+    if (value == NULL) {
         return JSONFailure;
+    }
     if (json_array_replace_value(array, i, value) == JSONFailure) {
         json_value_free(value);
         return JSONFailure;
@@ -1369,8 +1479,9 @@
 
 JSON_Status json_array_replace_number(JSON_Array *array, size_t i, double number) {
     JSON_Value *value = json_value_init_number(number);
-    if (value == NULL)
+    if (value == NULL) {
         return JSONFailure;
+    }
     if (json_array_replace_value(array, i, value) == JSONFailure) {
         json_value_free(value);
         return JSONFailure;
@@ -1380,8 +1491,9 @@
 
 JSON_Status json_array_replace_boolean(JSON_Array *array, size_t i, int boolean) {
     JSON_Value *value = json_value_init_boolean(boolean);
-    if (value == NULL)
+    if (value == NULL) {
         return JSONFailure;
+    }
     if (json_array_replace_value(array, i, value) == JSONFailure) {
         json_value_free(value);
         return JSONFailure;
@@ -1391,8 +1503,9 @@
 
 JSON_Status json_array_replace_null(JSON_Array *array, size_t i) {
     JSON_Value *value = json_value_init_null();
-    if (value == NULL)
+    if (value == NULL) {
         return JSONFailure;
+    }
     if (json_array_replace_value(array, i, value) == JSONFailure) {
         json_value_free(value);
         return JSONFailure;
@@ -1402,8 +1515,9 @@
 
 JSON_Status json_array_clear(JSON_Array *array) {
     size_t i = 0;
-    if (array == NULL)
+    if (array == NULL) {
         return JSONFailure;
+    }
     for (i = 0; i < json_array_get_count(array); i++) {
         json_value_free(json_array_get_value(array, i));
     }
@@ -1412,15 +1526,17 @@
 }
 
 JSON_Status json_array_append_value(JSON_Array *array, JSON_Value *value) {
-    if (array == NULL || value == NULL)
+    if (array == NULL || value == NULL) {
         return JSONFailure;
+    }
     return json_array_add(array, value);
 }
 
 JSON_Status json_array_append_string(JSON_Array *array, const char *string) {
     JSON_Value *value = json_value_init_string(string);
-    if (value == NULL)
+    if (value == NULL) {
         return JSONFailure;
+    }
     if (json_array_append_value(array, value) == JSONFailure) {
         json_value_free(value);
         return JSONFailure;
@@ -1430,8 +1546,9 @@
 
 JSON_Status json_array_append_number(JSON_Array *array, double number) {
     JSON_Value *value = json_value_init_number(number);
-    if (value == NULL)
+    if (value == NULL) {
         return JSONFailure;
+    }
     if (json_array_append_value(array, value) == JSONFailure) {
         json_value_free(value);
         return JSONFailure;
@@ -1441,8 +1558,9 @@
 
 JSON_Status json_array_append_boolean(JSON_Array *array, int boolean) {
     JSON_Value *value = json_value_init_boolean(boolean);
-    if (value == NULL)
+    if (value == NULL) {
         return JSONFailure;
+    }
     if (json_array_append_value(array, value) == JSONFailure) {
         json_value_free(value);
         return JSONFailure;
@@ -1452,8 +1570,9 @@
 
 JSON_Status json_array_append_null(JSON_Array *array) {
     JSON_Value *value = json_value_init_null();
-    if (value == NULL)
+    if (value == NULL) {
         return JSONFailure;
+    }
     if (json_array_append_value(array, value) == JSONFailure) {
         json_value_free(value);
         return JSONFailure;
@@ -1464,8 +1583,9 @@
 JSON_Status json_object_set_value(JSON_Object *object, const char *name, JSON_Value *value) {
     size_t i = 0;
     JSON_Value *old_value;
-    if (object == NULL || name == NULL || value == NULL)
+    if (object == NULL || name == NULL || value == NULL) {
         return JSONFailure;
+    }
     old_value = json_object_get_value(object, name);
     if (old_value != NULL) { /* free and overwrite old value */
         json_value_free(old_value);
@@ -1501,8 +1621,9 @@
     char *current_name = NULL;
     JSON_Object *temp_obj = NULL;
     JSON_Value *new_value = NULL;
-    if (value == NULL || name == NULL || value == NULL)
+    if (value == NULL || name == NULL || value == NULL) {
         return JSONFailure;
+    }
     dot_pos = strchr(name, '.');
     if (dot_pos == NULL) {
         return json_object_set_value(object, name, value);
@@ -1529,8 +1650,9 @@
 
 JSON_Status json_object_dotset_string(JSON_Object *object, const char *name, const char *string) {
     JSON_Value *value = json_value_init_string(string);
-    if (value == NULL)
+    if (value == NULL) {
         return JSONFailure;
+    }
     if (json_object_dotset_value(object, name, value) == JSONFailure) {
         json_value_free(value);
         return JSONFailure;
@@ -1540,8 +1662,9 @@
 
 JSON_Status json_object_dotset_number(JSON_Object *object, const char *name, double number) {
     JSON_Value *value = json_value_init_number(number);
-    if (value == NULL)
+    if (value == NULL) {
         return JSONFailure;
+    }
     if (json_object_dotset_value(object, name, value) == JSONFailure) {
         json_value_free(value);
         return JSONFailure;
@@ -1551,8 +1674,9 @@
 
 JSON_Status json_object_dotset_boolean(JSON_Object *object, const char *name, int boolean) {
     JSON_Value *value = json_value_init_boolean(boolean);
-    if (value == NULL)
+    if (value == NULL) {
         return JSONFailure;
+    }
     if (json_object_dotset_value(object, name, value) == JSONFailure) {
         json_value_free(value);
         return JSONFailure;
@@ -1562,8 +1686,9 @@
 
 JSON_Status json_object_dotset_null(JSON_Object *object, const char *name) {
     JSON_Value *value = json_value_init_null();
-    if (value == NULL)
+    if (value == NULL) {
         return JSONFailure;
+    }
     if (json_object_dotset_value(object, name, value) == JSONFailure) {
         json_value_free(value);
         return JSONFailure;
@@ -1573,8 +1698,9 @@
 
 JSON_Status json_object_remove(JSON_Object *object, const char *name) {
     size_t i = 0, last_item_index = 0;
-    if (object == NULL || json_object_get_value(object, name) == NULL)
+    if (object == NULL || json_object_get_value(object, name) == NULL) {
         return JSONFailure;
+    }
     last_item_index = json_object_get_count(object) - 1;
     for (i = 0; i < json_object_get_count(object); i++) {
         if (strcmp(object->names[i], name) == 0) {
@@ -1629,24 +1755,27 @@
     JSON_Value_Type schema_type = JSONError, value_type = JSONError;
     const char *key = NULL;
     size_t i = 0, count = 0;
-    if (schema == NULL || value == NULL)
+    if (schema == NULL || value == NULL) {
         return JSONFailure;
+    }
     schema_type = json_value_get_type(schema);
     value_type = json_value_get_type(value);
-    if (schema_type != value_type && schema_type != JSONNull) /* null represents all values */
+    if (schema_type != value_type && schema_type != JSONNull) { /* null represents all values */
         return JSONFailure;
+    }
     switch (schema_type) {
         case JSONArray:
             schema_array = json_value_get_array(schema);
             value_array = json_value_get_array(value);
             count = json_array_get_count(schema_array);
-            if (count == 0)
+            if (count == 0) {
                 return JSONSuccess; /* Empty array allows all types */
+            }
             /* Get first value from array, rest is ignored */
             temp_schema_value = json_array_get_value(schema_array, 0);
             for (i = 0; i < json_array_get_count(value_array); i++) {
                 temp_value = json_array_get_value(value_array, i);
-                if (json_validate(temp_schema_value, temp_value) == 0) {
+                if (json_validate(temp_schema_value, temp_value) == JSONFailure) {
                     return JSONFailure;
                 }
             }
@@ -1655,18 +1784,21 @@
             schema_object = json_value_get_object(schema);
             value_object = json_value_get_object(value);
             count = json_object_get_count(schema_object);
-            if (count == 0)
+            if (count == 0) {
                 return JSONSuccess; /* Empty object allows all objects */
-            else if (json_object_get_count(value_object) < count)
+            } else if (json_object_get_count(value_object) < count) {
                 return JSONFailure; /* Tested object mustn't have less name-value pairs than schema */
+            }
             for (i = 0; i < count; i++) {
                 key = json_object_get_name(schema_object, i);
                 temp_schema_value = json_object_get_value(schema_object, key);
                 temp_value = json_object_get_value(value_object, key);
-                if (temp_value == NULL)
+                if (temp_value == NULL) {
                     return JSONFailure;
-                if (json_validate(temp_schema_value, temp_value) == JSONFailure)
+                }
+                if (json_validate(temp_schema_value, temp_value) == JSONFailure) {
                     return JSONFailure;
+                }
             }
             return JSONSuccess;
         case JSONString: case JSONNumber: case JSONBoolean: case JSONNull: