fizx-ordered_json 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. data/.gitignore +29 -0
  2. data/CHANGELOG +22 -0
  3. data/README +4 -0
  4. data/Rakefile +35 -0
  5. data/ext/Makefile +149 -0
  6. data/ext/json-c-0.8/AUTHORS +2 -0
  7. data/ext/json-c-0.8/COPYING +19 -0
  8. data/ext/json-c-0.8/ChangeLog +73 -0
  9. data/ext/json-c-0.8/INSTALL +229 -0
  10. data/ext/json-c-0.8/Makefile.am +43 -0
  11. data/ext/json-c-0.8/Makefile.in +734 -0
  12. data/ext/json-c-0.8/NEWS +1 -0
  13. data/ext/json-c-0.8/README +20 -0
  14. data/ext/json-c-0.8/aclocal.m4 +8794 -0
  15. data/ext/json-c-0.8/arraylist.c +93 -0
  16. data/ext/json-c-0.8/arraylist.h +45 -0
  17. data/ext/json-c-0.8/bits.h +27 -0
  18. data/ext/json-c-0.8/config.guess +1526 -0
  19. data/ext/json-c-0.8/config.h.in +121 -0
  20. data/ext/json-c-0.8/config.sub +1658 -0
  21. data/ext/json-c-0.8/configure +14243 -0
  22. data/ext/json-c-0.8/configure.in +33 -0
  23. data/ext/json-c-0.8/debug.c +98 -0
  24. data/ext/json-c-0.8/debug.h +42 -0
  25. data/ext/json-c-0.8/depcomp +530 -0
  26. data/ext/json-c-0.8/doc/html/annotated.html +40 -0
  27. data/ext/json-c-0.8/doc/html/arraylist_8h.html +234 -0
  28. data/ext/json-c-0.8/doc/html/bits_8h.html +144 -0
  29. data/ext/json-c-0.8/doc/html/config_8h.html +606 -0
  30. data/ext/json-c-0.8/doc/html/debug_8h.html +386 -0
  31. data/ext/json-c-0.8/doc/html/doxygen.css +473 -0
  32. data/ext/json-c-0.8/doc/html/doxygen.png +0 -0
  33. data/ext/json-c-0.8/doc/html/files.html +42 -0
  34. data/ext/json-c-0.8/doc/html/functions.html +206 -0
  35. data/ext/json-c-0.8/doc/html/functions_vars.html +206 -0
  36. data/ext/json-c-0.8/doc/html/globals.html +445 -0
  37. data/ext/json-c-0.8/doc/html/globals_defs.html +200 -0
  38. data/ext/json-c-0.8/doc/html/globals_enum.html +50 -0
  39. data/ext/json-c-0.8/doc/html/globals_eval.html +135 -0
  40. data/ext/json-c-0.8/doc/html/globals_func.html +194 -0
  41. data/ext/json-c-0.8/doc/html/globals_type.html +58 -0
  42. data/ext/json-c-0.8/doc/html/globals_vars.html +50 -0
  43. data/ext/json-c-0.8/doc/html/index.html +25 -0
  44. data/ext/json-c-0.8/doc/html/json_8h.html +26 -0
  45. data/ext/json-c-0.8/doc/html/json__object_8h.html +1042 -0
  46. data/ext/json-c-0.8/doc/html/json__object__private_8h.html +69 -0
  47. data/ext/json-c-0.8/doc/html/json__tokener_8h.html +360 -0
  48. data/ext/json-c-0.8/doc/html/json__util_8h.html +100 -0
  49. data/ext/json-c-0.8/doc/html/linkhash_8h.html +734 -0
  50. data/ext/json-c-0.8/doc/html/printbuf_8h.html +171 -0
  51. data/ext/json-c-0.8/doc/html/structarray__list.html +104 -0
  52. data/ext/json-c-0.8/doc/html/structjson__object.html +141 -0
  53. data/ext/json-c-0.8/doc/html/structjson__object__iter.html +87 -0
  54. data/ext/json-c-0.8/doc/html/structjson__tokener.html +206 -0
  55. data/ext/json-c-0.8/doc/html/structjson__tokener__srec.html +104 -0
  56. data/ext/json-c-0.8/doc/html/structlh__entry.html +105 -0
  57. data/ext/json-c-0.8/doc/html/structlh__table.html +275 -0
  58. data/ext/json-c-0.8/doc/html/structprintbuf.html +87 -0
  59. data/ext/json-c-0.8/doc/html/tab_b.gif +0 -0
  60. data/ext/json-c-0.8/doc/html/tab_l.gif +0 -0
  61. data/ext/json-c-0.8/doc/html/tab_r.gif +0 -0
  62. data/ext/json-c-0.8/doc/html/tabs.css +102 -0
  63. data/ext/json-c-0.8/doc/html/unionjson__object_1_1data.html +140 -0
  64. data/ext/json-c-0.8/install-sh +519 -0
  65. data/ext/json-c-0.8/json.h +31 -0
  66. data/ext/json-c-0.8/json.pc.in +11 -0
  67. data/ext/json-c-0.8/json_object.c +511 -0
  68. data/ext/json-c-0.8/json_object.h +310 -0
  69. data/ext/json-c-0.8/json_object_private.h +44 -0
  70. data/ext/json-c-0.8/json_tokener.c +517 -0
  71. data/ext/json-c-0.8/json_tokener.h +90 -0
  72. data/ext/json-c-0.8/json_util.c +122 -0
  73. data/ext/json-c-0.8/json_util.h +23 -0
  74. data/ext/json-c-0.8/linkhash.c +218 -0
  75. data/ext/json-c-0.8/linkhash.h +264 -0
  76. data/ext/json-c-0.8/ltmain.sh +7880 -0
  77. data/ext/json-c-0.8/missing +360 -0
  78. data/ext/json-c-0.8/printbuf.c +145 -0
  79. data/ext/json-c-0.8/printbuf.h +38 -0
  80. data/ext/json-c-0.8/test1.c +164 -0
  81. data/ext/json-c-0.8/test2.c +20 -0
  82. data/ext/json-c-0.8/test3.c +22 -0
  83. data/ext/ordered_json_c.c +199 -0
  84. data/lib/ordered_json.rb +26 -0
  85. data/ordered_json.gemspec +102 -0
  86. data/test/crashing_example.rb +1 -0
  87. data/test/ordered_json_test.rb +86 -0
  88. data/test/pretty.json +11 -0
  89. metadata +91 -50
@@ -0,0 +1,511 @@
1
+ /*
2
+ * $Id: json_object.c,v 1.17 2006/07/25 03:24:50 mclark Exp $
3
+ *
4
+ * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
5
+ * Michael Clark <michael@metaparadigm.com>
6
+ *
7
+ * This library is free software; you can redistribute it and/or modify
8
+ * it under the terms of the MIT license. See COPYING for details.
9
+ *
10
+ */
11
+
12
+ #include "config.h"
13
+
14
+ #include <stdio.h>
15
+ #include <stdlib.h>
16
+ #include <stddef.h>
17
+ #include <string.h>
18
+
19
+ #include "debug.h"
20
+ #include "printbuf.h"
21
+ #include "linkhash.h"
22
+ #include "arraylist.h"
23
+ #include "json_object.h"
24
+ #include "json_object_private.h"
25
+ #include "json_tokener.h"
26
+
27
+ #if !HAVE_STRNDUP
28
+ char* strndup(const char* str, size_t n);
29
+ #endif /* !HAVE_STRNDUP */
30
+
31
+ #define REFCOUNT_DEBUG 1
32
+
33
+ char *json_number_chars = "0123456789.+-eE";
34
+ char *json_hex_chars = "0123456789abcdef";
35
+
36
+ #ifdef REFCOUNT_DEBUG
37
+ static char* json_type_name[] = {
38
+ "null",
39
+ "boolean",
40
+ "double",
41
+ "int",
42
+ "object",
43
+ "array",
44
+ "string",
45
+ };
46
+ #endif /* REFCOUNT_DEBUG */
47
+
48
+ static void json_object_generic_delete(struct json_object* this);
49
+ static struct json_object* json_object_new(enum json_type o_type);
50
+
51
+
52
+ /* ref count debugging */
53
+
54
+ #ifdef REFCOUNT_DEBUG
55
+
56
+ static struct lh_table *json_object_table;
57
+
58
+ static void json_object_init(void) __attribute__ ((constructor));
59
+ static void json_object_init(void) {
60
+ MC_DEBUG("json_object_init: creating object table\n");
61
+ json_object_table = lh_kptr_table_new(128, "json_object_table", NULL);
62
+ }
63
+
64
+ static void json_object_fini(void) __attribute__ ((destructor));
65
+ static void json_object_fini(void) {
66
+ struct lh_entry *ent;
67
+ if(MC_GET_DEBUG()) {
68
+ if (json_object_table->count) {
69
+ MC_DEBUG("json_object_fini: %d referenced objects at exit\n",
70
+ json_object_table->count);
71
+ lh_foreach(json_object_table, ent) {
72
+ struct json_object* obj = (struct json_object*)ent->v;
73
+ MC_DEBUG("\t%s:%p\n", json_type_name[obj->o_type], obj);
74
+ }
75
+ }
76
+ }
77
+ MC_DEBUG("json_object_fini: freeing object table\n");
78
+ lh_table_free(json_object_table);
79
+ }
80
+ #endif /* REFCOUNT_DEBUG */
81
+
82
+
83
+ /* string escaping */
84
+
85
+ static int json_escape_str(struct printbuf *pb, char *str)
86
+ {
87
+ int pos = 0, start_offset = 0;
88
+ unsigned char c;
89
+ do {
90
+ c = str[pos];
91
+ switch(c) {
92
+ case '\0':
93
+ break;
94
+ case '\b':
95
+ case '\n':
96
+ case '\r':
97
+ case '\t':
98
+ case '"':
99
+ case '\\':
100
+ case '/':
101
+ if(pos - start_offset > 0)
102
+ printbuf_memappend(pb, str + start_offset, pos - start_offset);
103
+ if(c == '\b') printbuf_memappend(pb, "\\b", 2);
104
+ else if(c == '\n') printbuf_memappend(pb, "\\n", 2);
105
+ else if(c == '\r') printbuf_memappend(pb, "\\r", 2);
106
+ else if(c == '\t') printbuf_memappend(pb, "\\t", 2);
107
+ else if(c == '"') printbuf_memappend(pb, "\\\"", 2);
108
+ else if(c == '\\') printbuf_memappend(pb, "\\\\", 2);
109
+ else if(c == '/') printbuf_memappend(pb, "\\/", 2);
110
+ start_offset = ++pos;
111
+ break;
112
+ default:
113
+ if(c < ' ') {
114
+ if(pos - start_offset > 0)
115
+ printbuf_memappend(pb, str + start_offset, pos - start_offset);
116
+ sprintbuf(pb, "\\u00%c%c",
117
+ json_hex_chars[c >> 4],
118
+ json_hex_chars[c & 0xf]);
119
+ start_offset = ++pos;
120
+ } else pos++;
121
+ }
122
+ } while(c);
123
+ if(pos - start_offset > 0)
124
+ printbuf_memappend(pb, str + start_offset, pos - start_offset);
125
+ return 0;
126
+ }
127
+
128
+
129
+ /* reference counting */
130
+
131
+ extern struct json_object* json_object_get(struct json_object *this)
132
+ {
133
+ if(this) {
134
+ this->_ref_count++;
135
+ }
136
+ return this;
137
+ }
138
+
139
+ extern void json_object_put(struct json_object *this)
140
+ {
141
+ if(this) {
142
+ this->_ref_count--;
143
+ if(!this->_ref_count) this->_delete(this);
144
+ }
145
+ }
146
+
147
+
148
+ /* generic object construction and destruction parts */
149
+
150
+ static void json_object_generic_delete(struct json_object* this)
151
+ {
152
+ #ifdef REFCOUNT_DEBUG
153
+ MC_DEBUG("json_object_delete_%s: %p\n",
154
+ json_type_name[this->o_type], this);
155
+ lh_table_delete(json_object_table, this);
156
+ #endif /* REFCOUNT_DEBUG */
157
+ printbuf_free(this->_pb);
158
+ free(this);
159
+ }
160
+
161
+ static struct json_object* json_object_new(enum json_type o_type)
162
+ {
163
+ struct json_object *this = calloc(sizeof(struct json_object), 1);
164
+ if(!this) return NULL;
165
+ this->o_type = o_type;
166
+ this->_ref_count = 1;
167
+ this->_delete = &json_object_generic_delete;
168
+ #ifdef REFCOUNT_DEBUG
169
+ lh_table_insert(json_object_table, this, this);
170
+ MC_DEBUG("json_object_new_%s: %p\n", json_type_name[this->o_type], this);
171
+ #endif /* REFCOUNT_DEBUG */
172
+ return this;
173
+ }
174
+
175
+
176
+ /* type checking functions */
177
+
178
+ int json_object_is_type(struct json_object *this, enum json_type type)
179
+ {
180
+ return (this->o_type == type);
181
+ }
182
+
183
+ enum json_type json_object_get_type(struct json_object *this)
184
+ {
185
+ return this->o_type;
186
+ }
187
+
188
+
189
+ /* json_object_to_json_string */
190
+
191
+ char* json_object_to_json_string(struct json_object *this)
192
+ {
193
+ if(!this) return "null";
194
+ if(!this->_pb) {
195
+ if(!(this->_pb = printbuf_new())) return NULL;
196
+ } else {
197
+ printbuf_reset(this->_pb);
198
+ }
199
+ if(this->_to_json_string(this, this->_pb) < 0) return NULL;
200
+ return this->_pb->buf;
201
+ }
202
+
203
+
204
+ /* json_object_object */
205
+
206
+ static int json_object_object_to_json_string(struct json_object* this,
207
+ struct printbuf *pb)
208
+ {
209
+ int i=0;
210
+ struct json_object_iter iter;
211
+ sprintbuf(pb, "{");
212
+
213
+ /* CAW: scope operator to make ANSI correctness */
214
+ /* CAW: switched to json_object_object_foreachC which uses an iterator struct */
215
+ json_object_object_foreachC(this, iter) {
216
+ if(i) sprintbuf(pb, ",");
217
+ sprintbuf(pb, " \"");
218
+ json_escape_str(pb, iter.key);
219
+ sprintbuf(pb, "\": ");
220
+ if(iter.val == NULL) sprintbuf(pb, "null");
221
+ else iter.val->_to_json_string(iter.val, pb);
222
+ i++;
223
+ }
224
+
225
+ return sprintbuf(pb, " }");
226
+ }
227
+
228
+ static void json_object_lh_entry_free(struct lh_entry *ent)
229
+ {
230
+ free(ent->k);
231
+ json_object_put((struct json_object*)ent->v);
232
+ }
233
+
234
+ static void json_object_object_delete(struct json_object* this)
235
+ {
236
+ lh_table_free(this->o.c_object);
237
+ json_object_generic_delete(this);
238
+ }
239
+
240
+ struct json_object* json_object_new_object()
241
+ {
242
+ struct json_object *this = json_object_new(json_type_object);
243
+ if(!this) return NULL;
244
+ this->_delete = &json_object_object_delete;
245
+ this->_to_json_string = &json_object_object_to_json_string;
246
+ this->o.c_object = lh_kchar_table_new(JSON_OBJECT_DEF_HASH_ENTIRES,
247
+ NULL, &json_object_lh_entry_free);
248
+ return this;
249
+ }
250
+
251
+ struct lh_table* json_object_get_object(struct json_object *this)
252
+ {
253
+ if(!this) return NULL;
254
+ switch(this->o_type) {
255
+ case json_type_object:
256
+ return this->o.c_object;
257
+ default:
258
+ return NULL;
259
+ }
260
+ }
261
+
262
+ void json_object_object_add(struct json_object* this, char *key,
263
+ struct json_object *val)
264
+ {
265
+ lh_table_delete(this->o.c_object, key);
266
+ lh_table_insert(this->o.c_object, strdup(key), val);
267
+ }
268
+
269
+ struct json_object* json_object_object_get(struct json_object* this, char *key)
270
+ {
271
+ return (struct json_object*) lh_table_lookup(this->o.c_object, key);
272
+ }
273
+
274
+ void json_object_object_del(struct json_object* this, char *key)
275
+ {
276
+ lh_table_delete(this->o.c_object, key);
277
+ }
278
+
279
+
280
+ /* json_object_boolean */
281
+
282
+ static int json_object_boolean_to_json_string(struct json_object* this,
283
+ struct printbuf *pb)
284
+ {
285
+ if(this->o.c_boolean) return sprintbuf(pb, "true");
286
+ else return sprintbuf(pb, "false");
287
+ }
288
+
289
+ struct json_object* json_object_new_boolean(boolean b)
290
+ {
291
+ struct json_object *this = json_object_new(json_type_boolean);
292
+ if(!this) return NULL;
293
+ this->_to_json_string = &json_object_boolean_to_json_string;
294
+ this->o.c_boolean = b;
295
+ return this;
296
+ }
297
+
298
+ boolean json_object_get_boolean(struct json_object *this)
299
+ {
300
+ if(!this) return FALSE;
301
+ switch(this->o_type) {
302
+ case json_type_boolean:
303
+ return this->o.c_boolean;
304
+ case json_type_int:
305
+ return (this->o.c_int != 0);
306
+ case json_type_double:
307
+ return (this->o.c_double != 0);
308
+ case json_type_string:
309
+ if(strlen(this->o.c_string)) return TRUE;
310
+ default:
311
+ return TRUE;
312
+ }
313
+ }
314
+
315
+
316
+ /* json_object_int */
317
+
318
+ static int json_object_int_to_json_string(struct json_object* this,
319
+ struct printbuf *pb)
320
+ {
321
+ return sprintbuf(pb, "%d", this->o.c_int);
322
+ }
323
+
324
+ struct json_object* json_object_new_int(int i)
325
+ {
326
+ struct json_object *this = json_object_new(json_type_int);
327
+ if(!this) return NULL;
328
+ this->_to_json_string = &json_object_int_to_json_string;
329
+ this->o.c_int = i;
330
+ return this;
331
+ }
332
+
333
+ int json_object_get_int(struct json_object *this)
334
+ {
335
+ int cint;
336
+
337
+ if(!this) return 0;
338
+ switch(this->o_type) {
339
+ case json_type_int:
340
+ return this->o.c_int;
341
+ case json_type_double:
342
+ return (int)this->o.c_double;
343
+ case json_type_boolean:
344
+ return this->o.c_boolean;
345
+ case json_type_string:
346
+ if(sscanf(this->o.c_string, "%d", &cint) == 1) return cint;
347
+ default:
348
+ return 0;
349
+ }
350
+ }
351
+
352
+
353
+ /* json_object_double */
354
+
355
+ static int json_object_double_to_json_string(struct json_object* this,
356
+ struct printbuf *pb)
357
+ {
358
+ return sprintbuf(pb, "%lf", this->o.c_double);
359
+ }
360
+
361
+ struct json_object* json_object_new_double(double d)
362
+ {
363
+ struct json_object *this = json_object_new(json_type_double);
364
+ if(!this) return NULL;
365
+ this->_to_json_string = &json_object_double_to_json_string;
366
+ this->o.c_double = d;
367
+ return this;
368
+ }
369
+
370
+ double json_object_get_double(struct json_object *this)
371
+ {
372
+ double cdouble;
373
+
374
+ if(!this) return 0.0;
375
+ switch(this->o_type) {
376
+ case json_type_double:
377
+ return this->o.c_double;
378
+ case json_type_int:
379
+ return this->o.c_int;
380
+ case json_type_boolean:
381
+ return this->o.c_boolean;
382
+ case json_type_string:
383
+ if(sscanf(this->o.c_string, "%lf", &cdouble) == 1) return cdouble;
384
+ default:
385
+ return 0.0;
386
+ }
387
+ }
388
+
389
+
390
+ /* json_object_string */
391
+
392
+ static int json_object_string_to_json_string(struct json_object* this,
393
+ struct printbuf *pb)
394
+ {
395
+ sprintbuf(pb, "\"");
396
+ json_escape_str(pb, this->o.c_string);
397
+ sprintbuf(pb, "\"");
398
+ return 0;
399
+ }
400
+
401
+ static void json_object_string_delete(struct json_object* this)
402
+ {
403
+ free(this->o.c_string);
404
+ json_object_generic_delete(this);
405
+ }
406
+
407
+ struct json_object* json_object_new_string(char *s)
408
+ {
409
+ struct json_object *this = json_object_new(json_type_string);
410
+ if(!this) return NULL;
411
+ this->_delete = &json_object_string_delete;
412
+ this->_to_json_string = &json_object_string_to_json_string;
413
+ this->o.c_string = strdup(s);
414
+ return this;
415
+ }
416
+
417
+ struct json_object* json_object_new_string_len(char *s, int len)
418
+ {
419
+ struct json_object *this = json_object_new(json_type_string);
420
+ if(!this) return NULL;
421
+ this->_delete = &json_object_string_delete;
422
+ this->_to_json_string = &json_object_string_to_json_string;
423
+ this->o.c_string = strndup(s, len);
424
+ return this;
425
+ }
426
+
427
+ char* json_object_get_string(struct json_object *this)
428
+ {
429
+ if(!this) return NULL;
430
+ switch(this->o_type) {
431
+ case json_type_string:
432
+ return this->o.c_string;
433
+ default:
434
+ return json_object_to_json_string(this);
435
+ }
436
+ }
437
+
438
+
439
+ /* json_object_array */
440
+
441
+ static int json_object_array_to_json_string(struct json_object* this,
442
+ struct printbuf *pb)
443
+ {
444
+ int i;
445
+ sprintbuf(pb, "[");
446
+ for(i=0; i < json_object_array_length(this); i++) {
447
+ struct json_object *val;
448
+ if(i) { sprintbuf(pb, ", "); }
449
+ else { sprintbuf(pb, " "); }
450
+
451
+ val = json_object_array_get_idx(this, i);
452
+ if(val == NULL) { sprintbuf(pb, "null"); }
453
+ else { val->_to_json_string(val, pb); }
454
+ }
455
+ return sprintbuf(pb, " ]");
456
+ }
457
+
458
+ static void json_object_array_entry_free(void *data)
459
+ {
460
+ json_object_put((struct json_object*)data);
461
+ }
462
+
463
+ static void json_object_array_delete(struct json_object* this)
464
+ {
465
+ array_list_free(this->o.c_array);
466
+ json_object_generic_delete(this);
467
+ }
468
+
469
+ struct json_object* json_object_new_array()
470
+ {
471
+ struct json_object *this = json_object_new(json_type_array);
472
+ if(!this) return NULL;
473
+ this->_delete = &json_object_array_delete;
474
+ this->_to_json_string = &json_object_array_to_json_string;
475
+ this->o.c_array = array_list_new(&json_object_array_entry_free);
476
+ return this;
477
+ }
478
+
479
+ struct array_list* json_object_get_array(struct json_object *this)
480
+ {
481
+ if(!this) return NULL;
482
+ switch(this->o_type) {
483
+ case json_type_array:
484
+ return this->o.c_array;
485
+ default:
486
+ return NULL;
487
+ }
488
+ }
489
+
490
+ int json_object_array_length(struct json_object *this)
491
+ {
492
+ return array_list_length(this->o.c_array);
493
+ }
494
+
495
+ int json_object_array_add(struct json_object *this,struct json_object *val)
496
+ {
497
+ return array_list_add(this->o.c_array, val);
498
+ }
499
+
500
+ int json_object_array_put_idx(struct json_object *this, int idx,
501
+ struct json_object *val)
502
+ {
503
+ return array_list_put_idx(this->o.c_array, idx, val);
504
+ }
505
+
506
+ struct json_object* json_object_array_get_idx(struct json_object *this,
507
+ int idx)
508
+ {
509
+ return (struct json_object*)array_list_get_idx(this->o.c_array, idx);
510
+ }
511
+