summaryrefslogtreecommitdiff
path: root/json_object.c
diff options
context:
space:
mode:
authordota17 <chenguopingdota@163.com>2020-03-28 10:25:00 +0800
committerdota17 <chenguopingdota@163.com>2020-04-03 11:39:30 +0800
commit8b162c4b896e5b40feb587aedb7c4f687d14dc67 (patch)
tree1404f3ba6994caf33f5147c793307c2569ca3226 /json_object.c
parentc117d8a8a83d5bcdd433b05e1846ccb73eb3413d (diff)
downloadjson-c-8b162c4b896e5b40feb587aedb7c4f687d14dc67.tar.gz
clang-format the files
Diffstat (limited to 'json_object.c')
-rw-r--r--json_object.c930
1 files changed, 451 insertions, 479 deletions
diff --git a/json_object.c b/json_object.c
index 3dbd191..8817edc 100644
--- a/json_object.c
+++ b/json_object.c
@@ -16,23 +16,23 @@
#include <assert.h>
#include <ctype.h>
+#include <math.h>
+#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
-#include <stddef.h>
#include <string.h>
-#include <math.h>
-#include "debug.h"
-#include "printbuf.h"
-#include "linkhash.h"
#include "arraylist.h"
+#include "debug.h"
#include "json_inttypes.h"
#include "json_object.h"
#include "json_object_private.h"
#include "json_util.h"
+#include "linkhash.h"
#include "math_compat.h"
-#include "strdup_compat.h"
+#include "printbuf.h"
#include "snprintf_compat.h"
+#include "strdup_compat.h"
#if SIZEOF_LONG_LONG != SIZEOF_INT64_T
#error "The long long type isn't 64-bits"
@@ -44,8 +44,8 @@
const char *json_number_chars = "0123456789.+-eE";
const char *json_hex_chars = "0123456789abcdefABCDEF";
-static void json_object_generic_delete(struct json_object* jso);
-static struct json_object* json_object_new(enum json_type o_type);
+static void json_object_generic_delete(struct json_object *jso);
+static struct json_object *json_object_new(enum json_type o_type);
static json_object_to_json_string_fn json_object_object_to_json_string;
static json_object_to_json_string_fn json_object_boolean_to_json_string;
@@ -55,20 +55,20 @@ static json_object_to_json_string_fn json_object_string_to_json_string;
static json_object_to_json_string_fn json_object_array_to_json_string;
static json_object_to_json_string_fn _json_object_userdata_to_json_string;
-
/* ref count debugging */
#ifdef REFCOUNT_DEBUG
static struct lh_table *json_object_table;
-static void json_object_init(void) __attribute__ ((constructor));
-static void json_object_init(void) {
+static void json_object_init(void) __attribute__((constructor));
+static void json_object_init(void)
+{
MC_DEBUG("json_object_init: creating object table\n");
json_object_table = lh_kptr_table_new(128, NULL);
}
-static void json_object_fini(void) __attribute__ ((destructor));
+static void json_object_fini(void) __attribute__((destructor));
static void json_object_fini(void)
{
struct lh_entry *ent;
@@ -77,13 +77,11 @@ static void json_object_fini(void)
if (json_object_table->count)
{
MC_DEBUG("json_object_fini: %d referenced objects at exit\n",
- json_object_table->count);
+ json_object_table->count);
lh_foreach(json_object_table, ent)
{
- struct json_object* obj =
- (struct json_object*) lh_entry_v(ent);
- MC_DEBUG("\t%s:%p\n",
- json_type_to_name(obj->o_type), obj);
+ struct json_object *obj = (struct json_object *)lh_entry_v(ent);
+ MC_DEBUG("\t%s:%p\n", json_type_to_name(obj->o_type), obj);
}
}
}
@@ -92,14 +90,12 @@ static void json_object_fini(void)
}
#endif /* REFCOUNT_DEBUG */
-
/* helper for accessing the optimized string data component in json_object
*/
-static const char *
-get_string_component(const struct json_object *jso)
+static const char *get_string_component(const struct json_object *jso)
{
- return (jso->o.c_string.len < LEN_DIRECT_STRING_DATA) ?
- jso->o.c_string.str.data : jso->o.c_string.str.ptr;
+ return (jso->o.c_string.len < LEN_DIRECT_STRING_DATA) ? jso->o.c_string.str.data
+ : jso->o.c_string.str.ptr;
}
/* string escaping */
@@ -111,7 +107,7 @@ static int json_escape_str(struct printbuf *pb, const char *str, int len, int fl
while (len--)
{
c = str[pos];
- switch(c)
+ switch (c)
{
case '\b':
case '\n':
@@ -121,41 +117,47 @@ static int json_escape_str(struct printbuf *pb, const char *str, int len, int fl
case '"':
case '\\':
case '/':
- if((flags & JSON_C_TO_STRING_NOSLASHESCAPE) && c == '/')
+ if ((flags & JSON_C_TO_STRING_NOSLASHESCAPE) && c == '/')
{
pos++;
break;
}
- if(pos - start_offset > 0)
+ if (pos - start_offset > 0)
printbuf_memappend(pb, str + start_offset, pos - start_offset);
- if(c == '\b') printbuf_memappend(pb, "\\b", 2);
- else if(c == '\n') printbuf_memappend(pb, "\\n", 2);
- else if(c == '\r') printbuf_memappend(pb, "\\r", 2);
- else if(c == '\t') printbuf_memappend(pb, "\\t", 2);
- else if(c == '\f') printbuf_memappend(pb, "\\f", 2);
- else if(c == '"') printbuf_memappend(pb, "\\\"", 2);
- else if(c == '\\') printbuf_memappend(pb, "\\\\", 2);
- else if(c == '/') printbuf_memappend(pb, "\\/", 2);
+ if (c == '\b')
+ printbuf_memappend(pb, "\\b", 2);
+ else if (c == '\n')
+ printbuf_memappend(pb, "\\n", 2);
+ else if (c == '\r')
+ printbuf_memappend(pb, "\\r", 2);
+ else if (c == '\t')
+ printbuf_memappend(pb, "\\t", 2);
+ else if (c == '\f')
+ printbuf_memappend(pb, "\\f", 2);
+ else if (c == '"')
+ printbuf_memappend(pb, "\\\"", 2);
+ else if (c == '\\')
+ printbuf_memappend(pb, "\\\\", 2);
+ else if (c == '/')
+ printbuf_memappend(pb, "\\/", 2);
start_offset = ++pos;
break;
default:
- if(c < ' ')
+ if (c < ' ')
{
char sbuf[7];
- if(pos - start_offset > 0)
- printbuf_memappend(pb,
- str + start_offset,
- pos - start_offset);
- snprintf(sbuf, sizeof(sbuf),
- "\\u00%c%c",
- json_hex_chars[c >> 4],
- json_hex_chars[c & 0xf]);
- printbuf_memappend_fast(pb, sbuf, (int) sizeof(sbuf) - 1);
+ if (pos - start_offset > 0)
+ printbuf_memappend(pb, str + start_offset,
+ pos - start_offset);
+ snprintf(sbuf, sizeof(sbuf), "\\u00%c%c", json_hex_chars[c >> 4],
+ json_hex_chars[c & 0xf]);
+ printbuf_memappend_fast(pb, sbuf, (int)sizeof(sbuf) - 1);
start_offset = ++pos;
- } else
+ }
+ else
pos++;
}
}
@@ -164,12 +166,12 @@ static int json_escape_str(struct printbuf *pb, const char *str, int len, int fl
return 0;
}
-
/* reference counting */
-struct json_object* json_object_get(struct json_object *jso)
+struct json_object *json_object_get(struct json_object *jso)
{
- if (!jso) return jso;
+ if (!jso)
+ return jso;
// Don't overflow the refcounter.
assert(jso->_ref_count < UINT32_MAX);
@@ -178,14 +180,15 @@ struct json_object* json_object_get(struct json_object *jso)
__sync_add_and_fetch(&jso->_ref_count, 1);
#else
++jso->_ref_count;
-#endif
+#endif
return jso;
}
int json_object_put(struct json_object *jso)
{
- if(!jso) return 0;
+ if (!jso)
+ return 0;
/* Avoid invalid free and crash explicitly instead of (silently)
* segfaulting.
@@ -199,9 +202,11 @@ int json_object_put(struct json_object *jso)
* as that can result in the thread that loses the race to 0
* operating on an already-freed object.
*/
- if (__sync_sub_and_fetch(&jso->_ref_count, 1) > 0) return 0;
+ if (__sync_sub_and_fetch(&jso->_ref_count, 1) > 0)
+ return 0;
#else
- if (--jso->_ref_count > 0) return 0;
+ if (--jso->_ref_count > 0)
+ return 0;
#endif
if (jso->_user_delete)
@@ -210,25 +215,23 @@ int json_object_put(struct json_object *jso)
return 1;
}
-
/* generic object construction and destruction parts */
-static void json_object_generic_delete(struct json_object* jso)
+static void json_object_generic_delete(struct json_object *jso)
{
#ifdef REFCOUNT_DEBUG
- MC_DEBUG("json_object_delete_%s: %p\n",
- json_type_to_name(jso->o_type), jso);
+ MC_DEBUG("json_object_delete_%s: %p\n", json_type_to_name(jso->o_type), jso);
lh_table_delete(json_object_table, jso);
#endif /* REFCOUNT_DEBUG */
printbuf_free(jso->_pb);
free(jso);
}
-static struct json_object* json_object_new(enum json_type o_type)
+static struct json_object *json_object_new(enum json_type o_type)
{
struct json_object *jso;
- jso = (struct json_object*)calloc(1, sizeof(struct json_object));
+ jso = (struct json_object *)calloc(1, sizeof(struct json_object));
if (!jso)
return NULL;
jso->o_type = o_type;
@@ -241,7 +244,6 @@ static struct json_object* json_object_new(enum json_type o_type)
return jso;
}
-
/* type checking functions */
int json_object_is_type(const struct json_object *jso, enum json_type type)
@@ -258,12 +260,12 @@ enum json_type json_object_get_type(const struct json_object *jso)
return jso->o_type;
}
-void* json_object_get_userdata(json_object *jso) {
+void *json_object_get_userdata(json_object *jso)
+{
return jso ? jso->_userdata : NULL;
}
-void json_object_set_userdata(json_object *jso, void *userdata,
- json_object_delete_fn *user_delete)
+void json_object_set_userdata(json_object *jso, void *userdata, json_object_delete_fn *user_delete)
{
// Can't return failure, so abort if we can't perform the operation.
assert(jso != NULL);
@@ -278,30 +280,24 @@ void json_object_set_userdata(json_object *jso, void *userdata,
/* set a custom conversion to string */
-void json_object_set_serializer(json_object *jso,
- json_object_to_json_string_fn *to_string_func,
- void *userdata,
- json_object_delete_fn *user_delete)
+void json_object_set_serializer(json_object *jso, json_object_to_json_string_fn *to_string_func,
+ void *userdata, json_object_delete_fn *user_delete)
{
json_object_set_userdata(jso, userdata, user_delete);
if (to_string_func == NULL)
{
// Reset to the standard serialization function
- switch(jso->o_type)
+ switch (jso->o_type)
{
- case json_type_null:
- jso->_to_json_string = NULL;
- break;
+ case json_type_null: jso->_to_json_string = NULL; break;
case json_type_boolean:
jso->_to_json_string = &json_object_boolean_to_json_string;
break;
case json_type_double:
jso->_to_json_string = &json_object_double_to_json_string_default;
break;
- case json_type_int:
- jso->_to_json_string = &json_object_int_to_json_string;
- break;
+ case json_type_int: jso->_to_json_string = &json_object_int_to_json_string; break;
case json_type_object:
jso->_to_json_string = &json_object_object_to_json_string;
break;
@@ -318,10 +314,9 @@ void json_object_set_serializer(json_object *jso,
jso->_to_json_string = to_string_func;
}
-
/* extended conversion to string */
-const char* json_object_to_json_string_length(struct json_object *jso, int flags, size_t *length)
+const char *json_object_to_json_string_length(struct json_object *jso, int flags, size_t *length)
{
const char *r = NULL;
size_t s = 0;
@@ -335,7 +330,7 @@ const char* json_object_to_json_string_length(struct json_object *jso, int flags
{
printbuf_reset(jso->_pb);
- if(jso->_to_json_string(jso, jso->_pb, 0, flags) >= 0)
+ if (jso->_to_json_string(jso, jso->_pb, 0, flags) >= 0)
{
s = (size_t)jso->_pb->bpos;
r = jso->_pb->buf;
@@ -347,14 +342,14 @@ const char* json_object_to_json_string_length(struct json_object *jso, int flags
return r;
}
-const char* json_object_to_json_string_ext(struct json_object *jso, int flags)
+const char *json_object_to_json_string_ext(struct json_object *jso, int flags)
{
return json_object_to_json_string_length(jso, flags, NULL);
}
/* backwards-compatible conversion to string */
-const char* json_object_to_json_string(struct json_object *jso)
+const char *json_object_to_json_string(struct json_object *jso)
{
return json_object_to_json_string_ext(jso, JSON_C_TO_STRING_SPACED);
}
@@ -376,10 +371,8 @@ static void indent(struct printbuf *pb, int level, int flags)
/* json_object_object */
-static int json_object_object_to_json_string(struct json_object* jso,
- struct printbuf *pb,
- int level,
- int flags)
+static int json_object_object_to_json_string(struct json_object *jso, struct printbuf *pb,
+ int level, int flags)
{
int had_children = 0;
struct json_object_iter iter;
@@ -398,24 +391,23 @@ static int json_object_object_to_json_string(struct json_object* jso,
had_children = 1;
if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
printbuf_strappend(pb, " ");
- indent(pb, level+1, flags);
+ indent(pb, level + 1, flags);
printbuf_strappend(pb, "\"");
json_escape_str(pb, iter.key, strlen(iter.key), flags);
if (flags & JSON_C_TO_STRING_SPACED)
printbuf_strappend(pb, "\": ");
else
printbuf_strappend(pb, "\":");
- if(iter.val == NULL)
+ if (iter.val == NULL)
printbuf_strappend(pb, "null");
- else
- if (iter.val->_to_json_string(iter.val, pb, level+1,flags) < 0)
- return -1;
+ else if (iter.val->_to_json_string(iter.val, pb, level + 1, flags) < 0)
+ return -1;
}
if (flags & JSON_C_TO_STRING_PRETTY)
{
if (had_children)
printbuf_strappend(pb, "\n");
- indent(pb,level,flags);
+ indent(pb, level, flags);
}
if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
return printbuf_strappend(pb, /*{*/ " }");
@@ -423,29 +415,28 @@ static int json_object_object_to_json_string(struct json_object* jso,
return printbuf_strappend(pb, /*{*/ "}");
}
-
static void json_object_lh_entry_free(struct lh_entry *ent)
{
if (!ent->k_is_constant)
free(lh_entry_k(ent));
- json_object_put((struct json_object*)lh_entry_v(ent));
+ json_object_put((struct json_object *)lh_entry_v(ent));
}
-static void json_object_object_delete(struct json_object* jso)
+static void json_object_object_delete(struct json_object *jso)
{
lh_table_free(jso->o.c_object);
json_object_generic_delete(jso);
}
-struct json_object* json_object_new_object(void)
+struct json_object *json_object_new_object(void)
{
struct json_object *jso = json_object_new(json_type_object);
if (!jso)
return NULL;
jso->_delete = &json_object_object_delete;
jso->_to_json_string = &json_object_object_to_json_string;
- jso->o.c_object = lh_kchar_table_new(JSON_OBJECT_DEF_HASH_ENTRIES,
- &json_object_lh_entry_free);
+ jso->o.c_object =
+ lh_kchar_table_new(JSON_OBJECT_DEF_HASH_ENTRIES, &json_object_lh_entry_free);
if (!jso->o.c_object)
{
json_object_generic_delete(jso);
@@ -455,23 +446,19 @@ struct json_object* json_object_new_object(void)
return jso;
}
-struct lh_table* json_object_get_object(const struct json_object *jso)
+struct lh_table *json_object_get_object(const struct json_object *jso)
{
if (!jso)
return NULL;
- switch(jso->o_type)
+ switch (jso->o_type)
{
- case json_type_object:
- return jso->o.c_object;
- default:
- return NULL;
+ case json_type_object: return jso->o.c_object;
+ default: return NULL;
}
}
-int json_object_object_add_ex(struct json_object* jso,
- const char *const key,
- struct json_object *const val,
- const unsigned opts)
+int json_object_object_add_ex(struct json_object *jso, const char *const key,
+ struct json_object *const val, const unsigned opts)
{
struct json_object *existing_value = NULL;
struct lh_entry *existing_entry;
@@ -482,9 +469,10 @@ int json_object_object_add_ex(struct json_object* jso,
// We lookup the entry and replace the value, rather than just deleting
// and re-adding it, so the existing key remains valid.
hash = lh_get_hash(jso->o.c_object, (const void *)key);
- existing_entry = (opts & JSON_C_OBJECT_ADD_KEY_IS_NEW) ? NULL :
- lh_table_lookup_entry_w_hash(jso->o.c_object,
- (const void *)key, hash);
+ existing_entry =
+ (opts & JSON_C_OBJECT_ADD_KEY_IS_NEW)
+ ? NULL
+ : lh_table_lookup_entry_w_hash(jso->o.c_object, (const void *)key, hash);
// The caller must avoid creating loops in the object tree, but do a
// quick check anyway to make sure we're not creating a trivial loop.
@@ -493,26 +481,24 @@ int json_object_object_add_ex(struct json_object* jso,
if (!existing_entry)
{
- const void *const k = (opts & JSON_C_OBJECT_KEY_IS_CONSTANT) ?
- (const void *)key : strdup(key);
+ const void *const k =
+ (opts & JSON_C_OBJECT_KEY_IS_CONSTANT) ? (const void *)key : strdup(key);
if (k == NULL)
return -1;
return lh_table_insert_w_hash(jso->o.c_object, k, val, hash, opts);
}
- existing_value = (json_object *) lh_entry_v(existing_entry);
+ existing_value = (json_object *)lh_entry_v(existing_entry);
if (existing_value)
json_object_put(existing_value);
existing_entry->v = val;
return 0;
}
-int json_object_object_add(struct json_object* jso, const char *key,
- struct json_object *val)
+int json_object_object_add(struct json_object *jso, const char *key, struct json_object *val)
{
return json_object_object_add_ex(jso, key, val, 0);
}
-
int json_object_object_length(const struct json_object *jso)
{
assert(json_object_get_type(jso) == json_type_object);
@@ -524,16 +510,15 @@ size_t json_c_object_sizeof(void)
return sizeof(struct json_object);
}
-struct json_object* json_object_object_get(const struct json_object* jso,
- const char *key)
+struct json_object *json_object_object_get(const struct json_object *jso, const char *key)
{
struct json_object *result = NULL;
json_object_object_get_ex(jso, key, &result);
return result;
}
-json_bool json_object_object_get_ex(const struct json_object* jso, const char *key,
- struct json_object **value)
+json_bool json_object_object_get_ex(const struct json_object *jso, const char *key,
+ struct json_object **value)
{
if (value != NULL)
*value = NULL;
@@ -541,11 +526,10 @@ json_bool json_object_object_get_ex(const struct json_object* jso, const char *k
if (NULL == jso)
return 0;
- switch(jso->o_type)
+ switch (jso->o_type)
{
case json_type_object:
- return lh_table_lookup_ex(jso->o.c_object, (const void *) key,
- (void**) value);
+ return lh_table_lookup_ex(jso->o.c_object, (const void *)key, (void **)value);
default:
if (value != NULL)
*value = NULL;
@@ -553,26 +537,23 @@ json_bool json_object_object_get_ex(const struct json_object* jso, const char *k
}
}
-void json_object_object_del(struct json_object* jso, const char *key)
+void json_object_object_del(struct json_object *jso, const char *key)
{
assert(json_object_get_type(jso) == json_type_object);
lh_table_delete(jso->o.c_object, key);
}
-
/* json_object_boolean */
-static int json_object_boolean_to_json_string(struct json_object* jso,
- struct printbuf *pb,
- int level,
- int flags)
+static int json_object_boolean_to_json_string(struct json_object *jso, struct printbuf *pb,
+ int level, int flags)
{
if (jso->o.c_boolean)
return printbuf_strappend(pb, "true");
return printbuf_strappend(pb, "false");
}
-struct json_object* json_object_new_boolean(json_bool b)
+struct json_object *json_object_new_boolean(json_bool b)
{
struct json_object *jso = json_object_new(json_type_boolean);
if (!jso)
@@ -586,42 +567,34 @@ json_bool json_object_get_boolean(const struct json_object *jso)
{
if (!jso)
return 0;
- switch(jso->o_type)
+ switch (jso->o_type)
{
- case json_type_boolean:
- return jso->o.c_boolean;
+ case json_type_boolean: return jso->o.c_boolean;
case json_type_int:
- switch(jso->o.c_int.cint_type) {
- case json_object_int_type_int64:
- return (jso->o.c_int.cint.c_int64 != 0);
- case json_object_int_type_uint64:
- return (jso->o.c_int.cint.c_uint64 != 0);
- default:
- json_abort("invalid cint_type");
+ switch (jso->o.c_int.cint_type)
+ {
+ case json_object_int_type_int64: return (jso->o.c_int.cint.c_int64 != 0);
+ case json_object_int_type_uint64: return (jso->o.c_int.cint.c_uint64 != 0);
+ default: json_abort("invalid cint_type");
}
- case json_type_double:
- return (jso->o.c_double != 0);
- case json_type_string:
- return (jso->o.c_string.len != 0);
- default:
- return 0;
+ case json_type_double: return (jso->o.c_double != 0);
+ case json_type_string: return (jso->o.c_string.len != 0);
+ default: return 0;
}
}
-int json_object_set_boolean(struct json_object *jso,json_bool new_value){
- if (!jso || jso->o_type!=json_type_boolean)
+int json_object_set_boolean(struct json_object *jso, json_bool new_value)
+{
+ if (!jso || jso->o_type != json_type_boolean)
return 0;
- jso->o.c_boolean=new_value;
+ jso->o.c_boolean = new_value;
return 1;
}
-
/* json_object_int */
-static int json_object_int_to_json_string(struct json_object* jso,
- struct printbuf *pb,
- int level,
- int flags)
+static int json_object_int_to_json_string(struct json_object *jso, struct printbuf *pb, int level,
+ int flags)
{
/* room for 19 digits, the sign char, and a null term */
char sbuf[21];
@@ -629,10 +602,10 @@ static int json_object_int_to_json_string(struct json_object* jso,
snprintf(sbuf, sizeof(sbuf), "%" PRId64, jso->o.c_int.cint.c_int64);
else
snprintf(sbuf, sizeof(sbuf), "%" PRIu64, jso->o.c_int.cint.c_uint64);
- return printbuf_memappend (pb, sbuf, strlen(sbuf));
+ return printbuf_memappend(pb, sbuf, strlen(sbuf));
}
-struct json_object* json_object_new_int(int32_t i)
+struct json_object *json_object_new_int(int32_t i)
{
struct json_object *jso = json_object_new(json_type_int);
if (!jso)
@@ -645,58 +618,62 @@ struct json_object* json_object_new_int(int32_t i)
int32_t json_object_get_int(const struct json_object *jso)
{
- int64_t cint64;
- enum json_type o_type;
+ int64_t cint64;
+ enum json_type o_type;
- if(!jso) return 0;
+ if (!jso)
+ return 0;
- o_type = jso->o_type;
- if (jso->o.c_int.cint_type == json_object_int_type_int64) {
- cint64 = jso->o.c_int.cint.c_int64;
- } else {
- if (jso->o.c_int.cint.c_uint64 >= INT64_MAX)
- cint64 = INT64_MAX;
- else
- cint64 = (int64_t)jso->o.c_int.cint.c_uint64;
- }
+ o_type = jso->o_type;
+ if (jso->o.c_int.cint_type == json_object_int_type_int64)
+ {
+ cint64 = jso->o.c_int.cint.c_int64;
+ }
+ else
+ {
+ if (jso->o.c_int.cint.c_uint64 >= INT64_MAX)
+ cint64 = INT64_MAX;
+ else
+ cint64 = (int64_t)jso->o.c_int.cint.c_uint64;
+ }
- if (o_type == json_type_string)
- {
- /*
+ if (o_type == json_type_string)
+ {
+ /*
* Parse strings into 64-bit numbers, then use the
* 64-to-32-bit number handling below.
*/
- if (json_parse_int64(get_string_component(jso), &cint64) != 0)
- return 0; /* whoops, it didn't work. */
- o_type = json_type_int;
- }
-
- switch(o_type) {
- case json_type_int:
- /* Make sure we return the correct values for out of range numbers. */
- if (cint64 <= INT32_MIN)
- return INT32_MIN;
- if (cint64 >= INT32_MAX)
- return INT32_MAX;
- return (int32_t) cint64;
- case json_type_double:
- if (jso->o.c_double <= INT32_MIN)
- return INT32_MIN;
- if (jso->o.c_double >= INT32_MAX)
- return INT32_MAX;
- return (int32_t)jso->o.c_double;
- case json_type_boolean:
- return jso->o.c_boolean;
- default:
- return 0;
- }
-}
-
-int json_object_set_int(struct json_object *jso,int new_value){
- return json_object_set_int64(jso,(int64_t)new_value);
-}
-
-struct json_object* json_object_new_int64(int64_t i)
+ if (json_parse_int64(get_string_component(jso), &cint64) != 0)
+ return 0; /* whoops, it didn't work. */
+ o_type = json_type_int;
+ }
+
+ switch (o_type)
+ {
+ case json_type_int:
+ /* Make sure we return the correct values for out of range numbers. */
+ if (cint64 <= INT32_MIN)
+ return INT32_MIN;
+ if (cint64 >= INT32_MAX)
+ return INT32_MAX;
+ return (int32_t)cint64;
+ case json_type_double:
+ if (jso->o.c_double <= INT32_MIN)
+ return INT32_MIN;
+ if (jso->o.c_double >= INT32_MAX)
+ return INT32_MAX;
+ return (int32_t)jso->o.c_double;
+ case json_type_boolean: return jso->o.c_boolean;
+ default: return 0;
+ }
+}
+
+int json_object_set_int(struct json_object *jso, int new_value)
+{
+ return json_object_set_int64(jso, (int64_t)new_value);
+}
+
+struct json_object *json_object_new_int64(int64_t i)
{
struct json_object *jso = json_object_new(json_type_int);
if (!jso)
@@ -707,7 +684,7 @@ struct json_object* json_object_new_int64(int64_t i)
return jso;
}
-struct json_object* json_object_new_uint64(uint64_t i)
+struct json_object *json_object_new_uint64(uint64_t i)
{
struct json_object *jso = json_object_new(json_type_int);
if (!jso)
@@ -724,18 +701,17 @@ int64_t json_object_get_int64(const struct json_object *jso)
if (!jso)
return 0;
- switch(jso->o_type)
+ switch (jso->o_type)
{
case json_type_int:
- switch(jso->o.c_int.cint_type) {
- case json_object_int_type_int64:
- return jso->o.c_int.cint.c_int64;
+ switch (jso->o.c_int.cint_type)
+ {
+ case json_object_int_type_int64: return jso->o.c_int.cint.c_int64;
case json_object_int_type_uint64:
if (jso->o.c_int.cint.c_uint64 >= INT64_MAX)
return INT64_MAX;
return (int64_t)jso->o.c_int.cint.c_uint64;
- default:
- json_abort("invalid cint_type");
+ default: json_abort("invalid cint_type");
}
case json_type_double:
// INT64_MAX can't be exactly represented as a double
@@ -745,14 +721,12 @@ int64_t json_object_get_int64(const struct json_object *jso)
if (jso->o.c_double <= INT64_MIN)
return INT64_MIN;
return (int64_t)jso->o.c_double;
- case json_type_boolean:
- return jso->o.c_boolean;
+ case json_type_boolean: return jso->o.c_boolean;
case json_type_string:
if (json_parse_int64(get_string_component(jso), &cint) == 0)
return cint;
/* FALLTHRU */
- default:
- return 0;
+ default: return 0;
}
}
@@ -762,18 +736,17 @@ uint64_t json_object_get_uint64(const struct json_object *jso)
if (!jso)
return 0;
- switch(jso->o_type)
+ switch (jso->o_type)
{
case json_type_int:
- switch(jso->o.c_int.cint_type) {
+ switch (jso->o.c_int.cint_type)
+ {
case json_object_int_type_int64:
if (jso->o.c_int.cint.c_int64 < 0)
return 0;
return (uint64_t)jso->o.c_int.cint.c_int64;
- case json_object_int_type_uint64:
- return jso->o.c_int.cint.c_uint64;
- default:
- json_abort("invalid cint_type");
+ case json_object_int_type_uint64: return jso->o.c_int.cint.c_uint64;
+ default: json_abort("invalid cint_type");
}
case json_type_double:
// UINT64_MAX can't be exactly represented as a double
@@ -783,61 +756,75 @@ uint64_t json_object_get_uint64(const struct json_object *jso)
if (jso->o.c_double < 0)
return 0;
return (uint64_t)jso->o.c_double;
- case json_type_boolean:
- return jso->o.c_boolean;
+ case json_type_boolean: return jso->o.c_boolean;
case json_type_string:
if (json_parse_uint64(get_string_component(jso), &cuint) == 0)
return cuint;
/* FALLTHRU */
- default:
- return 0;
+ default: return 0;
}
}
-int json_object_set_int64(struct json_object *jso,int64_t new_value){
- if (!jso || jso->o_type!=json_type_int)
+int json_object_set_int64(struct json_object *jso, int64_t new_value)
+{
+ if (!jso || jso->o_type != json_type_int)
return 0;
- jso->o.c_int.cint.c_int64=new_value;
+ jso->o.c_int.cint.c_int64 = new_value;
jso->o.c_int.cint_type = json_object_int_type_int64;
return 1;
}
-int json_object_set_uint64(struct json_object *jso,uint64_t new_value){
- if (!jso || jso->o_type!=json_type_int)
+int json_object_set_uint64(struct json_object *jso, uint64_t new_value)
+{
+ if (!jso || jso->o_type != json_type_int)
return 0;
- jso->o.c_int.cint.c_uint64=new_value;
+ jso->o.c_int.cint.c_uint64 = new_value;
jso->o.c_int.cint_type = json_object_int_type_uint64;
return 1;
}
-int json_object_int_inc(struct json_object *jso, int64_t val) {
+int json_object_int_inc(struct json_object *jso, int64_t val)
+{
if (!jso || jso->o_type != json_type_int)
return 0;
- switch(jso->o.c_int.cint_type) {
+ switch (jso->o.c_int.cint_type)
+ {
case json_object_int_type_int64:
- if (val > 0 && jso->o.c_int.cint.c_int64 > INT64_MAX - val) {
- jso->o.c_int.cint.c_uint64 = (uint64_t)jso->o.c_int.cint.c_int64 + (uint64_t)val;
+ if (val > 0 && jso->o.c_int.cint.c_int64 > INT64_MAX - val)
+ {
+ jso->o.c_int.cint.c_uint64 =
+ (uint64_t)jso->o.c_int.cint.c_int64 + (uint64_t)val;
jso->o.c_int.cint_type = json_object_int_type_uint64;
- } else if (val < 0 && jso->o.c_int.cint.c_int64 < INT64_MIN - val) {
+ }
+ else if (val < 0 && jso->o.c_int.cint.c_int64 < INT64_MIN - val)
+ {
jso->o.c_int.cint.c_int64 = INT64_MIN;
- } else {
+ }
+ else
+ {
jso->o.c_int.cint.c_int64 += val;
}
return 1;
case json_object_int_type_uint64:
- if (val > 0 && jso->o.c_int.cint.c_uint64 > UINT64_MAX - (uint64_t)val) {
+ if (val > 0 && jso->o.c_int.cint.c_uint64 > UINT64_MAX - (uint64_t)val)
+ {
jso->o.c_int.cint.c_uint64 = UINT64_MAX;
- } else if (val < 0 && jso->o.c_int.cint.c_uint64 < (uint64_t)(-val)) {
+ }
+ else if (val < 0 && jso->o.c_int.cint.c_uint64 < (uint64_t)(-val))
+ {
jso->o.c_int.cint.c_int64 = (int64_t)jso->o.c_int.cint.c_uint64 + val;
jso->o.c_int.cint_type = json_object_int_type_int64;
- } else if (val < 0 && jso->o.c_int.cint.c_uint64 >= (uint64_t)(-val)) {
+ }
+ else if (val < 0 && jso->o.c_int.cint.c_uint64 >= (uint64_t)(-val))
+ {
jso->o.c_int.cint.c_uint64 -= (uint64_t)(-val);
- } else {
+ }
+ else
+ {
jso->o.c_int.cint.c_uint64 += val;
}
return 1;
- default:
- json_abort("invalid cint_type");
+ default: json_abort("invalid cint_type");
}
}
@@ -880,18 +867,15 @@ int json_c_set_serialization_double_format(const char *double_format, int global
}
else
{
- _json_c_set_last_err("json_c_set_option: invalid global_or_thread value: %d\n", global_or_thread);
+ _json_c_set_last_err("json_c_set_option: invalid global_or_thread value: %d\n",
+ global_or_thread);
return -1;
}
return 0;
}
-
-static int json_object_double_to_json_string_format(struct json_object* jso,
- struct printbuf *pb,
- int level,
- int flags,
- const char *format)
+static int json_object_double_to_json_string_format(struct json_object *jso, struct printbuf *pb,
+ int level, int flags, const char *format)
{
char buf[128], *p, *q;
int size;
@@ -906,7 +890,7 @@ static int json_object_double_to_json_string_format(struct json_object* jso,
}
else if (isinf(jso->o.c_double))
{
- if(jso->o.c_double > 0)
+ if (jso->o.c_double > 0)
size = snprintf(buf, sizeof(buf), "Infinity");
else
size = snprintf(buf, sizeof(buf), "-Infinity");
@@ -924,7 +908,7 @@ static int json_object_double_to_json_string_format(struct json_object* jso,
format = tls_serialization_float_format;
else
#endif
- if (global_serialization_float_format)
+ if (global_serialization_float_format)
format = global_serialization_float_format;
else
format = std_format;
@@ -947,9 +931,7 @@ static int json_object_double_to_json_string_format(struct json_object* jso,
isdigit((unsigned char)buf[0]) ||
(size > 1 && buf[0] == '-' && isdigit((unsigned char)buf[1]));
- if (size < (int)sizeof(buf) - 2 &&
- looks_numeric &&
- !p && /* Has no decimal point */
+ if (size < (int)sizeof(buf) - 2 && looks_numeric && !p && /* Has no decimal point */
strchr(buf, 'e') == NULL && /* Not scientific notation */
format_drops_decimals)
{
@@ -962,12 +944,14 @@ static int json_object_double_to_json_string_format(struct json_object* jso,
{
/* last useful digit, always keep 1 zero */
p++;
- for (q=p ; *q ; q++) {
- if (*q!='0') p=q;
+ for (q = p; *q; q++)
+ {
+ if (*q != '0')
+ p = q;
}
/* drop trailing zeroes */
*(++p) = 0;
- size = p-buf;
+ size = p - buf;
}
}
// although unlikely, snprintf can fail
@@ -982,25 +966,20 @@ static int json_object_double_to_json_string_format(struct json_object* jso,
return size;
}
-static int json_object_double_to_json_string_default(struct json_object* jso,
- struct printbuf *pb,
- int level,
- int flags)
+static int json_object_double_to_json_string_default(struct json_object *jso, struct printbuf *pb,
+ int level, int flags)
{
- return json_object_double_to_json_string_format(jso, pb, level, flags,
- NULL);
+ return json_object_double_to_json_string_format(jso, pb, level, flags, NULL);
}
-int json_object_double_to_json_string(struct json_object* jso,
- struct printbuf *pb,
- int level,
- int flags)
+int json_object_double_to_json_string(struct json_object *jso, struct printbuf *pb, int level,
+ int flags)
{
return json_object_double_to_json_string_format(jso, pb, level, flags,
- (const char *)jso->_userdata);
+ (const char *)jso->_userdata);
}
-struct json_object* json_object_new_double(double d)
+struct json_object *json_object_new_double(double d)
{
struct json_object *jso = json_object_new(json_type_double);
if (!jso)
@@ -1010,7 +989,7 @@ struct json_object* json_object_new_double(double d)
return jso;
}
-struct json_object* json_object_new_double_s(double d, const char *ds)
+struct json_object *json_object_new_double_s(double d, const char *ds)
{
char *new_ds;
struct json_object *jso = json_object_new_double(d);
@@ -1024,8 +1003,8 @@ struct json_object* json_object_new_double_s(double d, const char *ds)
errno = ENOMEM;
return NULL;
}
- json_object_set_serializer(jso, _json_object_userdata_to_json_string,
- new_ds, json_object_free_userdata);
+ json_object_set_serializer(jso, _json_object_userdata_to_json_string, new_ds,
+ json_object_free_userdata);
return jso;
}
@@ -1034,14 +1013,14 @@ struct json_object* json_object_new_double_s(double d, const char *ds)
* by json_object_new_double_s() just so json_object_set_double() can
* detect when it needs to reset the serializer to the default.
*/
-static int _json_object_userdata_to_json_string(struct json_object *jso,
- struct printbuf *pb, int level, int flags)
+static int _json_object_userdata_to_json_string(struct json_object *jso, struct printbuf *pb,
+ int level, int flags)
{
return json_object_userdata_to_json_string(jso, pb, level, flags);
}
-int json_object_userdata_to_json_string(struct json_object *jso,
- struct printbuf *pb, int level, int flags)
+int json_object_userdata_to_json_string(struct json_object *jso, struct printbuf *pb, int level,
+ int flags)
{
int userdata_len = strlen((const char *)jso->_userdata);
printbuf_memappend(pb, (const char *)jso->_userdata, userdata_len);
@@ -1055,47 +1034,45 @@ void json_object_free_userdata(struct json_object *jso, void *userdata)
double json_object_get_double(const struct json_object *jso)
{
- double cdouble;
- char *errPtr = NULL;
-
- if(!jso) return 0.0;
- switch(jso->o_type) {
- case json_type_double:
- return jso->o.c_double;
- case json_type_int:
- switch(jso->o.c_int.cint_type) {
- case json_object_int_type_int64:
- return jso->o.c_int.cint.c_int64;
- case json_object_int_type_uint64:
- return jso->o.c_int.cint.c_uint64;
- default:
- json_abort("invalid cint_type");
- }
- case json_type_boolean:
- return jso->o.c_boolean;
- case json_type_string:
- errno = 0;
- cdouble = strtod(get_string_component(jso), &errPtr);
-
- /* if conversion stopped at the first character, return 0.0 */
- if (errPtr == get_string_component(jso))
- {
- errno = EINVAL;
- return 0.0;
- }
-
- /*
+ double cdouble;
+ char *errPtr = NULL;
+
+ if (!jso)
+ return 0.0;
+ switch (jso->o_type)
+ {
+ case json_type_double: return jso->o.c_double;
+ case json_type_int:
+ switch (jso->o.c_int.cint_type)
+ {
+ case json_object_int_type_int64: return jso->o.c_int.cint.c_int64;
+ case json_object_int_type_uint64: return jso->o.c_int.cint.c_uint64;
+ default: json_abort("invalid cint_type");
+ }
+ case json_type_boolean: return jso->o.c_boolean;
+ case json_type_string:
+ errno = 0;
+ cdouble = strtod(get_string_component(jso), &errPtr);
+
+ /* if conversion stopped at the first character, return 0.0 */
+ if (errPtr == get_string_component(jso))
+ {
+ errno = EINVAL;
+ return 0.0;
+ }
+
+ /*
* Check that the conversion terminated on something sensible
*
* For example, { "pay" : 123AB } would parse as 123.
*/
- if (*errPtr != '\0')
- {
- errno = EINVAL;
- return 0.0;
- }
+ if (*errPtr != '\0')
+ {
+ errno = EINVAL;
+ return 0.0;
+ }
- /*
+ /*
* If strtod encounters a string which would exceed the
* capacity of a double, it returns +/- HUGE_VAL and sets
* errno to ERANGE. But +/- HUGE_VAL is also a valid result
@@ -1106,31 +1083,27 @@ double json_object_get_double(const struct json_object *jso)
*
* See CERT guideline ERR30-C
*/
- if ((HUGE_VAL == cdouble || -HUGE_VAL == cdouble) &&
- (ERANGE == errno))
- cdouble = 0.0;
- return cdouble;
- default:
- errno = EINVAL;
- return 0.0;
- }
-}
-
-int json_object_set_double(struct json_object *jso,double new_value){
- if (!jso || jso->o_type!=json_type_double)
+ if ((HUGE_VAL == cdouble || -HUGE_VAL == cdouble) && (ERANGE == errno))
+ cdouble = 0.0;
+ return cdouble;
+ default: errno = EINVAL; return 0.0;
+ }
+}
+
+int json_object_set_double(struct json_object *jso, double new_value)
+{
+ if (!jso || jso->o_type != json_type_double)
return 0;
- jso->o.c_double=new_value;
+ jso->o.c_double = new_value;
if (jso->_to_json_string == &_json_object_userdata_to_json_string)
- json_object_set_serializer(jso, NULL, NULL, NULL);
+ json_object_set_serializer(jso, NULL, NULL, NULL);
return 1;
}
/* json_object_string */
-static int json_object_string_to_json_string(struct json_object* jso,
- struct printbuf *pb,
- int level,
- int flags)
+static int json_object_string_to_json_string(struct json_object *jso, struct printbuf *pb,
+ int level, int flags)
{
printbuf_strappend(pb, "\"");
json_escape_str(pb, get_string_component(jso), jso->o.c_string.len, flags);
@@ -1138,14 +1111,14 @@ static int json_object_string_to_json_string(struct json_object* jso,
return 0;
}
-static void json_object_string_delete(struct json_object* jso)
+static void json_object_string_delete(struct json_object *jso)
{
- if(jso->o.c_string.len >= LEN_DIRECT_STRING_DATA)
+ if (jso->o.c_string.len >= LEN_DIRECT_STRING_DATA)
free(jso->o.c_string.str.ptr);
json_object_generic_delete(jso);
}
-struct json_object* json_object_new_string(const char *s)
+struct json_object *json_object_new_string(const char *s)
{
struct json_object *jso = json_object_new(json_type_string);
if (!jso)
@@ -1153,9 +1126,12 @@ struct json_object* json_object_new_string(const char *s)
jso->_delete = &json_object_string_delete;
jso->_to_json_string = &json_object_string_to_json_string;
jso->o.c_string.len = strlen(s);
- if(jso->o.c_string.len < LEN_DIRECT_STRING_DATA) {
+ if (jso->o.c_string.len < LEN_DIRECT_STRING_DATA)
+ {
memcpy(jso->o.c_string.str.data, s, jso->o.c_string.len);
- } else {
+ }
+ else
+ {
jso->o.c_string.str.ptr = strdup(s);
if (!jso->o.c_string.str.ptr)
{
@@ -1167,7 +1143,7 @@ struct json_object* json_object_new_string(const char *s)
return jso;
}
-struct json_object* json_object_new_string_len(const char *s, const int len)
+struct json_object *json_object_new_string_len(const char *s, const int len)
{
char *dstbuf;
struct json_object *jso = json_object_new(json_type_string);
@@ -1175,10 +1151,13 @@ struct json_object* json_object_new_string_len(const char *s, const int len)
return NULL;
jso->_delete = &json_object_string_delete;
jso->_to_json_string = &json_object_string_to_json_string;
- if(len < LEN_DIRECT_STRING_DATA) {
+ if (len < LEN_DIRECT_STRING_DATA)
+ {
dstbuf = jso->o.c_string.str.data;
- } else {
- jso->o.c_string.str.ptr = (char*)malloc(len + 1);
+ }
+ else
+ {
+ jso->o.c_string.str.ptr = (char *)malloc(len + 1);
if (!jso->o.c_string.str.ptr)
{
json_object_generic_delete(jso);
@@ -1193,16 +1172,14 @@ struct json_object* json_object_new_string_len(const char *s, const int len)
return jso;
}
-const char* json_object_get_string(struct json_object *jso)
+const char *json_object_get_string(struct json_object *jso)
{
if (!jso)
return NULL;
- switch(jso->o_type)
+ switch (jso->o_type)
{
- case json_type_string:
- return get_string_component(jso);
- default:
- return json_object_to_json_string(jso);
+ case json_type_string: return get_string_component(jso);
+ default: return json_object_to_json_string(jso);
}
}
@@ -1210,42 +1187,47 @@ int json_object_get_string_len(const struct json_object *jso)
{
if (!jso)
return 0;
- switch(jso->o_type)
+ switch (jso->o_type)
{
- case json_type_string:
- return jso->o.c_string.len;
- default:
- return 0;
+ case json_type_string: return jso->o.c_string.len;
+ default: return 0;
}
}
-int json_object_set_string(json_object* jso, const char* s) {
+int json_object_set_string(json_object *jso, const char *s)
+{
return json_object_set_string_len(jso, s, (int)(strlen(s)));
}
-int json_object_set_string_len(json_object* jso, const char* s, int len){
- char *dstbuf;
- if (jso==NULL || jso->o_type!=json_type_string) return 0;
- if (len<LEN_DIRECT_STRING_DATA) {
- dstbuf=jso->o.c_string.str.data;
- if (jso->o.c_string.len>=LEN_DIRECT_STRING_DATA) free(jso->o.c_string.str.ptr);
- } else {
- dstbuf=(char *)malloc(len+1);
- if (dstbuf==NULL) return 0;
- if (jso->o.c_string.len>=LEN_DIRECT_STRING_DATA) free(jso->o.c_string.str.ptr);
- jso->o.c_string.str.ptr=dstbuf;
+int json_object_set_string_len(json_object *jso, const char *s, int len)
+{
+ char *dstbuf;
+ if (jso == NULL || jso->o_type != json_type_string)
+ return 0;
+ if (len < LEN_DIRECT_STRING_DATA)
+ {
+ dstbuf = jso->o.c_string.str.data;
+ if (jso->o.c_string.len >= LEN_DIRECT_STRING_DATA)
+ free(jso->o.c_string.str.ptr);
+ }
+ else
+ {
+ dstbuf = (char *)malloc(len + 1);
+ if (dstbuf == NULL)
+ return 0;
+ if (jso->o.c_string.len >= LEN_DIRECT_STRING_DATA)
+ free(jso->o.c_string.str.ptr);
+ jso->o.c_string.str.ptr = dstbuf;
}
- jso->o.c_string.len=len;
+ jso->o.c_string.len = len;
memcpy(dstbuf, (const void *)s, len);
dstbuf[len] = '\0';
- return 1;
+ return 1;
}
/* json_object_array */
-static int json_object_array_to_json_string(struct json_object* jso,
- struct printbuf *pb,
- int level,
+static int json_object_array_to_json_string(struct json_object *jso, struct printbuf *pb, int level,
int flags)
{
int had_children = 0;
@@ -1254,7 +1236,7 @@ static int json_object_array_to_json_string(struct json_object* jso,
printbuf_strappend(pb, "[");
if (flags & JSON_C_TO_STRING_PRETTY)
printbuf_strappend(pb, "\n");
- for(ii=0; ii < json_object_array_length(jso); ii++)
+ for (ii = 0; ii < json_object_array_length(jso); ii++)
{
struct json_object *val;
if (had_children)
@@ -1264,40 +1246,39 @@ static int json_object_array_to_json_string(struct json_object* jso,
printbuf_strappend(pb, "\n");
}
had_children = 1;
- if (flags & JSON_C_TO_STRING_SPACED && !(flags&JSON_C_TO_STRING_PRETTY))
+ if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
printbuf_strappend(pb, " ");
indent(pb, level + 1, flags);
val = json_object_array_get_idx(jso, ii);
- if(val == NULL)
+ if (val == NULL)
printbuf_strappend(pb, "null");
- else
- if (val->_to_json_string(val, pb, level+1, flags) < 0)
- return -1;
+ else if (val->_to_json_string(val, pb, level + 1, flags) < 0)
+ return -1;
}
if (flags & JSON_C_TO_STRING_PRETTY)
{
if (had_children)
printbuf_strappend(pb, "\n");
- indent(pb,level,flags);
+ indent(pb, level, flags);
}
- if (flags & JSON_C_TO_STRING_SPACED && !(flags&JSON_C_TO_STRING_PRETTY))
+ if (flags & JSON_C_TO_STRING_SPACED && !(flags & JSON_C_TO_STRING_PRETTY))
return printbuf_strappend(pb, " ]");
return printbuf_strappend(pb, "]");
}
static void json_object_array_entry_free(void *data)
{
- json_object_put((struct json_object*)data);
+ json_object_put((struct json_object *)data);
}
-static void json_object_array_delete(struct json_object* jso)
+static void json_object_array_delete(struct json_object *jso)
{
array_list_free(jso->o.c_array);
json_object_generic_delete(jso);
}
-struct json_object* json_object_new_array(void)
+struct json_object *json_object_new_array(void)
{
struct json_object *jso = json_object_new(json_type_array);
if (!jso)
@@ -1305,44 +1286,40 @@ struct json_object* json_object_new_array(void)
jso->_delete = &json_object_array_delete;
jso->_to_json_string = &json_object_array_to_json_string;
jso->o.c_array = array_list_new(&json_object_array_entry_free);
- if(jso->o.c_array == NULL)
+ if (jso->o.c_array == NULL)
{
- free(jso);
- return NULL;
+ free(jso);
+ return NULL;
}
return jso;
}
-struct array_list* json_object_get_array(const struct json_object *jso)
+struct array_list *json_object_get_array(const struct json_object *jso)
{
if (!jso)
return NULL;
- switch(jso->o_type)
+ switch (jso->o_type)
{
- case json_type_array:
- return jso->o.c_array;
- default:
- return NULL;
+ case json_type_array: return jso->o.c_array;
+ default: return NULL;
}
}
-void json_object_array_sort(struct json_object *jso,
- int(*sort_fn)(const void *, const void *))
+void json_object_array_sort(struct json_object *jso, int (*sort_fn)(const void *, const void *))
{
assert(json_object_get_type(jso) == json_type_array);
array_list_sort(jso->o.c_array, sort_fn);
}
-struct json_object* json_object_array_bsearch(
- const struct json_object *key,
- const struct json_object *jso,
- int (*sort_fn)(const void *, const void *))
+struct json_object *json_object_array_bsearch(const struct json_object *key,
+ const struct json_object *jso,
+ int (*sort_fn)(const void *, const void *))
{
struct json_object **result;
assert(json_object_get_type(jso) == json_type_array);
- result = (struct json_object **)array_list_bsearch(
- (const void **)(void *)&key, jso->o.c_array, sort_fn);
+ result = (struct json_object **)array_list_bsearch((const void **)(void *)&key,
+ jso->o.c_array, sort_fn);
if (!result)
return NULL;
@@ -1355,14 +1332,13 @@ size_t json_object_array_length(const struct json_object *jso)
return array_list_length(jso->o.c_array);
}
-int json_object_array_add(struct json_object *jso,struct json_object *val)
+int json_object_array_add(struct json_object *jso, struct json_object *val)
{
assert(json_object_get_type(jso) == json_type_array);
return array_list_add(jso->o.c_array, val);
}
-int json_object_array_put_idx(struct json_object *jso, size_t idx,
- struct json_object *val)
+int json_object_array_put_idx(struct json_object *jso, size_t idx, struct json_object *val)
{
assert(json_object_get_type(jso) == json_type_array);
return array_list_put_idx(jso->o.c_array, idx, val);
@@ -1374,15 +1350,13 @@ int json_object_array_del_idx(struct json_object *jso, size_t idx, size_t count)
return array_list_del_idx(jso->o.c_array, idx, count);
}
-struct json_object* json_object_array_get_idx(const struct json_object *jso,
- size_t idx)
+struct json_object *json_object_array_get_idx(const struct json_object *jso, size_t idx)
{
assert(json_object_get_type(jso) == json_type_array);
- return (struct json_object*)array_list_get_idx(jso->o.c_array, idx);
+ return (struct json_object *)array_list_get_idx(jso->o.c_array, idx);
}
-static int json_array_equal(struct json_object* jso1,
- struct json_object* jso2)
+static int json_array_equal(struct json_object *jso1, struct json_object *jso2)
{
size_t len, i;
@@ -1390,21 +1364,21 @@ static int json_array_equal(struct json_object* jso1,
if (len != json_object_array_length(jso2))
return 0;
- for (i = 0; i < len; i++) {
+ for (i = 0; i < len; i++)
+ {
if (!json_object_equal(json_object_array_get_idx(jso1, i),
- json_object_array_get_idx(jso2, i)))
+ json_object_array_get_idx(jso2, i)))
return 0;
}
return 1;
}
-struct json_object* json_object_new_null()
+struct json_object *json_object_new_null()
{
- return NULL;
+ return NULL;
}
-static int json_object_all_values_equal(struct json_object* jso1,
- struct json_object* jso2)
+static int json_object_all_values_equal(struct json_object *jso1, struct json_object *jso2)
{
struct json_object_iter iter;
struct json_object *sub;
@@ -1412,25 +1386,25 @@ static int json_object_all_values_equal(struct json_object* jso1,
assert(json_object_get_type(jso1) == json_type_object);
assert(json_object_get_type(jso2) == json_type_object);
/* Iterate over jso1 keys and see if they exist and are equal in jso2 */
- json_object_object_foreachC(jso1, iter) {
- if (!lh_table_lookup_ex(jso2->o.c_object, (void*)iter.key,
- (void**)(void *)&sub))
+ json_object_object_foreachC(jso1, iter)
+ {
+ if (!lh_table_lookup_ex(jso2->o.c_object, (void *)iter.key, (void **)(void *)&sub))
return 0;
if (!json_object_equal(iter.val, sub))
return 0;
- }
+ }
/* Iterate over jso2 keys to see if any exist that are not in jso1 */
- json_object_object_foreachC(jso2, iter) {
- if (!lh_table_lookup_ex(jso1->o.c_object, (void*)iter.key,
- (void**)(void *)&sub))
+ json_object_object_foreachC(jso2, iter)
+ {
+ if (!lh_table_lookup_ex(jso1->o.c_object, (void *)iter.key, (void **)(void *)&sub))
return 0;
- }
+ }
return 1;
}
-int json_object_equal(struct json_object* jso1, struct json_object* jso2)
+int json_object_equal(struct json_object *jso1, struct json_object *jso2)
{
if (jso1 == jso2)
return 1;
@@ -1441,43 +1415,39 @@ int json_object_equal(struct json_object* jso1, struct json_object* jso2)
if (jso1->o_type != jso2->o_type)
return 0;
- switch(jso1->o_type) {
- case json_type_boolean:
- return (jso1->o.c_boolean == jso2->o.c_boolean);
+ switch (jso1->o_type)
+ {
+ case json_type_boolean: return (jso1->o.c_boolean == jso2->o.c_boolean);
- case json_type_double:
- return (jso1->o.c_double == jso2->o.c_double);
+ case json_type_double: return (jso1->o.c_double == jso2->o.c_double);
- case json_type_int:
- if (jso1->o.c_int.cint_type == json_object_int_type_int64)
- {
- if (jso2->o.c_int.cint_type == json_object_int_type_int64)
- return (jso1->o.c_int.cint.c_int64 == jso2->o.c_int.cint.c_int64);
- if (jso1->o.c_int.cint.c_int64 < 0)
- return 0;
- return ((uint64_t)jso1->o.c_int.cint.c_int64 == jso2->o.c_int.cint.c_uint64);
- }
- // else jso1 is a uint64
- if (jso2->o.c_int.cint_type == json_object_int_type_uint64)
- return (jso1->o.c_int.cint.c_uint64 == jso2->o.c_int.cint.c_uint64);
- if (jso2->o.c_int.cint.c_int64 < 0)
+ case json_type_int:
+ if (jso1->o.c_int.cint_type == json_object_int_type_int64)
+ {
+ if (jso2->o.c_int.cint_type == json_object_int_type_int64)
+ return (jso1->o.c_int.cint.c_int64 == jso2->o.c_int.cint.c_int64);
+ if (jso1->o.c_int.cint.c_int64 < 0)
return 0;
- return (jso1->o.c_int.cint.c_uint64 == (uint64_t)jso2->o.c_int.cint.c_int64);
+ return ((uint64_t)jso1->o.c_int.cint.c_int64 ==
+ jso2->o.c_int.cint.c_uint64);
+ }
+ // else jso1 is a uint64
+ if (jso2->o.c_int.cint_type == json_object_int_type_uint64)
+ return (jso1->o.c_int.cint.c_uint64 == jso2->o.c_int.cint.c_uint64);
+ if (jso2->o.c_int.cint.c_int64 < 0)
+ return 0;
+ return (jso1->o.c_int.cint.c_uint64 == (uint64_t)jso2->o.c_int.cint.c_int64);
- case json_type_string:
- return (jso1->o.c_string.len == jso2->o.c_string.len &&
- memcmp(get_string_component(jso1),
- get_string_component(jso2),
- jso1->o.c_string.len) == 0);
+ case json_type_string:
+ return (jso1->o.c_string.len == jso2->o.c_string.len &&
+ memcmp(get_string_component(jso1), get_string_component(jso2),
+ jso1->o.c_string.len) == 0);
- case json_type_object:
- return json_object_all_values_equal(jso1, jso2);
+ case json_type_object: return json_object_all_values_equal(jso1, jso2);
- case json_type_array:
- return json_array_equal(jso1, jso2);
+ case json_type_array: return json_array_equal(jso1, jso2);
- case json_type_null:
- return 1;
+ case json_type_null: return 1;
};
return 0;
@@ -1496,14 +1466,15 @@ static int json_object_copy_serializer_data(struct json_object *src, struct json
// else if ... other supported serializers ...
else
{
- _json_c_set_last_err("json_object_deep_copy: unable to copy unknown serializer data: %p\n", dst->_to_json_string);
+ _json_c_set_last_err(
+ "json_object_deep_copy: unable to copy unknown serializer data: %p\n",
+ dst->_to_json_string);
return -1;
}
dst->_user_delete = src->_user_delete;
return 0;
}
-
/**
* The default shallow copy implementation. Simply creates a new object of the same
* type but does *not* copy over _userdata nor retain any custom serializer.
@@ -1512,48 +1483,39 @@ static int json_object_copy_serializer_data(struct json_object *src, struct json
*
* This always returns -1 or 1. It will never return 2 since it does not copy the serializer.
*/
-int json_c_shallow_copy_default(json_object *src, json_object *parent, const char *key, size_t index, json_object **dst)
+int json_c_shallow_copy_default(json_object *src, json_object *parent, const char *key,
+ size_t index, json_object **dst)
{
- switch (src->o_type) {
- case json_type_boolean:
- *dst = json_object_new_boolean(src->o.c_boolean);
- break;
+ switch (src->o_type)
+ {
+ case json_type_boolean: *dst = json_object_new_boolean(src->o.c_boolean); break;
- case json_type_double:
- *dst = json_object_new_double(src->o.c_double);
- break;
+ case json_type_double: *dst = json_object_new_double(src->o.c_double); break;
case json_type_int:
- switch(src->o.c_int.cint_type) {
+ switch (src->o.c_int.cint_type)
+ {
case json_object_int_type_int64:
*dst = json_object_new_int64(src->o.c_int.cint.c_int64);
break;
case json_object_int_type_uint64:
*dst = json_object_new_uint64(src->o.c_int.cint.c_uint64);
break;
- default:
- assert(!"invalid cint_type");
+ default: assert(!"invalid cint_type");
}
break;
- case json_type_string:
- *dst = json_object_new_string(get_string_component(src));
- break;
+ case json_type_string: *dst = json_object_new_string(get_string_component(src)); break;
- case json_type_object:
- *dst = json_object_new_object();
- break;
+ case json_type_object: *dst = json_object_new_object(); break;
- case json_type_array:
- *dst = json_object_new_array();
- break;
+ case json_type_array: *dst = json_object_new_array(); break;
- default:
- errno = EINVAL;
- return -1;
+ default: errno = EINVAL; return -1;
}
- if (!*dst) {
+ if (!*dst)
+ {
errno = ENOMEM;
return -1;
}
@@ -1568,7 +1530,10 @@ int json_c_shallow_copy_default(json_object *src, json_object *parent, const cha
*
* Note: caller is responsible for freeing *dst if this fails and returns -1.
*/
-static int json_object_deep_copy_recursive(struct json_object *src, struct json_object *parent, const char *key_in_parent, size_t index_in_parent, struct json_object **dst, json_c_shallow_copy_fn *shallow_copy)
+static int json_object_deep_copy_recursive(struct json_object *src, struct json_object *parent,
+ const char *key_in_parent, size_t index_in_parent,
+ struct json_object **dst,
+ json_c_shallow_copy_fn *shallow_copy)
{
struct json_object_iter iter;
size_t src_array_len, ii;
@@ -1583,14 +1548,17 @@ static int json_object_deep_copy_recursive(struct json_object *src, struct json_
}
assert(*dst != NULL);
- switch (src->o_type) {
+ switch (src->o_type)
+ {
case json_type_object:
- json_object_object_foreachC(src, iter) {
+ json_object_object_foreachC(src, iter)
+ {
struct json_object *jso = NULL;
/* This handles the `json_type_null` case */
if (!iter.val)
jso = NULL;
- else if (json_object_deep_copy_recursive(iter.val, src, iter.key, -1, &jso, shallow_copy) < 0)
+ else if (json_object_deep_copy_recursive(iter.val, src, iter.key, -1, &jso,
+ shallow_copy) < 0)
{
json_object_put(jso);
return -1;
@@ -1606,13 +1574,15 @@ static int json_object_deep_copy_recursive(struct json_object *src, struct json_
case json_type_array:
src_array_len = json_object_array_length(src);
- for (ii = 0; ii < src_array_len; ii++) {
+ for (ii = 0; ii < src_array_len; ii++)
+ {
struct json_object *jso = NULL;
struct json_object *jso1 = json_object_array_get_idx(src, ii);
/* This handles the `json_type_null` case */
if (!jso1)
jso = NULL;
- else if (json_object_deep_copy_recursive(jso1, src, NULL, ii, &jso, shallow_copy) < 0)
+ else if (json_object_deep_copy_recursive(jso1, src, NULL, ii, &jso,
+ shallow_copy) < 0)
{
json_object_put(jso);
return -1;
@@ -1637,12 +1607,14 @@ static int json_object_deep_copy_recursive(struct json_object *src, struct json_
return 0;
}
-int json_object_deep_copy(struct json_object *src, struct json_object **dst, json_c_shallow_copy_fn *shallow_copy)
+int json_object_deep_copy(struct json_object *src, struct json_object **dst,
+ json_c_shallow_copy_fn *shallow_copy)
{
int rc;
/* Check if arguments are sane ; *dst must not point to a non-NULL object */
- if (!src || !dst || *dst) {
+ if (!src || !dst || *dst)
+ {
errno = EINVAL;
return -1;
}
@@ -1651,11 +1623,11 @@ int json_object_deep_copy(struct json_object *src, struct json_object **dst, jso
shallow_copy = json_c_shallow_copy_default;
rc = json_object_deep_copy_recursive(src, NULL, NULL, -1, dst, shallow_copy);
- if (rc < 0) {
+ if (rc < 0)
+ {
json_object_put(*dst);
*dst = NULL;
}
return rc;
}
-