+/*
+ * Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
+ *
+ * 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 <stdlib.h>
#include <string.h>
#include <jansson.h>
#include "hashtable.h"
+#include "jansson_private.h"
+#include "utf.h"
#include "util.h"
#define container_of(ptr_, type_, member_) \
return hashtable_get(&object->hashtable, key);
}
-int json_object_set(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_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_new_nocheck(json, key, value);
}
int json_object_del(json_t *json, const char *key)
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;
/*** string ***/
-json_t *json_string(const char *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(!value || !utf8_check_string(value, -1))
+ return NULL;
+
+ return json_string_nocheck(value);
+}
+
const char *json_string_value(const json_t *json)
{
if(!json_is_string(json))