json 1.8.1 → 1.8.6

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of json might be problematic. Click here for more details.

Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +4 -0
  3. data/.travis.yml +10 -12
  4. data/CHANGES +23 -0
  5. data/Gemfile +1 -5
  6. data/{README.rdoc → README.md} +34 -47
  7. data/Rakefile +10 -20
  8. data/VERSION +1 -1
  9. data/ext/json/ext/fbuffer/fbuffer.h +10 -1
  10. data/ext/json/ext/generator/extconf.rb +0 -10
  11. data/ext/json/ext/generator/generator.c +85 -28
  12. data/ext/json/ext/generator/generator.h +34 -5
  13. data/ext/json/ext/parser/extconf.rb +0 -10
  14. data/ext/json/ext/parser/parser.c +166 -130
  15. data/ext/json/ext/parser/parser.h +19 -4
  16. data/ext/json/ext/parser/parser.rl +84 -48
  17. data/ext/json/extconf.rb +3 -0
  18. data/java/src/json/ext/ByteListTranscoder.java +1 -2
  19. data/java/src/json/ext/Generator.java +9 -7
  20. data/java/src/json/ext/GeneratorMethods.java +1 -2
  21. data/java/src/json/ext/GeneratorService.java +1 -2
  22. data/java/src/json/ext/GeneratorState.java +1 -2
  23. data/java/src/json/ext/OptionsReader.java +1 -2
  24. data/java/src/json/ext/Parser.java +87 -87
  25. data/java/src/json/ext/Parser.rl +7 -7
  26. data/java/src/json/ext/ParserService.java +1 -2
  27. data/java/src/json/ext/RuntimeInfo.java +1 -2
  28. data/java/src/json/ext/StringDecoder.java +1 -2
  29. data/java/src/json/ext/StringEncoder.java +5 -0
  30. data/java/src/json/ext/Utils.java +1 -2
  31. data/json-java.gemspec +16 -1
  32. data/json.gemspec +0 -0
  33. data/json_pure.gemspec +23 -26
  34. data/lib/json/add/complex.rb +7 -1
  35. data/lib/json/add/rational.rb +5 -0
  36. data/lib/json/add/time.rb +1 -1
  37. data/lib/json/common.rb +5 -5
  38. data/lib/json/pure/generator.rb +10 -2
  39. data/lib/json/version.rb +1 -1
  40. data/tests/test_json.rb +8 -34
  41. data/tests/test_json_generate.rb +34 -8
  42. data/tools/diff.sh +18 -0
  43. metadata +39 -39
  44. data/COPYING +0 -58
  45. data/COPYING-json-jruby +0 -57
  46. data/GPL +0 -340
@@ -7,7 +7,13 @@ static ID i_encoding, i_encode;
7
7
  #endif
8
8
 
9
9
  static VALUE mJSON, mExt, mGenerator, cState, mGeneratorMethods, mObject,
10
- mHash, mArray, mFixnum, mBignum, mFloat, mString, mString_Extend,
10
+ mHash, mArray,
11
+ #ifdef RUBY_INTEGER_UNIFICATION
12
+ mInteger,
13
+ #else
14
+ mFixnum, mBignum,
15
+ #endif
16
+ mFloat, mString, mString_Extend,
11
17
  mTrueClass, mFalseClass, mNilClass, eGeneratorError,
12
18
  eNestingError, CRegexp_MULTILINE, CJSON_SAFE_STATE_PROTOTYPE,
13
19
  i_SAFE_STATE_PROTOTYPE;
@@ -342,6 +348,18 @@ static VALUE mArray_to_json(int argc, VALUE *argv, VALUE self) {
342
348
  GENERATE_JSON(array);
343
349
  }
344
350
 
351
+ #ifdef RUBY_INTEGER_UNIFICATION
352
+ /*
353
+ * call-seq: to_json(*)
354
+ *
355
+ * Returns a JSON string representation for this Integer number.
356
+ */
357
+ static VALUE mInteger_to_json(int argc, VALUE *argv, VALUE self)
358
+ {
359
+ GENERATE_JSON(integer);
360
+ }
361
+
362
+ #else
345
363
  /*
346
364
  * call-seq: to_json(*)
347
365
  *
@@ -361,6 +379,7 @@ static VALUE mBignum_to_json(int argc, VALUE *argv, VALUE self)
361
379
  {
362
380
  GENERATE_JSON(bignum);
363
381
  }
382
+ #endif
364
383
 
365
384
  /*
366
385
  * call-seq: to_json(*)
@@ -486,8 +505,9 @@ static VALUE mObject_to_json(int argc, VALUE *argv, VALUE self)
486
505
  return cState_partial_generate(state, string);
487
506
  }
488
507
 
489
- static void State_free(JSON_Generator_State *state)
508
+ static void State_free(void *ptr)
490
509
  {
510
+ JSON_Generator_State *state = ptr;
491
511
  if (state->indent) ruby_xfree(state->indent);
492
512
  if (state->space) ruby_xfree(state->space);
493
513
  if (state->space_before) ruby_xfree(state->space_before);
@@ -499,17 +519,37 @@ static void State_free(JSON_Generator_State *state)
499
519
  ruby_xfree(state);
500
520
  }
501
521
 
502
- static JSON_Generator_State *State_allocate()
503
- {
504
- JSON_Generator_State *state = ALLOC(JSON_Generator_State);
505
- MEMZERO(state, JSON_Generator_State, 1);
506
- return state;
507
- }
522
+ static size_t State_memsize(const void *ptr)
523
+ {
524
+ const JSON_Generator_State *state = ptr;
525
+ size_t size = sizeof(*state);
526
+ if (state->indent) size += state->indent_len + 1;
527
+ if (state->space) size += state->space_len + 1;
528
+ if (state->space_before) size += state->space_before_len + 1;
529
+ if (state->object_nl) size += state->object_nl_len + 1;
530
+ if (state->array_nl) size += state->array_nl_len + 1;
531
+ if (state->array_delim) size += FBUFFER_CAPA(state->array_delim);
532
+ if (state->object_delim) size += FBUFFER_CAPA(state->object_delim);
533
+ if (state->object_delim2) size += FBUFFER_CAPA(state->object_delim2);
534
+ return size;
535
+ }
536
+
537
+ #ifdef NEW_TYPEDDATA_WRAPPER
538
+ static const rb_data_type_t JSON_Generator_State_type = {
539
+ "JSON/Generator/State",
540
+ {NULL, State_free, State_memsize,},
541
+ #ifdef RUBY_TYPED_FREE_IMMEDIATELY
542
+ 0, 0,
543
+ RUBY_TYPED_FREE_IMMEDIATELY,
544
+ #endif
545
+ };
546
+ #endif
508
547
 
509
548
  static VALUE cState_s_allocate(VALUE klass)
510
549
  {
511
- JSON_Generator_State *state = State_allocate();
512
- return Data_Wrap_Struct(klass, NULL, State_free, state);
550
+ JSON_Generator_State *state;
551
+ return TypedData_Make_Struct(klass, JSON_Generator_State,
552
+ &JSON_Generator_State_type, state);
513
553
  }
514
554
 
515
555
  /*
@@ -646,7 +686,7 @@ static VALUE cState_to_h(VALUE self)
646
686
  /*
647
687
  * call-seq: [](name)
648
688
  *
649
- * Return the value returned by method +name+.
689
+ * Returns the value returned by method +name+.
650
690
  */
651
691
  static VALUE cState_aref(VALUE self, VALUE name)
652
692
  {
@@ -661,7 +701,7 @@ static VALUE cState_aref(VALUE self, VALUE name)
661
701
  /*
662
702
  * call-seq: []=(name, value)
663
703
  *
664
- * Set the attribute name to value.
704
+ * Sets the attribute name to value.
665
705
  */
666
706
  static VALUE cState_aset(VALUE self, VALUE name, VALUE value)
667
707
  {
@@ -804,6 +844,16 @@ static void generate_json_bignum(FBuffer *buffer, VALUE Vstate, JSON_Generator_S
804
844
  fbuffer_append_str(buffer, tmp);
805
845
  }
806
846
 
847
+ #ifdef RUBY_INTEGER_UNIFICATION
848
+ static void generate_json_integer(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
849
+ {
850
+ if (FIXNUM_P(obj))
851
+ generate_json_fixnum(buffer, Vstate, state, obj);
852
+ else
853
+ generate_json_bignum(buffer, Vstate, state, obj);
854
+ }
855
+ #endif
856
+
807
857
  static void generate_json_float(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
808
858
  {
809
859
  double value = RFLOAT_VALUE(obj);
@@ -812,10 +862,10 @@ static void generate_json_float(FBuffer *buffer, VALUE Vstate, JSON_Generator_St
812
862
  if (!allow_nan) {
813
863
  if (isinf(value)) {
814
864
  fbuffer_free(buffer);
815
- rb_raise(eGeneratorError, "%u: %s not allowed in JSON", __LINE__, StringValueCStr(tmp));
865
+ rb_raise(eGeneratorError, "%u: %"PRIsVALUE" not allowed in JSON", __LINE__, RB_OBJ_STRING(tmp));
816
866
  } else if (isnan(value)) {
817
867
  fbuffer_free(buffer);
818
- rb_raise(eGeneratorError, "%u: %s not allowed in JSON", __LINE__, StringValueCStr(tmp));
868
+ rb_raise(eGeneratorError, "%u: %"PRIsVALUE" not allowed in JSON", __LINE__, RB_OBJ_STRING(tmp));
819
869
  }
820
870
  }
821
871
  fbuffer_append_str(buffer, tmp);
@@ -837,9 +887,9 @@ static void generate_json(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *s
837
887
  generate_json_false(buffer, Vstate, state, obj);
838
888
  } else if (obj == Qtrue) {
839
889
  generate_json_true(buffer, Vstate, state, obj);
840
- } else if (klass == rb_cFixnum) {
890
+ } else if (FIXNUM_P(obj)) {
841
891
  generate_json_fixnum(buffer, Vstate, state, obj);
842
- } else if (klass == rb_cBignum) {
892
+ } else if (RB_TYPE_P(obj, T_BIGNUM)) {
843
893
  generate_json_bignum(buffer, Vstate, state, obj);
844
894
  } else if (klass == rb_cFloat) {
845
895
  generate_json_float(buffer, Vstate, state, obj);
@@ -850,7 +900,7 @@ static void generate_json(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *s
850
900
  } else {
851
901
  tmp = rb_funcall(obj, i_to_s, 0);
852
902
  Check_Type(tmp, T_STRING);
853
- generate_json(buffer, Vstate, state, tmp);
903
+ generate_json_string(buffer, Vstate, state, tmp);
854
904
  }
855
905
  }
856
906
 
@@ -871,6 +921,7 @@ static FBuffer *cState_prepare_buffer(VALUE self)
871
921
  } else {
872
922
  state->object_delim2 = fbuffer_alloc(16);
873
923
  }
924
+ if (state->space_before) fbuffer_append(state->object_delim2, state->space_before, state->space_before_len);
874
925
  fbuffer_append_char(state->object_delim2, ':');
875
926
  if (state->space) fbuffer_append(state->object_delim2, state->space, state->space_len);
876
927
 
@@ -958,15 +1009,16 @@ static VALUE cState_initialize(int argc, VALUE *argv, VALUE self)
958
1009
  /*
959
1010
  * call-seq: initialize_copy(orig)
960
1011
  *
961
- * Initializes this object from orig if it to be duplicated/cloned and returns
1012
+ * Initializes this object from orig if it can be duplicated/cloned and returns
962
1013
  * it.
963
1014
  */
964
1015
  static VALUE cState_init_copy(VALUE obj, VALUE orig)
965
1016
  {
966
1017
  JSON_Generator_State *objState, *origState;
967
1018
 
968
- Data_Get_Struct(obj, JSON_Generator_State, objState);
969
- Data_Get_Struct(orig, JSON_Generator_State, origState);
1019
+ if (obj == orig) return obj;
1020
+ GET_STATE_TO(obj, objState);
1021
+ GET_STATE_TO(orig, origState);
970
1022
  if (!objState) rb_raise(rb_eArgError, "unallocated JSON::State");
971
1023
 
972
1024
  MEMCPY(objState, origState, JSON_Generator_State, 1);
@@ -1005,7 +1057,7 @@ static VALUE cState_from_state_s(VALUE self, VALUE opts)
1005
1057
  /*
1006
1058
  * call-seq: indent()
1007
1059
  *
1008
- * This string is used to indent levels in the JSON text.
1060
+ * Returns the string that is used to indent levels in the JSON text.
1009
1061
  */
1010
1062
  static VALUE cState_indent(VALUE self)
1011
1063
  {
@@ -1016,7 +1068,7 @@ static VALUE cState_indent(VALUE self)
1016
1068
  /*
1017
1069
  * call-seq: indent=(indent)
1018
1070
  *
1019
- * This string is used to indent levels in the JSON text.
1071
+ * Sets the string that is used to indent levels in the JSON text.
1020
1072
  */
1021
1073
  static VALUE cState_indent_set(VALUE self, VALUE indent)
1022
1074
  {
@@ -1041,7 +1093,7 @@ static VALUE cState_indent_set(VALUE self, VALUE indent)
1041
1093
  /*
1042
1094
  * call-seq: space()
1043
1095
  *
1044
- * This string is used to insert a space between the tokens in a JSON
1096
+ * Returns the string that is used to insert a space between the tokens in a JSON
1045
1097
  * string.
1046
1098
  */
1047
1099
  static VALUE cState_space(VALUE self)
@@ -1053,7 +1105,7 @@ static VALUE cState_space(VALUE self)
1053
1105
  /*
1054
1106
  * call-seq: space=(space)
1055
1107
  *
1056
- * This string is used to insert a space between the tokens in a JSON
1108
+ * Sets _space_ to the string that is used to insert a space between the tokens in a JSON
1057
1109
  * string.
1058
1110
  */
1059
1111
  static VALUE cState_space_set(VALUE self, VALUE space)
@@ -1079,7 +1131,7 @@ static VALUE cState_space_set(VALUE self, VALUE space)
1079
1131
  /*
1080
1132
  * call-seq: space_before()
1081
1133
  *
1082
- * This string is used to insert a space before the ':' in JSON objects.
1134
+ * Returns the string that is used to insert a space before the ':' in JSON objects.
1083
1135
  */
1084
1136
  static VALUE cState_space_before(VALUE self)
1085
1137
  {
@@ -1090,7 +1142,7 @@ static VALUE cState_space_before(VALUE self)
1090
1142
  /*
1091
1143
  * call-seq: space_before=(space_before)
1092
1144
  *
1093
- * This string is used to insert a space before the ':' in JSON objects.
1145
+ * Sets the string that is used to insert a space before the ':' in JSON objects.
1094
1146
  */
1095
1147
  static VALUE cState_space_before_set(VALUE self, VALUE space_before)
1096
1148
  {
@@ -1297,7 +1349,7 @@ static VALUE cState_depth_set(VALUE self, VALUE depth)
1297
1349
  /*
1298
1350
  * call-seq: buffer_initial_length
1299
1351
  *
1300
- * This integer returns the current inital length of the buffer.
1352
+ * This integer returns the current initial length of the buffer.
1301
1353
  */
1302
1354
  static VALUE cState_buffer_initial_length(VALUE self)
1303
1355
  {
@@ -1326,7 +1378,7 @@ static VALUE cState_buffer_initial_length_set(VALUE self, VALUE buffer_initial_l
1326
1378
  /*
1327
1379
  *
1328
1380
  */
1329
- void Init_generator()
1381
+ void Init_generator(void)
1330
1382
  {
1331
1383
  rb_require("json/common");
1332
1384
 
@@ -1379,10 +1431,15 @@ void Init_generator()
1379
1431
  rb_define_method(mHash, "to_json", mHash_to_json, -1);
1380
1432
  mArray = rb_define_module_under(mGeneratorMethods, "Array");
1381
1433
  rb_define_method(mArray, "to_json", mArray_to_json, -1);
1434
+ #ifdef RUBY_INTEGER_UNIFICATION
1435
+ mInteger = rb_define_module_under(mGeneratorMethods, "Integer");
1436
+ rb_define_method(mInteger, "to_json", mInteger_to_json, -1);
1437
+ #else
1382
1438
  mFixnum = rb_define_module_under(mGeneratorMethods, "Fixnum");
1383
1439
  rb_define_method(mFixnum, "to_json", mFixnum_to_json, -1);
1384
1440
  mBignum = rb_define_module_under(mGeneratorMethods, "Bignum");
1385
1441
  rb_define_method(mBignum, "to_json", mBignum_to_json, -1);
1442
+ #endif
1386
1443
  mFloat = rb_define_module_under(mGeneratorMethods, "Float");
1387
1444
  rb_define_method(mFloat, "to_json", mFloat_to_json, -1);
1388
1445
  mString = rb_define_module_under(mGeneratorMethods, "String");
@@ -21,9 +21,13 @@
21
21
  #define rb_obj_instance_variables(object) rb_funcall(object, rb_intern("instance_variables"), 0)
22
22
  #endif
23
23
 
24
+ #ifndef RB_TYPE_P
25
+ #define RB_TYPE_P(obj, type) (rb_type(obj) == type)
26
+ #endif
27
+
24
28
  #define option_given_p(opts, key) RTEST(rb_funcall(opts, i_key_p, 1, key))
25
29
 
26
- /* unicode defintions */
30
+ /* unicode definitions */
27
31
 
28
32
  #define UNI_STRICT_CONVERSION 1
29
33
 
@@ -78,9 +82,12 @@ typedef struct JSON_Generator_StateStruct {
78
82
  long buffer_initial_length;
79
83
  } JSON_Generator_State;
80
84
 
85
+ #define GET_STATE_TO(self, state) \
86
+ TypedData_Get_Struct(self, JSON_Generator_State, &JSON_Generator_State_type, state)
87
+
81
88
  #define GET_STATE(self) \
82
89
  JSON_Generator_State *state; \
83
- Data_Get_Struct(self, JSON_Generator_State, state)
90
+ GET_STATE_TO(self, state)
84
91
 
85
92
  #define GENERATE_JSON(type) \
86
93
  FBuffer *buffer; \
@@ -89,15 +96,19 @@ typedef struct JSON_Generator_StateStruct {
89
96
  \
90
97
  rb_scan_args(argc, argv, "01", &Vstate); \
91
98
  Vstate = cState_from_state_s(cState, Vstate); \
92
- Data_Get_Struct(Vstate, JSON_Generator_State, state); \
99
+ TypedData_Get_Struct(Vstate, JSON_Generator_State, &JSON_Generator_State_type, state); \
93
100
  buffer = cState_prepare_buffer(Vstate); \
94
101
  generate_json_##type(buffer, Vstate, state, self); \
95
102
  return fbuffer_to_s(buffer)
96
103
 
97
104
  static VALUE mHash_to_json(int argc, VALUE *argv, VALUE self);
98
105
  static VALUE mArray_to_json(int argc, VALUE *argv, VALUE self);
106
+ #ifdef RUBY_INTEGER_UNIFICATION
107
+ static VALUE mInteger_to_json(int argc, VALUE *argv, VALUE self);
108
+ #else
99
109
  static VALUE mFixnum_to_json(int argc, VALUE *argv, VALUE self);
100
110
  static VALUE mBignum_to_json(int argc, VALUE *argv, VALUE self);
111
+ #endif
101
112
  static VALUE mFloat_to_json(int argc, VALUE *argv, VALUE self);
102
113
  static VALUE mString_included_s(VALUE self, VALUE modul);
103
114
  static VALUE mString_to_json(int argc, VALUE *argv, VALUE self);
@@ -108,8 +119,7 @@ static VALUE mTrueClass_to_json(int argc, VALUE *argv, VALUE self);
108
119
  static VALUE mFalseClass_to_json(int argc, VALUE *argv, VALUE self);
109
120
  static VALUE mNilClass_to_json(int argc, VALUE *argv, VALUE self);
110
121
  static VALUE mObject_to_json(int argc, VALUE *argv, VALUE self);
111
- static void State_free(JSON_Generator_State *state);
112
- static JSON_Generator_State *State_allocate();
122
+ static void State_free(void *state);
113
123
  static VALUE cState_s_allocate(VALUE klass);
114
124
  static VALUE cState_configure(VALUE self, VALUE opts);
115
125
  static VALUE cState_to_h(VALUE self);
@@ -120,6 +130,9 @@ static void generate_json_string(FBuffer *buffer, VALUE Vstate, JSON_Generator_S
120
130
  static void generate_json_null(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
121
131
  static void generate_json_false(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
122
132
  static void generate_json_true(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
133
+ #ifdef RUBY_INTEGER_UNIFICATION
134
+ static void generate_json_integer(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
135
+ #endif
123
136
  static void generate_json_fixnum(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
124
137
  static void generate_json_bignum(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
125
138
  static void generate_json_float(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
@@ -144,5 +157,21 @@ static VALUE cState_ascii_only_p(VALUE self);
144
157
  static VALUE cState_depth(VALUE self);
145
158
  static VALUE cState_depth_set(VALUE self, VALUE depth);
146
159
  static FBuffer *cState_prepare_buffer(VALUE self);
160
+ #ifndef ZALLOC
161
+ #define ZALLOC(type) ((type *)ruby_zalloc(sizeof(type)))
162
+ static inline void *ruby_zalloc(size_t n)
163
+ {
164
+ void *p = ruby_xmalloc(n);
165
+ memset(p, 0, n);
166
+ return p;
167
+ }
168
+ #endif
169
+ #ifdef TypedData_Make_Struct
170
+ static const rb_data_type_t JSON_Generator_State_type;
171
+ #define NEW_TYPEDDATA_WRAPPER 1
172
+ #else
173
+ #define TypedData_Make_Struct(klass, type, ignore, json) Data_Make_Struct(klass, type, NULL, State_free, json)
174
+ #define TypedData_Get_Struct(self, JSON_Generator_State, ignore, json) Data_Get_Struct(self, JSON_Generator_State, json)
175
+ #endif
147
176
 
148
177
  #endif
@@ -1,13 +1,3 @@
1
1
  require 'mkmf'
2
2
 
3
- unless $CFLAGS.gsub!(/ -O[\dsz]?/, ' -O3')
4
- $CFLAGS << ' -O3'
5
- end
6
- if CONFIG['CC'] =~ /gcc/
7
- $CFLAGS << ' -Wall'
8
- if $DEBUG && !$CFLAGS.gsub!(/ -O[\dsz]?/, ' -O0 -ggdb')
9
- $CFLAGS << ' -O0 -ggdb'
10
- end
11
- end
12
-
13
3
  create_makefile 'json/ext/parser'