diff options
author | Eric Haszlakiewicz <erh+git@nimenees.com> | 2012-04-28 13:26:09 -0500 |
---|---|---|
committer | Eric Haszlakiewicz <erh+git@nimenees.com> | 2012-04-28 13:26:09 -0500 |
commit | 3fcffe1bb01de6978f4516abd49a3b3c9e2cd2f2 (patch) | |
tree | 732aac674963768bf2aa9e3abc3cf5b7f1937758 /json_object.c | |
parent | f931f61851c949f8ab943dee8935b0d775346178 (diff) |
Add a json_object_to_json_string_ext() function to allow the formatting of output to be selected.
There are now three options: JSON_C_TO_STRING_SPACED, JSON_C_TO_STRING_PLAIN and JSON_C_TO_STRING_PRETTY.
This also add a json_object_to_file_ext() that takes the same flags.
Existing output of json_object_to_json_string() is unchanged, and uses JSON_C_TO_STRING_SPACED.
Thanks fo Grant Edwards for the initial patches.
Diffstat (limited to 'json_object.c')
-rw-r--r-- | json_object.c | 164 |
1 files changed, 120 insertions, 44 deletions
diff --git a/json_object.c b/json_object.c index 84af414..825630f 100644 --- a/json_object.c +++ b/json_object.c @@ -180,45 +180,88 @@ enum json_type json_object_get_type(struct json_object *jso) return jso->o_type; } -/* json_object_to_json_string */ +/* extended conversion to string */ + +const char* json_object_to_json_string_ext(struct json_object *jso, int flags) +{ + if (!jso) + return "null"; + + if ((!jso->_pb) && !(jso->_pb = printbuf_new())) + return NULL; + + printbuf_reset(jso->_pb); + + if(jso->_to_json_string(jso, jso->_pb, 0, flags) < 0) + return NULL; + + return jso->_pb->buf; +} + +/* backwards-compatible conversion to string */ const char* json_object_to_json_string(struct json_object *jso) { - if(!jso) return "null"; - if(!jso->_pb) { - if(!(jso->_pb = printbuf_new())) return NULL; - } else { - printbuf_reset(jso->_pb); - } - if(jso->_to_json_string(jso, jso->_pb) < 0) return NULL; - return jso->_pb->buf; + return json_object_to_json_string_ext(jso, JSON_C_TO_STRING_SPACED); } +static void indent(struct printbuf *pb, int level, int flags) +{ + if (flags & JSON_C_TO_STRING_PRETTY) + { + printbuf_memset(pb, -1, ' ', level * 2); + } +} /* json_object_object */ static int json_object_object_to_json_string(struct json_object* jso, - struct printbuf *pb) -{ - int i=0; - struct json_object_iter iter; - sprintbuf(pb, "{"); - - /* CAW: scope operator to make ANSI correctness */ - /* CAW: switched to json_object_object_foreachC which uses an iterator struct */ - json_object_object_foreachC(jso, iter) { - if(i) sprintbuf(pb, ","); - sprintbuf(pb, " \""); - json_escape_str(pb, iter.key, strlen(iter.key)); + struct printbuf *pb, + int level, + int flags) +{ + int had_children = 0; + struct json_object_iter iter; + + sprintbuf(pb, "{" /*}*/); + if (flags & JSON_C_TO_STRING_PRETTY) + sprintbuf(pb, "\n"); + json_object_object_foreachC(jso, iter) + { + if (had_children) + { + sprintbuf(pb, ","); + if (flags & JSON_C_TO_STRING_PRETTY) + sprintbuf(pb, "\n"); + } + had_children = 1; + if (flags & JSON_C_TO_STRING_SPACED) + sprintbuf(pb, " "); + indent(pb, level+1, flags); + sprintbuf(pb, "\""); + json_escape_str(pb, iter.key, strlen(iter.key)); + if (flags & JSON_C_TO_STRING_SPACED) sprintbuf(pb, "\": "); - if(iter.val == NULL) sprintbuf(pb, "null"); - else iter.val->_to_json_string(iter.val, pb); - i++; + else + sprintbuf(pb, "\":"); + if(iter.val == NULL) + sprintbuf(pb, "null"); + else + iter.val->_to_json_string(iter.val, pb, level+1,flags); } - - return sprintbuf(pb, " }"); + if (flags & JSON_C_TO_STRING_PRETTY) + { + if (had_children) + sprintbuf(pb, "\n"); + indent(pb,level,flags); + } + if (flags & JSON_C_TO_STRING_SPACED) + return sprintbuf(pb, /*{*/ " }"); + else + return sprintbuf(pb, /*{*/ "}"); } + static void json_object_lh_entry_free(struct lh_entry *ent) { free(ent->k); @@ -291,7 +334,9 @@ void json_object_object_del(struct json_object* jso, const char *key) /* json_object_boolean */ static int json_object_boolean_to_json_string(struct json_object* jso, - struct printbuf *pb) + struct printbuf *pb, + int level, + int flags) { if(jso->o.c_boolean) return sprintbuf(pb, "true"); else return sprintbuf(pb, "false"); @@ -327,7 +372,9 @@ json_bool json_object_get_boolean(struct json_object *jso) /* json_object_int */ static int json_object_int_to_json_string(struct json_object* jso, - struct printbuf *pb) + struct printbuf *pb, + int level, + int flags) { return sprintbuf(pb, "%"PRId64, jso->o.c_int64); } @@ -412,7 +459,9 @@ int64_t json_object_get_int64(struct json_object *jso) /* json_object_double */ static int json_object_double_to_json_string(struct json_object* jso, - struct printbuf *pb) + struct printbuf *pb, + int level, + int flags) { return sprintbuf(pb, "%lf", jso->o.c_double); } @@ -449,7 +498,9 @@ double json_object_get_double(struct json_object *jso) /* json_object_string */ static int json_object_string_to_json_string(struct json_object* jso, - struct printbuf *pb) + struct printbuf *pb, + int level, + int flags) { sprintbuf(pb, "\""); json_escape_str(pb, jso->o.c_string.str, jso->o.c_string.len); @@ -511,20 +562,45 @@ int json_object_get_string_len(struct json_object *jso) { /* json_object_array */ static int json_object_array_to_json_string(struct json_object* jso, - struct printbuf *pb) -{ - int i; - sprintbuf(pb, "["); - for(i=0; i < json_object_array_length(jso); i++) { - struct json_object *val; - if(i) { sprintbuf(pb, ", "); } - else { sprintbuf(pb, " "); } - - val = json_object_array_get_idx(jso, i); - if(val == NULL) { sprintbuf(pb, "null"); } - else { val->_to_json_string(val, pb); } - } - return sprintbuf(pb, " ]"); + struct printbuf *pb, + int level, + int flags) +{ + int had_children = 0; + int ii; + sprintbuf(pb, "["); + if (flags & JSON_C_TO_STRING_PRETTY) + sprintbuf(pb, "\n"); + for(ii=0; ii < json_object_array_length(jso); ii++) + { + struct json_object *val; + if (had_children) + { + sprintbuf(pb, ","); + if (flags & JSON_C_TO_STRING_PRETTY) + sprintbuf(pb, "\n"); + } + had_children = 1; + if (flags & JSON_C_TO_STRING_SPACED) + sprintbuf(pb, " "); + indent(pb, level + 1, flags); + val = json_object_array_get_idx(jso, ii); + if(val == NULL) + sprintbuf(pb, "null"); + else + val->_to_json_string(val, pb, level+1, flags); + } + if (flags & JSON_C_TO_STRING_PRETTY) + { + if (had_children) + sprintbuf(pb, "\n"); + indent(pb,level,flags); + } + + if (flags & JSON_C_TO_STRING_SPACED) + return sprintbuf(pb, " ]"); + else + return sprintbuf(pb, "]"); } static void json_object_array_entry_free(void *data) |