json 1.8.3 → 2.1.0

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 (78) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.travis.yml +3 -10
  4. data/{CHANGES → CHANGES.md} +183 -90
  5. data/Gemfile +11 -5
  6. data/{README-json-jruby.markdown → README-json-jruby.md} +0 -0
  7. data/{README.rdoc → README.md} +147 -113
  8. data/Rakefile +32 -36
  9. data/VERSION +1 -1
  10. data/ext/json/ext/fbuffer/fbuffer.h +0 -3
  11. data/ext/json/ext/generator/generator.c +46 -61
  12. data/ext/json/ext/generator/generator.h +7 -2
  13. data/ext/json/ext/parser/extconf.rb +3 -0
  14. data/ext/json/ext/parser/parser.c +353 -460
  15. data/ext/json/ext/parser/parser.h +4 -5
  16. data/ext/json/ext/parser/parser.rl +111 -181
  17. data/ext/json/extconf.rb +0 -1
  18. data/java/src/json/ext/ByteListTranscoder.java +1 -2
  19. data/java/src/json/ext/Generator.java +11 -12
  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 +3 -56
  23. data/java/src/json/ext/OptionsReader.java +2 -3
  24. data/java/src/json/ext/Parser.java +135 -418
  25. data/java/src/json/ext/Parser.rl +48 -124
  26. data/java/src/json/ext/ParserService.java +1 -2
  27. data/java/src/json/ext/RuntimeInfo.java +1 -6
  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 +15 -0
  32. data/json.gemspec +0 -0
  33. data/json_pure.gemspec +24 -26
  34. data/lib/json/add/bigdecimal.rb +1 -0
  35. data/lib/json/add/complex.rb +2 -1
  36. data/lib/json/add/core.rb +1 -0
  37. data/lib/json/add/date.rb +1 -1
  38. data/lib/json/add/date_time.rb +1 -1
  39. data/lib/json/add/exception.rb +1 -1
  40. data/lib/json/add/ostruct.rb +2 -2
  41. data/lib/json/add/range.rb +1 -1
  42. data/lib/json/add/rational.rb +1 -0
  43. data/lib/json/add/regexp.rb +1 -1
  44. data/lib/json/add/struct.rb +1 -1
  45. data/lib/json/add/symbol.rb +1 -1
  46. data/lib/json/add/time.rb +1 -1
  47. data/lib/json/common.rb +24 -52
  48. data/lib/json/ext.rb +0 -6
  49. data/lib/json/generic_object.rb +5 -4
  50. data/lib/json/pure/generator.rb +61 -125
  51. data/lib/json/pure/parser.rb +33 -81
  52. data/lib/json/pure.rb +2 -8
  53. data/lib/json/version.rb +2 -1
  54. data/lib/json.rb +1 -0
  55. data/references/rfc7159.txt +899 -0
  56. data/tests/fixtures/obsolete_fail1.json +1 -0
  57. data/tests/{test_json_addition.rb → json_addition_test.rb} +22 -25
  58. data/tests/json_common_interface_test.rb +126 -0
  59. data/tests/json_encoding_test.rb +107 -0
  60. data/tests/json_ext_parser_test.rb +15 -0
  61. data/tests/{test_json_fixtures.rb → json_fixtures_test.rb} +5 -8
  62. data/tests/{test_json_generate.rb → json_generator_test.rb} +79 -39
  63. data/tests/{test_json_generic_object.rb → json_generic_object_test.rb} +15 -8
  64. data/tests/json_parser_test.rb +471 -0
  65. data/tests/json_string_matching_test.rb +38 -0
  66. data/tests/{setup_variant.rb → test_helper.rb} +10 -0
  67. data/tools/diff.sh +18 -0
  68. data/tools/fuzz.rb +1 -9
  69. metadata +28 -43
  70. data/COPYING +0 -58
  71. data/COPYING-json-jruby +0 -57
  72. data/GPL +0 -340
  73. data/TODO +0 -1
  74. data/tests/fixtures/fail1.json +0 -1
  75. data/tests/test_json.rb +0 -553
  76. data/tests/test_json_encoding.rb +0 -65
  77. data/tests/test_json_string_matching.rb +0 -39
  78. data/tests/test_json_unicode.rb +0 -72
@@ -7,14 +7,20 @@ 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;
14
20
 
15
21
  static ID i_to_s, i_to_json, i_new, i_indent, i_space, i_space_before,
16
22
  i_object_nl, i_array_nl, i_max_nesting, i_allow_nan, i_ascii_only,
17
- i_quirks_mode, i_pack, i_unpack, i_create_id, i_extend, i_key_p,
23
+ i_pack, i_unpack, i_create_id, i_extend, i_key_p,
18
24
  i_aref, i_send, i_respond_to_p, i_match, i_keys, i_depth,
19
25
  i_buffer_initial_length, i_dup;
20
26
 
@@ -216,6 +222,7 @@ static void convert_UTF8_to_JSON_ASCII(FBuffer *buffer, VALUE string)
216
222
  unicode_escape_to_buffer(buffer, buf, (UTF16)((ch & halfMask) + UNI_SUR_LOW_START));
217
223
  }
218
224
  }
225
+ RB_GC_GUARD(string);
219
226
  }
220
227
 
221
228
  /* Converts string to a JSON string in FBuffer buffer, where only the
@@ -301,7 +308,7 @@ static char *fstrndup(const char *ptr, unsigned long len) {
301
308
  char *result;
302
309
  if (len <= 0) return NULL;
303
310
  result = ALLOC_N(char, len);
304
- memccpy(result, ptr, 0, len);
311
+ memcpy(result, ptr, len);
305
312
  return result;
306
313
  }
307
314
 
@@ -342,6 +349,18 @@ static VALUE mArray_to_json(int argc, VALUE *argv, VALUE self) {
342
349
  GENERATE_JSON(array);
343
350
  }
344
351
 
352
+ #ifdef RUBY_INTEGER_UNIFICATION
353
+ /*
354
+ * call-seq: to_json(*)
355
+ *
356
+ * Returns a JSON string representation for this Integer number.
357
+ */
358
+ static VALUE mInteger_to_json(int argc, VALUE *argv, VALUE self)
359
+ {
360
+ GENERATE_JSON(integer);
361
+ }
362
+
363
+ #else
345
364
  /*
346
365
  * call-seq: to_json(*)
347
366
  *
@@ -361,6 +380,7 @@ static VALUE mBignum_to_json(int argc, VALUE *argv, VALUE self)
361
380
  {
362
381
  GENERATE_JSON(bignum);
363
382
  }
383
+ #endif
364
384
 
365
385
  /*
366
386
  * call-seq: to_json(*)
@@ -622,8 +642,6 @@ static VALUE cState_configure(VALUE self, VALUE opts)
622
642
  state->allow_nan = RTEST(tmp);
623
643
  tmp = rb_hash_aref(opts, ID2SYM(i_ascii_only));
624
644
  state->ascii_only = RTEST(tmp);
625
- tmp = rb_hash_aref(opts, ID2SYM(i_quirks_mode));
626
- state->quirks_mode = RTEST(tmp);
627
645
  return self;
628
646
  }
629
647
 
@@ -657,7 +675,6 @@ static VALUE cState_to_h(VALUE self)
657
675
  rb_hash_aset(result, ID2SYM(i_array_nl), rb_str_new(state->array_nl, state->array_nl_len));
658
676
  rb_hash_aset(result, ID2SYM(i_allow_nan), state->allow_nan ? Qtrue : Qfalse);
659
677
  rb_hash_aset(result, ID2SYM(i_ascii_only), state->ascii_only ? Qtrue : Qfalse);
660
- rb_hash_aset(result, ID2SYM(i_quirks_mode), state->quirks_mode ? Qtrue : Qfalse);
661
678
  rb_hash_aset(result, ID2SYM(i_max_nesting), LONG2FIX(state->max_nesting));
662
679
  rb_hash_aset(result, ID2SYM(i_depth), LONG2FIX(state->depth));
663
680
  rb_hash_aset(result, ID2SYM(i_buffer_initial_length), LONG2FIX(state->buffer_initial_length));
@@ -825,6 +842,15 @@ static void generate_json_bignum(FBuffer *buffer, VALUE Vstate, JSON_Generator_S
825
842
  fbuffer_append_str(buffer, tmp);
826
843
  }
827
844
 
845
+ #ifdef RUBY_INTEGER_UNIFICATION
846
+ static void generate_json_integer(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
847
+ {
848
+ if (FIXNUM_P(obj))
849
+ generate_json_fixnum(buffer, Vstate, state, obj);
850
+ else
851
+ generate_json_bignum(buffer, Vstate, state, obj);
852
+ }
853
+ #endif
828
854
  static void generate_json_float(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
829
855
  {
830
856
  double value = RFLOAT_VALUE(obj);
@@ -858,9 +884,9 @@ static void generate_json(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *s
858
884
  generate_json_false(buffer, Vstate, state, obj);
859
885
  } else if (obj == Qtrue) {
860
886
  generate_json_true(buffer, Vstate, state, obj);
861
- } else if (klass == rb_cFixnum) {
887
+ } else if (FIXNUM_P(obj)) {
862
888
  generate_json_fixnum(buffer, Vstate, state, obj);
863
- } else if (klass == rb_cBignum) {
889
+ } else if (RB_TYPE_P(obj, T_BIGNUM)) {
864
890
  generate_json_bignum(buffer, Vstate, state, obj);
865
891
  } else if (klass == rb_cFloat) {
866
892
  generate_json_float(buffer, Vstate, state, obj);
@@ -871,7 +897,7 @@ static void generate_json(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *s
871
897
  } else {
872
898
  tmp = rb_funcall(obj, i_to_s, 0);
873
899
  Check_Type(tmp, T_STRING);
874
- generate_json(buffer, Vstate, state, tmp);
900
+ generate_json_string(buffer, Vstate, state, tmp);
875
901
  }
876
902
  }
877
903
 
@@ -914,21 +940,6 @@ static VALUE cState_partial_generate(VALUE self, VALUE obj)
914
940
  return fbuffer_to_s(buffer);
915
941
  }
916
942
 
917
- /*
918
- * This function returns true if string is either a JSON array or JSON object.
919
- * It might suffer from false positives, e. g. syntactically incorrect JSON in
920
- * the string or certain UTF-8 characters on the right hand side.
921
- */
922
- static int isArrayOrObject(VALUE string)
923
- {
924
- long string_len = RSTRING_LEN(string);
925
- char *p = RSTRING_PTR(string), *q = p + string_len - 1;
926
- if (string_len < 2) return 0;
927
- for (; p < q && isspace((unsigned char)*p); p++);
928
- for (; q > p && isspace((unsigned char)*q); q--);
929
- return (*p == '[' && *q == ']') || (*p == '{' && *q == '}');
930
- }
931
-
932
943
  /*
933
944
  * call-seq: generate(obj)
934
945
  *
@@ -940,9 +951,7 @@ static VALUE cState_generate(VALUE self, VALUE obj)
940
951
  {
941
952
  VALUE result = cState_partial_generate(self, obj);
942
953
  GET_STATE(self);
943
- if (!state->quirks_mode && !isArrayOrObject(result)) {
944
- rb_raise(eGeneratorError, "only generation of JSON objects or arrays allowed");
945
- }
954
+ (void)state;
946
955
  return result;
947
956
  }
948
957
 
@@ -961,8 +970,6 @@ static VALUE cState_generate(VALUE self, VALUE obj)
961
970
  * * *allow_nan*: true if NaN, Infinity, and -Infinity should be
962
971
  * generated, otherwise an exception is thrown, if these values are
963
972
  * encountered. This options defaults to false.
964
- * * *quirks_mode*: Enables quirks_mode for parser, that is for example
965
- * generating single JSON values instead of documents is possible.
966
973
  * * *buffer_initial_length*: sets the initial length of the generator's
967
974
  * internal buffer.
968
975
  */
@@ -1055,7 +1062,7 @@ static VALUE cState_indent_set(VALUE self, VALUE indent)
1055
1062
  }
1056
1063
  } else {
1057
1064
  if (state->indent) ruby_xfree(state->indent);
1058
- state->indent = strdup(RSTRING_PTR(indent));
1065
+ state->indent = fstrndup(RSTRING_PTR(indent), len);
1059
1066
  state->indent_len = len;
1060
1067
  }
1061
1068
  return Qnil;
@@ -1093,7 +1100,7 @@ static VALUE cState_space_set(VALUE self, VALUE space)
1093
1100
  }
1094
1101
  } else {
1095
1102
  if (state->space) ruby_xfree(state->space);
1096
- state->space = strdup(RSTRING_PTR(space));
1103
+ state->space = fstrndup(RSTRING_PTR(space), len);
1097
1104
  state->space_len = len;
1098
1105
  }
1099
1106
  return Qnil;
@@ -1129,7 +1136,7 @@ static VALUE cState_space_before_set(VALUE self, VALUE space_before)
1129
1136
  }
1130
1137
  } else {
1131
1138
  if (state->space_before) ruby_xfree(state->space_before);
1132
- state->space_before = strdup(RSTRING_PTR(space_before));
1139
+ state->space_before = fstrndup(RSTRING_PTR(space_before), len);
1133
1140
  state->space_before_len = len;
1134
1141
  }
1135
1142
  return Qnil;
@@ -1166,7 +1173,7 @@ static VALUE cState_object_nl_set(VALUE self, VALUE object_nl)
1166
1173
  }
1167
1174
  } else {
1168
1175
  if (state->object_nl) ruby_xfree(state->object_nl);
1169
- state->object_nl = strdup(RSTRING_PTR(object_nl));
1176
+ state->object_nl = fstrndup(RSTRING_PTR(object_nl), len);
1170
1177
  state->object_nl_len = len;
1171
1178
  }
1172
1179
  return Qnil;
@@ -1201,7 +1208,7 @@ static VALUE cState_array_nl_set(VALUE self, VALUE array_nl)
1201
1208
  }
1202
1209
  } else {
1203
1210
  if (state->array_nl) ruby_xfree(state->array_nl);
1204
- state->array_nl = strdup(RSTRING_PTR(array_nl));
1211
+ state->array_nl = fstrndup(RSTRING_PTR(array_nl), len);
1205
1212
  state->array_nl_len = len;
1206
1213
  }
1207
1214
  return Qnil;
@@ -1269,29 +1276,6 @@ static VALUE cState_ascii_only_p(VALUE self)
1269
1276
  return state->ascii_only ? Qtrue : Qfalse;
1270
1277
  }
1271
1278
 
1272
- /*
1273
- * call-seq: quirks_mode?
1274
- *
1275
- * Returns true, if quirks mode is enabled. Otherwise returns false.
1276
- */
1277
- static VALUE cState_quirks_mode_p(VALUE self)
1278
- {
1279
- GET_STATE(self);
1280
- return state->quirks_mode ? Qtrue : Qfalse;
1281
- }
1282
-
1283
- /*
1284
- * call-seq: quirks_mode=(enable)
1285
- *
1286
- * If set to true, enables the quirks_mode mode.
1287
- */
1288
- static VALUE cState_quirks_mode_set(VALUE self, VALUE enable)
1289
- {
1290
- GET_STATE(self);
1291
- state->quirks_mode = RTEST(enable);
1292
- return Qnil;
1293
- }
1294
-
1295
1279
  /*
1296
1280
  * call-seq: depth
1297
1281
  *
@@ -1380,9 +1364,6 @@ void Init_generator(void)
1380
1364
  rb_define_method(cState, "check_circular?", cState_check_circular_p, 0);
1381
1365
  rb_define_method(cState, "allow_nan?", cState_allow_nan_p, 0);
1382
1366
  rb_define_method(cState, "ascii_only?", cState_ascii_only_p, 0);
1383
- rb_define_method(cState, "quirks_mode?", cState_quirks_mode_p, 0);
1384
- rb_define_method(cState, "quirks_mode", cState_quirks_mode_p, 0);
1385
- rb_define_method(cState, "quirks_mode=", cState_quirks_mode_set, 1);
1386
1367
  rb_define_method(cState, "depth", cState_depth, 0);
1387
1368
  rb_define_method(cState, "depth=", cState_depth_set, 1);
1388
1369
  rb_define_method(cState, "buffer_initial_length", cState_buffer_initial_length, 0);
@@ -1402,10 +1383,15 @@ void Init_generator(void)
1402
1383
  rb_define_method(mHash, "to_json", mHash_to_json, -1);
1403
1384
  mArray = rb_define_module_under(mGeneratorMethods, "Array");
1404
1385
  rb_define_method(mArray, "to_json", mArray_to_json, -1);
1386
+ #ifdef RUBY_INTEGER_UNIFICATION
1387
+ mInteger = rb_define_module_under(mGeneratorMethods, "Integer");
1388
+ rb_define_method(mInteger, "to_json", mInteger_to_json, -1);
1389
+ #else
1405
1390
  mFixnum = rb_define_module_under(mGeneratorMethods, "Fixnum");
1406
1391
  rb_define_method(mFixnum, "to_json", mFixnum_to_json, -1);
1407
1392
  mBignum = rb_define_module_under(mGeneratorMethods, "Bignum");
1408
1393
  rb_define_method(mBignum, "to_json", mBignum_to_json, -1);
1394
+ #endif
1409
1395
  mFloat = rb_define_module_under(mGeneratorMethods, "Float");
1410
1396
  rb_define_method(mFloat, "to_json", mFloat_to_json, -1);
1411
1397
  mString = rb_define_module_under(mGeneratorMethods, "String");
@@ -1434,7 +1420,6 @@ void Init_generator(void)
1434
1420
  i_max_nesting = rb_intern("max_nesting");
1435
1421
  i_allow_nan = rb_intern("allow_nan");
1436
1422
  i_ascii_only = rb_intern("ascii_only");
1437
- i_quirks_mode = rb_intern("quirks_mode");
1438
1423
  i_depth = rb_intern("depth");
1439
1424
  i_buffer_initial_length = rb_intern("buffer_initial_length");
1440
1425
  i_pack = rb_intern("pack");
@@ -1,7 +1,6 @@
1
1
  #ifndef _GENERATOR_H_
2
2
  #define _GENERATOR_H_
3
3
 
4
- #include <string.h>
5
4
  #include <math.h>
6
5
  #include <ctype.h>
7
6
 
@@ -73,7 +72,6 @@ typedef struct JSON_Generator_StateStruct {
73
72
  long max_nesting;
74
73
  char allow_nan;
75
74
  char ascii_only;
76
- char quirks_mode;
77
75
  long depth;
78
76
  long buffer_initial_length;
79
77
  } JSON_Generator_State;
@@ -99,8 +97,12 @@ typedef struct JSON_Generator_StateStruct {
99
97
 
100
98
  static VALUE mHash_to_json(int argc, VALUE *argv, VALUE self);
101
99
  static VALUE mArray_to_json(int argc, VALUE *argv, VALUE self);
100
+ #ifdef RUBY_INTEGER_UNIFICATION
101
+ static VALUE mInteger_to_json(int argc, VALUE *argv, VALUE self);
102
+ #else
102
103
  static VALUE mFixnum_to_json(int argc, VALUE *argv, VALUE self);
103
104
  static VALUE mBignum_to_json(int argc, VALUE *argv, VALUE self);
105
+ #endif
104
106
  static VALUE mFloat_to_json(int argc, VALUE *argv, VALUE self);
105
107
  static VALUE mString_included_s(VALUE self, VALUE modul);
106
108
  static VALUE mString_to_json(int argc, VALUE *argv, VALUE self);
@@ -122,6 +124,9 @@ static void generate_json_string(FBuffer *buffer, VALUE Vstate, JSON_Generator_S
122
124
  static void generate_json_null(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
123
125
  static void generate_json_false(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
124
126
  static void generate_json_true(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
127
+ #ifdef RUBY_INTEGER_UNIFICATION
128
+ static void generate_json_integer(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
129
+ #endif
125
130
  static void generate_json_fixnum(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
126
131
  static void generate_json_bignum(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
127
132
  static void generate_json_float(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj);
@@ -1,3 +1,6 @@
1
+ # frozen_string_literal: false
1
2
  require 'mkmf'
2
3
 
4
+ have_func("rb_enc_raise", "ruby.h")
5
+
3
6
  create_makefile 'json/ext/parser'