json_object.c
Go to the documentation of this file.
00001 /*
00002  * $Id: json_object.c,v 1.17 2006/07/25 03:24:50 mclark Exp $
00003  *
00004  * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
00005  * Michael Clark <michael@metaparadigm.com>
00006  *
00007  * This library is free software; you can redistribute it and/or modify
00008  * it under the terms of the MIT license. See COPYING for details.
00009  *
00010  */
00011 
00012 #include <assert.h>
00013 #include "config.h"
00014 
00015 /* This is an hack for strndup */
00016 #ifndef _GNU_SOURCE
00017 #define _GNU_SOURCE
00018 #endif
00019 
00020 #include <stdio.h>
00021 #include <stdlib.h>
00022 
00023 #include <string.h>
00024 #include <strings.h>
00025 
00026 #include "debug.h"
00027 #include "printbuf.h"
00028 #include "linkhash.h"
00029 #include "arraylist.h"
00030 #include "json_object.h"
00031 #include "json_object_private.h"
00032 #include "json_tokener.h"
00033 
00034 /*#if !HAVE_STRNDUP*/
00035   char* json_c_strndup(const char* str, size_t n);
00036 /*#endif  !HAVE_STRNDUP */
00037 
00038 /* #define REFCOUNT_DEBUG 1 */
00039 
00040 const char *json_number_chars = "0123456789.+-e";
00041 const char *json_hex_chars = "0123456789abcdef";
00042 
00043 #ifdef REFCOUNT_DEBUG
00044 static const char* json_type_name[] = {
00045   "null",
00046   "boolean",
00047   "double",
00048   "int",
00049   "object",
00050   "array",
00051   "string",
00052 };
00053 #endif /* REFCOUNT_DEBUG */
00054 
00055 static void json_object_generic_delete(struct json_object* this);
00056 static struct json_object* json_object_new(enum json_type o_type);
00057 
00058 
00059 /* ref count debugging */
00060 
00061 #ifdef REFCOUNT_DEBUG
00062 
00063 static struct lh_table *json_object_table;
00064 
00065 static void json_object_init() __attribute__ ((constructor));
00066 static void json_object_init() {
00067   mc_debug("json_object_init: creating object table\n");
00068   json_object_table = lh_kptr_table_new(128, "json_object_table", NULL);
00069 }
00070 
00071 static void json_object_fini() __attribute__ ((destructor));
00072 static void json_object_fini() {
00073   struct lh_entry *ent;
00074   if(mc_get_debug() && json_object_table->count) {
00075     mc_debug("json_object_fini: %d referenced objects at exit\n",
00076              json_object_table->count);
00077     lh_foreach(json_object_table, ent) {
00078       struct json_object* obj = (struct json_object*)ent->v;
00079       mc_debug("\t%s:%p\n", json_type_name[obj->o_type], obj);
00080     }
00081   }
00082   mc_debug("json_object_fini: freeing object table\n");
00083   lh_table_free(json_object_table);
00084 }
00085 #endif /* REFCOUNT_DEBUG */
00086 
00087 
00088 /* string escaping */
00089 
00090 static int json_escape_str(struct printbuf *pb, char *str)
00091 {
00092   int pos = 0, start_offset = 0;
00093   unsigned char c;
00094   do {
00095     c = str[pos];
00096     switch(c) {
00097     case '\0':
00098       break;
00099     case '\b':
00100     case '\n':
00101     case '\r':
00102     case '\t':
00103     case '"':
00104     case '\\':
00105     case '/':
00106       if(pos - start_offset > 0)
00107         printbuf_memappend(pb, str + start_offset, pos - start_offset);
00108       if(c == '\b') printbuf_memappend(pb, "\\b", 2);
00109       else if(c == '\n') printbuf_memappend(pb, "\\n", 2);
00110       else if(c == '\r') printbuf_memappend(pb, "\\r", 2);
00111       else if(c == '\t') printbuf_memappend(pb, "\\t", 2);
00112       else if(c == '"') printbuf_memappend(pb, "\\\"", 2);
00113       else if(c == '\\') printbuf_memappend(pb, "\\\\", 2);
00114       else if(c == '/') printbuf_memappend(pb, "\\/", 2);
00115       start_offset = ++pos;
00116       break;
00117     default:
00118       if(c < ' ') {
00119         if(pos - start_offset > 0)
00120           printbuf_memappend(pb, str + start_offset, pos - start_offset);
00121         sprintbuf(pb, "\\u00%c%c",
00122                   json_hex_chars[c >> 4],
00123                   json_hex_chars[c & 0xf]);
00124         start_offset = ++pos;
00125       } else pos++;
00126     }
00127   } while(c);
00128   if(pos - start_offset > 0)
00129     printbuf_memappend(pb, str + start_offset, pos - start_offset);
00130   return 0;
00131 }
00132 
00133 
00134 /* reference counting */
00135 
00136 extern struct json_object* json_object_get(struct json_object *this)
00137 {
00138   if(this) {
00139     this->_ref_count++;
00140   }
00141   return this;
00142 }
00143 
00144 extern void json_object_put(struct json_object *this)
00145 {
00146   if(this) {
00147     this->_ref_count--;
00148     if(!this->_ref_count) this->_delete(this);
00149   }
00150 }
00151 
00152 
00153 /* generic object construction and destruction parts */
00154 
00155 static void json_object_generic_delete(struct json_object* this)
00156 {
00157 #ifdef REFCOUNT_DEBUG
00158   mc_debug("json_object_delete_%s: %p\n",
00159            json_type_name[this->o_type], this);
00160   lh_table_delete(json_object_table, this);
00161 #endif /* REFCOUNT_DEBUG */
00162   printbuf_free(this->_pb);
00163   free(this);
00164 }
00165 
00166 static struct json_object* json_object_new(enum json_type o_type)
00167 {
00168   struct json_object *this = calloc(sizeof(struct json_object), 1);
00169   if(!this) return NULL;
00170   this->o_type = o_type;
00171   this->_ref_count = 1;
00172   this->_delete = &json_object_generic_delete;
00173 #ifdef REFCOUNT_DEBUG
00174   lh_table_insert(json_object_table, this, this);
00175   mc_debug("json_object_new_%s: %p\n", json_type_name[this->o_type], this);
00176 #endif /* REFCOUNT_DEBUG */
00177   return this;
00178 }
00179 
00180 
00181 /* type checking functions */
00182 
00183 /*int json_object_is_type(struct json_object *this, enum json_type type)*/
00184 int json_object_is_type(struct json_object *this, int type)
00185 {
00186         if(!this && json_type_null == type) return 1;
00187   return (this->o_type == type);
00188 }
00189 
00190 enum json_type json_object_get_type(struct json_object *this)
00191 {
00192   if(!this) return json_type_null;
00193   return this->o_type;
00194 }
00195 
00196 
00197 /* json_object_to_json_string */
00198 
00199 const char* json_object_to_json_string(struct json_object *this)
00200 {
00201   if(!this) return "null";
00202   if(!this->_pb) {
00203     if(!(this->_pb = printbuf_new())) return NULL;
00204   } else {
00205     printbuf_reset(this->_pb);
00206   }
00207   if(this->_to_json_string(this, this->_pb) < 0) return NULL;
00208   return this->_pb->buf;
00209 }
00210 
00211 
00212 /* json_object_object */
00213 
00214 static int json_object_object_to_json_string(struct json_object* this,
00215                                              struct printbuf *pb)
00216 {
00217   int i=0;
00218   struct json_object_iter iter;
00219   sprintbuf(pb, "{");
00220 
00221   /* CAW: scope operator to make ANSI correctness */
00222   /* CAW: switched to json_object_object_foreachC which uses an iterator struct */
00223         json_object_object_foreachC(this, iter) {
00224                         if(i) sprintbuf(pb, ",");
00225                         sprintbuf(pb, " \"");
00226                         json_escape_str(pb, iter.key);
00227                         sprintbuf(pb, "\": ");
00228                         if(iter.val == NULL) sprintbuf(pb, "null");
00229                         else iter.val->_to_json_string(iter.val, pb);
00230                         i++;
00231         }
00232 
00233   return sprintbuf(pb, " }");
00234 }
00235 
00236 static void json_object_lh_entry_free(struct lh_entry *ent)
00237 {
00238   free(ent->k);
00239   json_object_put((struct json_object*)ent->v);
00240 }
00241 
00242 static void json_object_object_delete(struct json_object* this)
00243 {
00244   lh_table_free(this->o.c_object);
00245   json_object_generic_delete(this);
00246 }
00247 
00248 struct json_object* json_object_new_object()
00249 {
00250   struct json_object *this = json_object_new(json_type_object);
00251   if(!this) return NULL;
00252   this->_delete = &json_object_object_delete;
00253   this->_to_json_string = &json_object_object_to_json_string;
00254   this->o.c_object = lh_kchar_table_new(JSON_OBJECT_DEF_HASH_ENTIRES,
00255                                         NULL, &json_object_lh_entry_free);
00256   return this;
00257 }
00258 
00259 struct lh_table* json_object_get_object(struct json_object *this)
00260 {
00261   if(!this) return NULL;
00262   switch(this->o_type) {
00263   case json_type_object:
00264     return this->o.c_object;
00265   default:
00266     return NULL;
00267   }
00268 }
00269 
00270 void json_object_object_add(struct json_object* this, const char *key,
00271                             struct json_object *val)
00272 {
00273   lh_table_delete(this->o.c_object,  (char*) key);
00274   lh_table_insert(this->o.c_object, strdup(key), val);
00275 }
00276 
00277 struct json_object* json_object_object_get(struct json_object* this, const char *key)
00278 {
00279   return (struct json_object*) lh_table_lookup(this->o.c_object, (char*)key);
00280 }
00281 
00282 void json_object_object_del(struct json_object* this, const char *key)
00283 {
00284   lh_table_delete(this->o.c_object,  (char*) key);
00285 }
00286 
00287 
00288 /* json_object_boolean */
00289 
00290 static int json_object_boolean_to_json_string(struct json_object* this,
00291                                               struct printbuf *pb)
00292 {
00293   if(this->o.c_boolean) return sprintbuf(pb, "true");
00294   else return sprintbuf(pb, "false");
00295 }
00296 
00297 struct json_object* json_object_new_boolean(boolean b)
00298 {
00299   struct json_object *this = json_object_new(json_type_boolean);
00300   if(!this) return NULL;
00301   this->_to_json_string = &json_object_boolean_to_json_string;
00302   this->o.c_boolean = b;
00303   return this;
00304 }
00305 
00306 boolean json_object_get_boolean(struct json_object *this)
00307 {
00308   if(!this) return FALSE;
00309   switch(this->o_type) {
00310   case json_type_boolean:
00311     return this->o.c_boolean;
00312   case json_type_int:
00313     return (this->o.c_int != 0);
00314   case json_type_double:
00315     return (this->o.c_double != 0);
00316   case json_type_string:
00317     if(strlen(this->o.c_string)) return TRUE;
00318   default:
00319     return TRUE;
00320   }
00321 }
00322 
00323 
00324 /* json_object_int */
00325 
00326 static int json_object_int_to_json_string(struct json_object* this,
00327                                           struct printbuf *pb)
00328 {
00329   return sprintbuf(pb, "%d", this->o.c_int);
00330 }
00331 
00332 struct json_object* json_object_new_int(int i)
00333 {
00334   struct json_object *this = json_object_new(json_type_int);
00335   if(!this) return NULL;
00336   this->_to_json_string = &json_object_int_to_json_string;
00337   this->o.c_int = i;
00338   return this;
00339 }
00340 
00341 int json_object_get_int(struct json_object *this)
00342 {
00343   int cint;
00344 
00345   if(!this) return 0;
00346   switch(this->o_type) {
00347   case json_type_int:
00348     return this->o.c_int;
00349   case json_type_double:
00350     return (int)this->o.c_double;
00351   case json_type_boolean:
00352     return this->o.c_boolean;
00353   case json_type_string:
00354     if(sscanf(this->o.c_string, "%d", &cint) == 1) return cint;
00355   default:
00356     return 0;
00357   }
00358 }
00359 
00360 const char *float_format = "%e";
00361 
00362 void json_set_float_format(const char*f) {
00363     float_format = f;
00364 }
00365 
00366 /* json_object_double */
00367 
00368 static int json_object_double_to_json_string(struct json_object* this,
00369                                              struct printbuf *pb)
00370 {
00371 #define AC_BETTER_PRECISION
00372 #ifdef AC_BETTER_PRECISION
00373 //#warning json: Using better precision in printing floats
00374         if( ((int) this->o.c_double) !=  this->o.c_double)
00375 //              return sprintbuf(pb, "%g", this->o.c_double);
00376             return sprintbuf(pb, float_format, this->o.c_double);
00377         else
00378                 return sprintbuf(pb, "%d.0", (int) this->o.c_double);
00379 #else
00380   return sprintbuf(pb, "%lf", this->o.c_double);
00381 
00382 #endif
00383 
00384 }
00385 
00386 struct json_object* json_object_new_double(double d)
00387 {
00388   struct json_object *this = json_object_new(json_type_double);
00389   if(!this) return NULL;
00390   this->_to_json_string = &json_object_double_to_json_string;
00391   this->o.c_double = d;
00392   return this;
00393 }
00394 
00395 double json_object_get_double(struct json_object *this)
00396 {
00397   double cdouble;
00398 
00399   if(!this) return 0.0;
00400   switch(this->o_type) {
00401   case json_type_double:
00402     return this->o.c_double;
00403   case json_type_int:
00404     return this->o.c_int;
00405   case json_type_boolean:
00406     return this->o.c_boolean;
00407   case json_type_string:
00408     if(sscanf(this->o.c_string, "%lf", &cdouble) == 1) return cdouble;
00409   default:
00410     return 0.0;
00411   }
00412 }
00413 
00414 
00415 /* json_object_string */
00416 
00417 static int json_object_string_to_json_string(struct json_object* this,
00418                                              struct printbuf *pb)
00419 {
00420   sprintbuf(pb, "\"");
00421   json_escape_str(pb, this->o.c_string);
00422   sprintbuf(pb, "\"");
00423   return 0;
00424 }
00425 
00426 static void json_object_string_delete(struct json_object* this)
00427 {
00428   free(this->o.c_string);
00429   json_object_generic_delete(this);
00430 }
00431 
00432 struct json_object* json_object_new_string(const char *s)
00433 {
00434   struct json_object *this = json_object_new(json_type_string);
00435   if(!this) return NULL;
00436   this->_delete = &json_object_string_delete;
00437   this->_to_json_string = &json_object_string_to_json_string;
00438   this->o.c_string = json_c_strndup(s, strlen(s));
00439   return this;
00440 }
00441 
00442 struct json_object* json_object_new_string_len(const char *s, int len)
00443 {
00444   struct json_object *this = json_object_new(json_type_string);
00445   if(!this) return NULL;
00446   this->_delete = &json_object_string_delete;
00447   this->_to_json_string = &json_object_string_to_json_string;
00448   this->o.c_string = json_c_strndup(s, (size_t)len);
00449   return this;
00450 }
00451 
00452 char* json_object_get_string(struct json_object *this)
00453 {
00454   if(!this) return NULL;
00455   switch(this->o_type) {
00456   case json_type_string:
00457     return this->o.c_string;
00458   default:
00459     return json_object_to_json_string(this);
00460   }
00461 }
00462 
00463 
00464 /* json_object_array */
00465 
00466 static int json_object_array_to_json_string(struct json_object* this,
00467                                             struct printbuf *pb)
00468 {
00469   int i;
00470   sprintbuf(pb, "[");
00471   for(i=0; i < json_object_array_length(this); i++) {
00472           struct json_object *val;
00473           if(i) { sprintbuf(pb, ", "); }
00474           else { sprintbuf(pb, " "); }
00475 
00476       val = json_object_array_get_idx(this, i);
00477           if(val == NULL) { sprintbuf(pb, "null"); }
00478           else { val->_to_json_string(val, pb); }
00479   }
00480   return sprintbuf(pb, " ]");
00481 }
00482 
00483 static void json_object_array_entry_free(void *data)
00484 {
00485   json_object_put((struct json_object*)data);
00486 }
00487 
00488 static void json_object_array_delete(struct json_object* this)
00489 {
00490         assert(json_object_is_type(this, json_type_array));
00491   array_list_free(this->o.c_array);
00492   json_object_generic_delete(this);
00493 }
00494 
00495 struct json_object* json_object_new_array()
00496 {
00497   struct json_object *this = json_object_new(json_type_array);
00498   if(!this) return NULL;
00499   this->_delete = &json_object_array_delete;
00500   this->_to_json_string = &json_object_array_to_json_string;
00501   this->o.c_array = array_list_new(&json_object_array_entry_free);
00502   return this;
00503 }
00504 
00505 struct array_list* json_object_get_array(struct json_object *this)
00506 {
00507   if(!this) return NULL;
00508   switch(this->o_type) {
00509   case json_type_array:
00510     return this->o.c_array;
00511   default:
00512     return NULL;
00513   }
00514 }
00515 
00516 int json_object_array_length(struct json_object *this)
00517 {
00518         assert(json_object_is_type(this, json_type_array));
00519         return array_list_length(this->o.c_array);
00520 }
00521 
00522 int json_object_array_add(struct json_object *this,struct json_object *val)
00523 {
00524         assert(json_object_is_type(this, json_type_array));
00525         return array_list_add(this->o.c_array, val);
00526 }
00527 
00528 int json_object_array_put_idx(struct json_object *this, int idx,
00529                               struct json_object *val)
00530 {
00531         assert(json_object_is_type(this, json_type_array));
00532         return array_list_put_idx(this->o.c_array, idx, val);
00533 }
00534 
00535 struct json_object* json_object_array_get_idx(struct json_object *this,
00536                                               int idx)
00537 {
00538         assert(json_object_is_type(this, json_type_array));
00539         return (struct json_object*)array_list_get_idx(this->o.c_array, idx);
00540 }
00541 


csm_ros
Author(s): Gaƫl Ecorchard , Ivan Dryanovski, William Morris, Andrea Censi
autogenerated on Sat Jun 8 2019 19:52:42