X-Git-Url: http://www.project-moonshot.org/gitweb/?a=blobdiff_plain;f=src%2Fvalue.c;h=c84bfd30c7d9f351f6141a8d7b6db421b33b26cd;hb=234ee4728124d8d07412d159b45dd10854b6d6d8;hp=6f0094b64cd137bcaba04d8ec04d4cd67e855f43;hpb=902bcdaa5e0d0273b1ba576c2fd676a5565b6d5e;p=jansson.git diff --git a/src/value.c b/src/value.c index 6f0094b..c84bfd3 100644 --- a/src/value.c +++ b/src/value.c @@ -1,3 +1,10 @@ +/* + * Copyright (c) 2009 Petri Lehtinen + * + * Jansson is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + #define _GNU_SOURCE #include #include @@ -111,23 +118,43 @@ json_t *json_object_get(const json_t *json, const char *key) return hashtable_get(&object->hashtable, key); } -int json_object_set_nocheck(json_t *json, const char *key, json_t *value) +int json_object_set_new_nocheck(json_t *json, const char *key, json_t *value) { json_object_t *object; - if(!json_is_object(json)) + if(!key || !value) return -1; + if(!json_is_object(json)) + { + json_decref(value); + return -1; + } object = json_to_object(json); - return hashtable_set(&object->hashtable, strdup(key), json_incref(value)); + + if(hashtable_set(&object->hashtable, strdup(key), value)) + { + json_decref(value); + return -1; + } + + return 0; } -int json_object_set(json_t *json, const char *key, json_t *value) +int json_object_set_nocheck(json_t *json, const char *key, json_t *value) +{ + return json_object_set_new_nocheck(json, key, json_incref(value)); +} + +int json_object_set_new(json_t *json, const char *key, json_t *value) { if(!utf8_check_string(key, -1)) + { + json_decref(value); return -1; + } - return json_object_set_nocheck(json, key, value); + return json_object_set_new_nocheck(json, key, value); } int json_object_del(json_t *json, const char *key) @@ -222,41 +249,63 @@ json_t *json_array_get(const json_t *json, unsigned int index) return NULL; array = json_to_array(json); - if(index >= array->size) + if(index >= array->entries) return NULL; return array->table[index]; } -int json_array_set(json_t *json, unsigned int index, json_t *value) +int json_array_set_new(json_t *json, unsigned int index, json_t *value) { json_array_t *array; + + if(!value) + return -1; + if(!json_is_array(json)) + { + json_decref(value); return -1; + } array = json_to_array(json); - if(index >= array->size) + if(index >= array->entries) + { + json_decref(value); return -1; + } + + json_decref(array->table[index]); + array->table[index] = value; - array->table[index] = json_incref(value); return 0; } -int json_array_append(json_t *json, json_t *value) +int json_array_append_new(json_t *json, json_t *value) { json_array_t *array; + + if(!value) + return -1; + if(!json_is_array(json)) + { + json_decref(value); return -1; + } array = json_to_array(json); if(array->entries == array->size) { array->size = max(8, array->size * 2); array->table = realloc(array->table, array->size * sizeof(json_t *)); if(!array->table) + { + json_decref(value); return -1; + } } - array->table[array->entries] = json_incref(value); + array->table[array->entries] = value; array->entries++; return 0; @@ -267,18 +316,28 @@ int json_array_append(json_t *json, json_t *value) json_t *json_string_nocheck(const char *value) { - json_string_t *string = malloc(sizeof(json_string_t)); + json_string_t *string; + + if(!value) + return NULL; + + string = malloc(sizeof(json_string_t)); if(!string) return NULL; json_init(&string->json, JSON_STRING); string->value = strdup(value); + if(!string->value) { + free(string); + return NULL; + } + return &string->json; } json_t *json_string(const char *value) { - if(!utf8_check_string(value, -1)) + if(!value || !utf8_check_string(value, -1)) return NULL; return json_string_nocheck(value);