12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493 |
- #include "config.h"
- #include "strerror_override.h"
- #include <assert.h>
- #include <ctype.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 "json_inttypes.h"
- #include "json_object.h"
- #include "json_object_private.h"
- #include "json_util.h"
- #include "math_compat.h"
- #include "strdup_compat.h"
- #include "snprintf_compat.h"
- #if SIZEOF_LONG_LONG != SIZEOF_INT64_T
- #error "The long long type isn't 64-bits"
- #endif
- 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 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;
- static json_object_to_json_string_fn json_object_double_to_json_string_default;
- static json_object_to_json_string_fn json_object_int_to_json_string;
- 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;
- #ifdef REFCOUNT_DEBUG
- static struct lh_table *json_object_table;
- 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)
- {
- struct lh_entry *ent;
- if (MC_GET_DEBUG())
- {
- if (json_object_table->count)
- {
- MC_DEBUG("json_object_fini: %d referenced objects at exit\n",
- 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);
- }
- }
- }
- MC_DEBUG("json_object_fini: freeing object table\n");
- lh_table_free(json_object_table);
- }
- #endif
- 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;
- }
- static int json_escape_str(struct printbuf *pb, const char *str, int len, int flags)
- {
- int pos = 0, start_offset = 0;
- unsigned char c;
- while (len--)
- {
- c = str[pos];
- switch(c)
- {
- case '\b':
- case '\n':
- case '\r':
- case '\t':
- case '\f':
- case '"':
- case '\\':
- case '/':
- if((flags & JSON_C_TO_STRING_NOSLASHESCAPE) && c == '/')
- {
- pos++;
- break;
- }
- 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);
- start_offset = ++pos;
- break;
- default:
- 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);
- start_offset = ++pos;
- } else
- pos++;
- }
- }
- if (pos - start_offset > 0)
- printbuf_memappend(pb, str + start_offset, pos - start_offset);
- return 0;
- }
- extern struct json_object* json_object_get(struct json_object *jso)
- {
- if (!jso) return jso;
- #if defined(HAVE_ATOMIC_BUILTINS) && defined(ENABLE_THREADING)
- __sync_add_and_fetch(&jso->_ref_count, 1);
- #else
- ++jso->_ref_count;
- #endif
- return jso;
- }
- int json_object_put(struct json_object *jso)
- {
- if(!jso) return 0;
-
- assert(jso->_ref_count > 0);
- #if defined(HAVE_ATOMIC_BUILTINS) && defined(ENABLE_THREADING)
-
- if (__sync_sub_and_fetch(&jso->_ref_count, 1) > 0) return 0;
- #else
- if (--jso->_ref_count > 0) return 0;
- #endif
- if (jso->_user_delete)
- jso->_user_delete(jso, jso->_userdata);
- jso->_delete(jso);
- return 1;
- }
- 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);
- lh_table_delete(json_object_table, jso);
- #endif
- printbuf_free(jso->_pb);
- free(jso);
- }
- static struct json_object* json_object_new(enum json_type o_type)
- {
- struct json_object *jso;
- jso = (struct json_object*)calloc(sizeof(struct json_object), 1);
- if (!jso)
- return NULL;
- jso->o_type = o_type;
- jso->_ref_count = 1;
- jso->_delete = &json_object_generic_delete;
- #ifdef REFCOUNT_DEBUG
- lh_table_insert(json_object_table, jso, jso);
- MC_DEBUG("json_object_new_%s: %p\n", json_type_to_name(jso->o_type), jso);
- #endif
- return jso;
- }
- int json_object_is_type(const struct json_object *jso, enum json_type type)
- {
- if (!jso)
- return (type == json_type_null);
- return (jso->o_type == type);
- }
- enum json_type json_object_get_type(const struct json_object *jso)
- {
- if (!jso)
- return json_type_null;
- return jso->o_type;
- }
- 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)
- {
-
- assert(jso != NULL);
-
- if (jso->_user_delete)
- jso->_user_delete(jso, jso->_userdata);
- jso->_userdata = userdata;
- jso->_user_delete = 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)
- {
-
- switch(jso->o_type)
- {
- 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_object:
- jso->_to_json_string = &json_object_object_to_json_string;
- break;
- case json_type_array:
- jso->_to_json_string = &json_object_array_to_json_string;
- break;
- case json_type_string:
- jso->_to_json_string = &json_object_string_to_json_string;
- break;
- }
- return;
- }
- jso->_to_json_string = to_string_func;
- }
- 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;
- if (!jso)
- {
- s = 4;
- r = "null";
- }
- else if ((jso->_pb) || (jso->_pb = printbuf_new()))
- {
- printbuf_reset(jso->_pb);
- if(jso->_to_json_string(jso, jso->_pb, 0, flags) >= 0)
- {
- s = (size_t)jso->_pb->bpos;
- r = jso->_pb->buf;
- }
- }
- if (length)
- *length = s;
- return r;
- }
- const char* json_object_to_json_string_ext(struct json_object *jso, int flags)
- {
- return json_object_to_json_string_length(jso, flags, NULL);
- }
- const char* json_object_to_json_string(struct json_object *jso)
- {
- 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)
- {
- if (flags & JSON_C_TO_STRING_PRETTY_TAB)
- {
- printbuf_memset(pb, -1, '\t', level);
- }
- else
- {
- printbuf_memset(pb, -1, ' ', level * 2);
- }
- }
- }
- 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;
- printbuf_strappend(pb, "{" );
- if (flags & JSON_C_TO_STRING_PRETTY)
- printbuf_strappend(pb, "\n");
- json_object_object_foreachC(jso, iter)
- {
- if (had_children)
- {
- printbuf_strappend(pb, ",");
- if (flags & JSON_C_TO_STRING_PRETTY)
- printbuf_strappend(pb, "\n");
- }
- had_children = 1;
- if (flags & JSON_C_TO_STRING_SPACED)
- printbuf_strappend(pb, " ");
- 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)
- printbuf_strappend(pb, "null");
- 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);
- }
- if (flags & JSON_C_TO_STRING_SPACED)
- return printbuf_strappend(pb, " }");
- else
- 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));
- }
- 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 *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);
- if (!jso->o.c_object)
- {
- json_object_generic_delete(jso);
- errno = ENOMEM;
- return NULL;
- }
- return jso;
- }
- struct lh_table* json_object_get_object(const struct json_object *jso)
- {
- if (!jso)
- return NULL;
- switch(jso->o_type)
- {
- 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)
- {
- struct json_object *existing_value = NULL;
- struct lh_entry *existing_entry;
- unsigned long hash;
- assert(json_object_get_type(jso) == json_type_object);
-
-
- 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);
-
-
- if (jso == val)
- return -1;
- if (!existing_entry)
- {
- 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);
- 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)
- {
- 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);
- return lh_table_length(jso->o.c_object);
- }
- 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 *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)
- {
- if (value != NULL)
- *value = NULL;
- if (NULL == jso)
- return FALSE;
- switch(jso->o_type)
- {
- case json_type_object:
- return lh_table_lookup_ex(jso->o.c_object, (const void *) key,
- (void**) value);
- default:
- if (value != NULL)
- *value = NULL;
- return FALSE;
- }
- }
- 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);
- }
- 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 *jso = json_object_new(json_type_boolean);
- if (!jso)
- return NULL;
- jso->_to_json_string = &json_object_boolean_to_json_string;
- jso->o.c_boolean = b;
- return jso;
- }
- json_bool json_object_get_boolean(const struct json_object *jso)
- {
- if (!jso)
- return FALSE;
- switch(jso->o_type)
- {
- case json_type_boolean:
- return jso->o.c_boolean;
- case json_type_int:
- return (jso->o.c_int64 != 0);
- case json_type_double:
- return (jso->o.c_double != 0);
- case json_type_string:
- return (jso->o.c_string.len != 0);
- default:
- return FALSE;
- }
- }
- 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;
- return 1;
- }
- static int json_object_int_to_json_string(struct json_object* jso,
- struct printbuf *pb,
- int level,
- int flags)
- {
-
- char sbuf[21];
- snprintf(sbuf, sizeof(sbuf), "%" PRId64, jso->o.c_int64);
- return printbuf_memappend (pb, sbuf, strlen(sbuf));
- }
- struct json_object* json_object_new_int(int32_t i)
- {
- struct json_object *jso = json_object_new(json_type_int);
- if (!jso)
- return NULL;
- jso->_to_json_string = &json_object_int_to_json_string;
- jso->o.c_int64 = i;
- return jso;
- }
- int32_t json_object_get_int(const struct json_object *jso)
- {
- int64_t cint64;
- enum json_type o_type;
- if(!jso) return 0;
- o_type = jso->o_type;
- cint64 = jso->o.c_int64;
- if (o_type == json_type_string)
- {
-
- if (json_parse_int64(get_string_component(jso), &cint64) != 0)
- return 0;
- o_type = json_type_int;
- }
- switch(o_type) {
- case json_type_int:
-
- 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){
- if (!jso || jso->o_type!=json_type_int)
- return 0;
- jso->o.c_int64=new_value;
- return 1;
- }
- struct json_object* json_object_new_int64(int64_t i)
- {
- struct json_object *jso = json_object_new(json_type_int);
- if (!jso)
- return NULL;
- jso->_to_json_string = &json_object_int_to_json_string;
- jso->o.c_int64 = i;
- return jso;
- }
- int64_t json_object_get_int64(const struct json_object *jso)
- {
- int64_t cint;
- if (!jso)
- return 0;
- switch(jso->o_type)
- {
- case json_type_int:
- return jso->o.c_int64;
- case json_type_double:
- if (jso->o.c_double >= INT64_MAX)
- return INT64_MAX;
- 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_string:
- if (json_parse_int64(get_string_component(jso), &cint) == 0)
- return cint;
-
- default:
- return 0;
- }
- }
- 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_int64=new_value;
- return 1;
- }
- int json_object_int_inc(struct json_object *jso, int64_t val) {
- if (!jso || jso->o_type != json_type_int)
- return 0;
- if (val > 0 && jso->o.c_int64 > INT64_MAX - val) {
- jso->o.c_int64 = INT64_MAX;
- } else if (val < 0 && jso->o.c_int64 < INT64_MIN - val) {
- jso->o.c_int64 = INT64_MIN;
- } else {
- jso->o.c_int64 += val;
- }
- return 1;
- }
- #if defined(HAVE___THREAD)
- static SPEC___THREAD char *tls_serialization_float_format = NULL;
- #endif
- static char *global_serialization_float_format = NULL;
- int json_c_set_serialization_double_format(const char *double_format, int global_or_thread)
- {
- if (global_or_thread == JSON_C_OPTION_GLOBAL)
- {
- #if defined(HAVE___THREAD)
- if (tls_serialization_float_format)
- {
- free(tls_serialization_float_format);
- tls_serialization_float_format = NULL;
- }
- #endif
- if (global_serialization_float_format)
- free(global_serialization_float_format);
- global_serialization_float_format = double_format ? strdup(double_format) : NULL;
- }
- else if (global_or_thread == JSON_C_OPTION_THREAD)
- {
- #if defined(HAVE___THREAD)
- if (tls_serialization_float_format)
- {
- free(tls_serialization_float_format);
- tls_serialization_float_format = NULL;
- }
- tls_serialization_float_format = double_format ? strdup(double_format) : NULL;
- #else
- _json_c_set_last_err("json_c_set_option: not compiled with __thread support\n");
- return -1;
- #endif
- }
- else
- {
- _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)
- {
- char buf[128], *p, *q;
- int size;
-
- if (isnan(jso->o.c_double))
- {
- size = snprintf(buf, sizeof(buf), "NaN");
- }
- else if (isinf(jso->o.c_double))
- {
- if(jso->o.c_double > 0)
- size = snprintf(buf, sizeof(buf), "Infinity");
- else
- size = snprintf(buf, sizeof(buf), "-Infinity");
- }
- else
- {
- const char *std_format = "%.17g";
- int format_drops_decimals = 0;
- if (!format)
- {
- #if defined(HAVE___THREAD)
- if (tls_serialization_float_format)
- format = tls_serialization_float_format;
- else
- #endif
- if (global_serialization_float_format)
- format = global_serialization_float_format;
- else
- format = std_format;
- }
- size = snprintf(buf, sizeof(buf), format, jso->o.c_double);
- if (size < 0)
- return -1;
- p = strchr(buf, ',');
- if (p)
- *p = '.';
- else
- p = strchr(buf, '.');
- if (format == std_format || strstr(format, ".0f") == NULL)
- format_drops_decimals = 1;
- if (size < (int)sizeof(buf) - 2 &&
- isdigit((int)buf[0]) &&
- !p &&
- strchr(buf, 'e') == NULL &&
- format_drops_decimals)
- {
-
-
- strcat(buf, ".0");
- size += 2;
- }
- if (p && (flags & JSON_C_TO_STRING_NOZERO))
- {
-
- p++;
- for (q=p ; *q ; q++) {
- if (*q!='0') p=q;
- }
-
- *(++p) = 0;
- size = p-buf;
- }
- }
-
- if (size < 0)
- return -1;
- if (size >= (int)sizeof(buf))
-
-
- size = sizeof(buf) - 1;
- printbuf_memappend(pb, buf, size);
- return size;
- }
- 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);
- }
- 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);
- }
- struct json_object* json_object_new_double(double d)
- {
- struct json_object *jso = json_object_new(json_type_double);
- if (!jso)
- return NULL;
- jso->_to_json_string = &json_object_double_to_json_string_default;
- jso->o.c_double = d;
- return jso;
- }
- 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);
- if (!jso)
- return NULL;
- new_ds = strdup(ds);
- if (!new_ds)
- {
- json_object_generic_delete(jso);
- errno = ENOMEM;
- return NULL;
- }
- json_object_set_serializer(jso, json_object_userdata_to_json_string,
- new_ds, json_object_free_userdata);
- return jso;
- }
- 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);
- return userdata_len;
- }
- void json_object_free_userdata(struct json_object *jso, void *userdata)
- {
- free(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:
- return jso->o.c_int64;
- case json_type_boolean:
- return jso->o.c_boolean;
- case json_type_string:
- errno = 0;
- cdouble = strtod(get_string_component(jso), &errPtr);
-
- if (errPtr == get_string_component(jso))
- return 0.0;
-
- if (*errPtr != '\0')
- return 0.0;
-
- if ((HUGE_VAL == cdouble || -HUGE_VAL == cdouble) &&
- (ERANGE == errno))
- cdouble = 0.0;
- return cdouble;
- default:
- 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;
- return 1;
- }
- 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);
- printbuf_strappend(pb, "\"");
- return 0;
- }
- static void json_object_string_delete(struct json_object* jso)
- {
- 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 *jso = json_object_new(json_type_string);
- if (!jso)
- return NULL;
- 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) {
- memcpy(jso->o.c_string.str.data, s, jso->o.c_string.len);
- } else {
- jso->o.c_string.str.ptr = strdup(s);
- if (!jso->o.c_string.str.ptr)
- {
- json_object_generic_delete(jso);
- errno = ENOMEM;
- return NULL;
- }
- }
- return jso;
- }
- struct json_object* json_object_new_string_len(const char *s, int len)
- {
- char *dstbuf;
- struct json_object *jso = json_object_new(json_type_string);
- if (!jso)
- return NULL;
- jso->_delete = &json_object_string_delete;
- jso->_to_json_string = &json_object_string_to_json_string;
- if(len < LEN_DIRECT_STRING_DATA) {
- dstbuf = jso->o.c_string.str.data;
- } else {
- jso->o.c_string.str.ptr = (char*)malloc(len + 1);
- if (!jso->o.c_string.str.ptr)
- {
- json_object_generic_delete(jso);
- errno = ENOMEM;
- return NULL;
- }
- dstbuf = jso->o.c_string.str.ptr;
- }
- memcpy(dstbuf, (const void *)s, len);
- dstbuf[len] = '\0';
- jso->o.c_string.len = len;
- return jso;
- }
- const char* json_object_get_string(struct json_object *jso)
- {
- if (!jso)
- return NULL;
- switch(jso->o_type)
- {
- case json_type_string:
- return get_string_component(jso);
- default:
- return json_object_to_json_string(jso);
- }
- }
- int json_object_get_string_len(const struct json_object *jso)
- {
- if (!jso)
- return 0;
- switch(jso->o_type)
- {
- case json_type_string:
- return jso->o.c_string.len;
- default:
- return 0;
- }
- }
- 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;
- }
- jso->o.c_string.len=len;
- memcpy(dstbuf, (const void *)s, len);
- dstbuf[len] = '\0';
- return 1;
- }
- static int json_object_array_to_json_string(struct json_object* jso,
- struct printbuf *pb,
- int level,
- int flags)
- {
- int had_children = 0;
- size_t ii;
- printbuf_strappend(pb, "[");
- if (flags & JSON_C_TO_STRING_PRETTY)
- printbuf_strappend(pb, "\n");
- for(ii=0; ii < json_object_array_length(jso); ii++)
- {
- struct json_object *val;
- if (had_children)
- {
- printbuf_strappend(pb, ",");
- if (flags & JSON_C_TO_STRING_PRETTY)
- printbuf_strappend(pb, "\n");
- }
- had_children = 1;
- if (flags & JSON_C_TO_STRING_SPACED)
- printbuf_strappend(pb, " ");
- indent(pb, level + 1, flags);
- val = json_object_array_get_idx(jso, ii);
- if(val == NULL)
- printbuf_strappend(pb, "null");
- 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);
- }
- if (flags & JSON_C_TO_STRING_SPACED)
- return printbuf_strappend(pb, " ]");
- return printbuf_strappend(pb, "]");
- }
- static void json_object_array_entry_free(void *data)
- {
- json_object_put((struct json_object*)data);
- }
- 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 *jso = json_object_new(json_type_array);
- if (!jso)
- return NULL;
- 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)
- {
- free(jso);
- return NULL;
- }
- return jso;
- }
- struct array_list* json_object_get_array(const struct json_object *jso)
- {
- if (!jso)
- return NULL;
- switch(jso->o_type)
- {
- 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 *))
- {
- 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 **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);
- if (!result)
- return NULL;
- return *result;
- }
- size_t json_object_array_length(const struct json_object *jso)
- {
- assert(json_object_get_type(jso) == json_type_array);
- return array_list_length(jso->o.c_array);
- }
- 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)
- {
- assert(json_object_get_type(jso) == json_type_array);
- return array_list_put_idx(jso->o.c_array, idx, val);
- }
- int json_object_array_del_idx(struct json_object *jso, size_t idx, size_t count)
- {
- assert(json_object_get_type(jso) == json_type_array);
- 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)
- {
- assert(json_object_get_type(jso) == json_type_array);
- 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)
- {
- size_t len, i;
- len = json_object_array_length(jso1);
- if (len != json_object_array_length(jso2))
- return 0;
- for (i = 0; i < len; i++) {
- if (!json_object_equal(json_object_array_get_idx(jso1, i),
- json_object_array_get_idx(jso2, i)))
- return 0;
- }
- return 1;
- }
- static int json_object_all_values_equal(struct json_object* jso1,
- struct json_object* jso2)
- {
- struct json_object_iter iter;
- struct json_object *sub;
- assert(json_object_get_type(jso1) == json_type_object);
- assert(json_object_get_type(jso2) == json_type_object);
-
- 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;
- }
-
- 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)
- {
- if (jso1 == jso2)
- return 1;
- if (!jso1 || !jso2)
- return 0;
- 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);
- case json_type_double:
- return (jso1->o.c_double == jso2->o.c_double);
- case json_type_int:
- return (jso1->o.c_int64 == jso2->o.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_object:
- return json_object_all_values_equal(jso1, jso2);
- case json_type_array:
- return json_array_equal(jso1, jso2);
- case json_type_null:
- return 1;
- };
- return 0;
- }
- static int json_object_copy_serializer_data(struct json_object *src, struct json_object *dst)
- {
- if (!src->_userdata && !src->_user_delete)
- return 0;
- if (dst->_to_json_string == json_object_userdata_to_json_string)
- {
- dst->_userdata = strdup(src->_userdata);
- }
-
- else
- {
- _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;
- }
- 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;
- case json_type_double:
- *dst = json_object_new_double(src->o.c_double);
- break;
- case json_type_int:
- *dst = json_object_new_int64(src->o.c_int64);
- 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_array:
- *dst = json_object_new_array();
- break;
- default:
- errno = EINVAL;
- return -1;
- }
- if (!*dst) {
- errno = ENOMEM;
- return -1;
- }
- (*dst)->_to_json_string = src->_to_json_string;
-
- return 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)
- {
- struct json_object_iter iter;
- size_t src_array_len, ii;
- int shallow_copy_rc = 0;
- shallow_copy_rc = shallow_copy(src, parent, key_in_parent, index_in_parent, dst);
-
- if (shallow_copy_rc < 1)
- {
- errno = EINVAL;
- return -1;
- }
- assert(*dst != NULL);
- switch (src->o_type) {
- case json_type_object:
- json_object_object_foreachC(src, iter) {
- struct json_object *jso = NULL;
-
- if (!iter.val)
- jso = NULL;
- else if (json_object_deep_copy_recursive(iter.val, src, iter.key, -1, &jso, shallow_copy) < 0)
- {
- json_object_put(jso);
- return -1;
- }
- if (json_object_object_add(*dst, iter.key, jso) < 0)
- {
- json_object_put(jso);
- return -1;
- }
- }
- break;
- case json_type_array:
- src_array_len = json_object_array_length(src);
- for (ii = 0; ii < src_array_len; ii++) {
- struct json_object *jso = NULL;
- struct json_object *jso1 = json_object_array_get_idx(src, ii);
-
- if (!jso1)
- jso = NULL;
- else if (json_object_deep_copy_recursive(jso1, src, NULL, ii, &jso, shallow_copy) < 0)
- {
- json_object_put(jso);
- return -1;
- }
- if (json_object_array_add(*dst, jso) < 0)
- {
- json_object_put(jso);
- return -1;
- }
- }
- break;
- default:
- break;
-
- }
- if (shallow_copy_rc != 2)
- return json_object_copy_serializer_data(src, *dst);
- return 0;
- }
- int json_object_deep_copy(struct json_object *src, struct json_object **dst, json_c_shallow_copy_fn *shallow_copy)
- {
- int rc;
-
- if (!src || !dst || *dst) {
- errno = EINVAL;
- return -1;
- }
- if (shallow_copy == NULL)
- shallow_copy = json_c_shallow_copy_default;
- rc = json_object_deep_copy_recursive(src, NULL, NULL, -1, dst, shallow_copy);
- if (rc < 0) {
- json_object_put(*dst);
- *dst = NULL;
- }
- return rc;
- }
|