Fixed json_object_object_add().

* Return value of json_object_object_add() changed from void to int.
  Return value now indicates success or failure.

* Check whether allocations are successful.

* Do not exit program from within the library.
This commit is contained in:
Alexander Klauer
2013-01-08 14:24:21 +01:00
parent 85da28c534
commit 2be921d883
4 changed files with 82 additions and 18 deletions

View File

@@ -371,7 +371,7 @@ struct lh_table* json_object_get_object(struct json_object *jso)
}
}
void json_object_object_add(struct json_object* jso, const char *key,
int json_object_object_add(struct json_object* jso, const char *key,
struct json_object *val)
{
// We lookup the entry and replace the value, rather than just deleting
@@ -381,13 +381,19 @@ void json_object_object_add(struct json_object* jso, const char *key,
existing_entry = lh_table_lookup_entry(jso->o.c_object, (void*)key);
if (!existing_entry)
{
lh_table_insert(jso->o.c_object, strdup(key), val);
return;
char * keydup = strdup( key );
if ( keydup == NULL ) {
return -1;
}
return lh_table_insert(jso->o.c_object, keydup, val);
}
existing_value = (void *)existing_entry->v;
if (existing_value)
json_object_put(existing_value);
existing_entry->v = val;
return 0;
}
struct json_object* json_object_object_get(struct json_object* jso, const char *key)