From 74871d3beeb606cc0b54f09d92c06d555030fc45 Mon Sep 17 00:00:00 2001 From: CHENQ Date: Fri, 2 Dec 2016 09:41:49 +0800 Subject: [PATCH] add json_object_duplicate --- json_util.c | 13 +++++++++++++ json_util.h | 7 +++++++ 2 files changed, 20 insertions(+) diff --git a/json_util.c b/json_util.c index b66c3dc..a18a451 100644 --- a/json_util.c +++ b/json_util.c @@ -324,3 +324,16 @@ const char *json_type_to_name(enum json_type o_type) return json_type_name[o_type]; } +struct json_object* json_object_duplicate(struct json_object* pObj) +{ + /*we could also duplicate by iterate json_object; + but this code is easy to read and write. + */ + const char* pTxt = json_object_to_json_string(pObj); + if (pTxt) + { + json_object* newObject = json_tokener_parse(pTxt); + return newObject; + } + return NULL; +} \ No newline at end of file diff --git a/json_util.h b/json_util.h index a913278..92153e0 100644 --- a/json_util.h +++ b/json_util.h @@ -82,6 +82,13 @@ extern int json_parse_double(const char *buf, double *retval); */ extern const char *json_type_to_name(enum json_type o_type); +/** duplilcate a json_object. +* Not thread safe, because of obj need to convert to string first +* @param obj the json_object instance +* @returns a new json object, remember when you newjson object should release by yourself +*/ +extern struct json_object* json_object_duplicate(const struct json_object* obj); + #ifdef __cplusplus } #endif