Reformat some code in json_object.c

This commit is contained in:
Eric Haszlakiewicz
2014-04-19 20:23:54 -04:00
committed by Eric Haszlakiewicz
parent 795e9151a1
commit 92a7740e90

View File

@@ -73,13 +73,17 @@ static void json_object_init(void) {
} }
static void json_object_fini(void) __attribute__ ((destructor)); static void json_object_fini(void) __attribute__ ((destructor));
static void json_object_fini(void) { static void json_object_fini(void)
{
struct lh_entry *ent; struct lh_entry *ent;
if(MC_GET_DEBUG()) { if (MC_GET_DEBUG())
if (json_object_table->count) { {
if (json_object_table->count)
{
MC_DEBUG("json_object_fini: %d referenced objects at exit\n", 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) { lh_foreach(json_object_table, ent)
{
struct json_object* obj = (struct json_object*)ent->v; struct json_object* obj = (struct json_object*)ent->v;
MC_DEBUG("\t%s:%p\n", json_type_to_name(obj->o_type), obj); MC_DEBUG("\t%s:%p\n", json_type_to_name(obj->o_type), obj);
} }
@@ -97,9 +101,11 @@ static int json_escape_str(struct printbuf *pb, char *str, int len)
{ {
int pos = 0, start_offset = 0; int pos = 0, start_offset = 0;
unsigned char c; unsigned char c;
while (len--) { while (len--)
{
c = str[pos]; c = str[pos];
switch(c) { switch(c)
{
case '\b': case '\b':
case '\n': case '\n':
case '\r': case '\r':
@@ -110,6 +116,7 @@ static int json_escape_str(struct printbuf *pb, char *str, int len)
case '/': case '/':
if(pos - start_offset > 0) if(pos - start_offset > 0)
printbuf_memappend(pb, str + start_offset, pos - start_offset); printbuf_memappend(pb, str + start_offset, pos - start_offset);
if(c == '\b') printbuf_memappend(pb, "\\b", 2); if(c == '\b') printbuf_memappend(pb, "\\b", 2);
else if(c == '\n') printbuf_memappend(pb, "\\n", 2); else if(c == '\n') printbuf_memappend(pb, "\\n", 2);
else if(c == '\r') printbuf_memappend(pb, "\\r", 2); else if(c == '\r') printbuf_memappend(pb, "\\r", 2);
@@ -118,17 +125,20 @@ static int json_escape_str(struct printbuf *pb, char *str, int len)
else if(c == '"') printbuf_memappend(pb, "\\\"", 2); else if(c == '"') printbuf_memappend(pb, "\\\"", 2);
else if(c == '\\') printbuf_memappend(pb, "\\\\", 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; start_offset = ++pos;
break; break;
default: default:
if(c < ' ') { if(c < ' ')
{
if(pos - start_offset > 0) if(pos - start_offset > 0)
printbuf_memappend(pb, str + start_offset, pos - start_offset); printbuf_memappend(pb, str + start_offset, pos - start_offset);
sprintbuf(pb, "\\u00%c%c", sprintbuf(pb, "\\u00%c%c",
json_hex_chars[c >> 4], json_hex_chars[c >> 4],
json_hex_chars[c & 0xf]); json_hex_chars[c & 0xf]);
start_offset = ++pos; start_offset = ++pos;
} else pos++; } else
pos++;
} }
} }
if (pos - start_offset > 0) if (pos - start_offset > 0)
@@ -141,9 +151,8 @@ static int json_escape_str(struct printbuf *pb, char *str, int len)
extern struct json_object* json_object_get(struct json_object *jso) extern struct json_object* json_object_get(struct json_object *jso)
{ {
if(jso) { if (jso)
jso->_ref_count++; jso->_ref_count++;
}
return jso; return jso;
} }
@@ -182,7 +191,8 @@ static struct json_object* json_object_new(enum json_type o_type)
struct json_object *jso; struct json_object *jso;
jso = (struct json_object*)calloc(sizeof(struct json_object), 1); jso = (struct json_object*)calloc(sizeof(struct json_object), 1);
if(!jso) return NULL; if (!jso)
return NULL;
jso->o_type = o_type; jso->o_type = o_type;
jso->_ref_count = 1; jso->_ref_count = 1;
jso->_delete = &json_object_generic_delete; jso->_delete = &json_object_generic_delete;
@@ -358,7 +368,8 @@ static void json_object_object_delete(struct json_object* 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); struct json_object *jso = json_object_new(json_type_object);
if(!jso) return NULL; if (!jso)
return NULL;
jso->_delete = &json_object_object_delete; jso->_delete = &json_object_object_delete;
jso->_to_json_string = &json_object_object_to_json_string; jso->_to_json_string = &json_object_object_to_json_string;
jso->o.c_object = lh_kchar_table_new(JSON_OBJECT_DEF_HASH_ENTRIES, jso->o.c_object = lh_kchar_table_new(JSON_OBJECT_DEF_HASH_ENTRIES,
@@ -368,8 +379,10 @@ struct json_object* json_object_new_object(void)
struct lh_table* json_object_get_object(struct json_object *jso) struct lh_table* json_object_get_object(struct json_object *jso)
{ {
if(!jso) return NULL; if (!jso)
switch(jso->o_type) { return NULL;
switch(jso->o_type)
{
case json_type_object: case json_type_object:
return jso->o.c_object; return jso->o.c_object;
default: default:
@@ -440,14 +453,17 @@ static int json_object_boolean_to_json_string(struct json_object* jso,
int level, int level,
int flags) int flags)
{ {
if(jso->o.c_boolean) return sprintbuf(pb, "true"); if (jso->o.c_boolean)
else return sprintbuf(pb, "false"); return sprintbuf(pb, "true");
else
return sprintbuf(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); struct json_object *jso = json_object_new(json_type_boolean);
if(!jso) return NULL; if (!jso)
return NULL;
jso->_to_json_string = &json_object_boolean_to_json_string; jso->_to_json_string = &json_object_boolean_to_json_string;
jso->o.c_boolean = b; jso->o.c_boolean = b;
return jso; return jso;
@@ -455,8 +471,10 @@ struct json_object* json_object_new_boolean(json_bool b)
json_bool json_object_get_boolean(struct json_object *jso) json_bool json_object_get_boolean(struct json_object *jso)
{ {
if(!jso) return FALSE; if (!jso)
switch(jso->o_type) { return FALSE;
switch(jso->o_type)
{
case json_type_boolean: case json_type_boolean:
return jso->o.c_boolean; return jso->o.c_boolean;
case json_type_int: case json_type_int:
@@ -484,7 +502,8 @@ static int json_object_int_to_json_string(struct json_object* jso,
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); struct json_object *jso = json_object_new(json_type_int);
if(!jso) return NULL; if (!jso)
return NULL;
jso->_to_json_string = &json_object_int_to_json_string; jso->_to_json_string = &json_object_int_to_json_string;
jso->o.c_int64 = i; jso->o.c_int64 = i;
return jso; return jso;
@@ -532,7 +551,8 @@ int32_t json_object_get_int(struct json_object *jso)
struct json_object* json_object_new_int64(int64_t i) struct json_object* json_object_new_int64(int64_t i)
{ {
struct json_object *jso = json_object_new(json_type_int); struct json_object *jso = json_object_new(json_type_int);
if(!jso) return NULL; if (!jso)
return NULL;
jso->_to_json_string = &json_object_int_to_json_string; jso->_to_json_string = &json_object_int_to_json_string;
jso->o.c_int64 = i; jso->o.c_int64 = i;
return jso; return jso;
@@ -542,8 +562,10 @@ int64_t json_object_get_int64(struct json_object *jso)
{ {
int64_t cint; int64_t cint;
if(!jso) return 0; if (!jso)
switch(jso->o_type) { return 0;
switch(jso->o_type)
{
case json_type_int: case json_type_int:
return jso->o.c_int64; return jso->o.c_int64;
case json_type_double: case json_type_double:
@@ -551,7 +573,8 @@ int64_t json_object_get_int64(struct json_object *jso)
case json_type_boolean: case json_type_boolean:
return jso->o.c_boolean; return jso->o.c_boolean;
case json_type_string: case json_type_string:
if (json_parse_int64(jso->o.c_string.str, &cint) == 0) return cint; if (json_parse_int64(jso->o.c_string.str, &cint) == 0)
return cint;
default: default:
return 0; return 0;
} }
@@ -707,7 +730,8 @@ static void json_object_string_delete(struct json_object* 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); struct json_object *jso = json_object_new(json_type_string);
if(!jso) return NULL; if (!jso)
return NULL;
jso->_delete = &json_object_string_delete; jso->_delete = &json_object_string_delete;
jso->_to_json_string = &json_object_string_to_json_string; jso->_to_json_string = &json_object_string_to_json_string;
jso->o.c_string.str = strdup(s); jso->o.c_string.str = strdup(s);
@@ -718,7 +742,8 @@ struct json_object* json_object_new_string(const char *s)
struct json_object* json_object_new_string_len(const char *s, int len) struct json_object* json_object_new_string_len(const char *s, int len)
{ {
struct json_object *jso = json_object_new(json_type_string); struct json_object *jso = json_object_new(json_type_string);
if(!jso) return NULL; if (!jso)
return NULL;
jso->_delete = &json_object_string_delete; jso->_delete = &json_object_string_delete;
jso->_to_json_string = &json_object_string_to_json_string; jso->_to_json_string = &json_object_string_to_json_string;
jso->o.c_string.str = (char*)malloc(len + 1); jso->o.c_string.str = (char*)malloc(len + 1);
@@ -730,8 +755,10 @@ struct json_object* json_object_new_string_len(const char *s, int len)
const char* json_object_get_string(struct json_object *jso) const char* json_object_get_string(struct json_object *jso)
{ {
if(!jso) return NULL; if (!jso)
switch(jso->o_type) { return NULL;
switch(jso->o_type)
{
case json_type_string: case json_type_string:
return jso->o.c_string.str; return jso->o.c_string.str;
default: default:
@@ -739,9 +766,12 @@ const char* json_object_get_string(struct json_object *jso)
} }
} }
int json_object_get_string_len(struct json_object *jso) { int json_object_get_string_len(struct json_object *jso)
if(!jso) return 0; {
switch(jso->o_type) { if (!jso)
return 0;
switch(jso->o_type)
{
case json_type_string: case json_type_string:
return jso->o.c_string.len; return jso->o.c_string.len;
default: default:
@@ -808,7 +838,8 @@ static void json_object_array_delete(struct json_object* 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); struct json_object *jso = json_object_new(json_type_array);
if(!jso) return NULL; if (!jso)
return NULL;
jso->_delete = &json_object_array_delete; jso->_delete = &json_object_array_delete;
jso->_to_json_string = &json_object_array_to_json_string; jso->_to_json_string = &json_object_array_to_json_string;
jso->o.c_array = array_list_new(&json_object_array_entry_free); jso->o.c_array = array_list_new(&json_object_array_entry_free);
@@ -817,8 +848,10 @@ struct json_object* json_object_new_array(void)
struct array_list* json_object_get_array(struct json_object *jso) struct array_list* json_object_get_array(struct json_object *jso)
{ {
if(!jso) return NULL; if (!jso)
switch(jso->o_type) { return NULL;
switch(jso->o_type)
{
case json_type_array: case json_type_array:
return jso->o.c_array; return jso->o.c_array;
default: default: