json_pure 1.4.2 → 1.4.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES CHANGED
@@ -1,3 +1,7 @@
1
+ 2010-05-05 (1.4.3)
2
+ * Fixed some test assertions, from Ruby r27587 and r27590, patch by nobu.
3
+ * Fixed issue http://github.com/flori/json/issues/#issue/20 reported by
4
+ electronicwhisper@github. Thx!
1
5
  2010-04-26 (1.4.2)
2
6
  * Applied patch from naruse Yui NARUSE <naruse@airemix.com> to make building with
3
7
  Microsoft Visual C possible again.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.4.2
1
+ 1.4.3
@@ -738,150 +738,132 @@ static VALUE cState_aref(VALUE self, VALUE name)
738
738
  static void generate_json(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj, long depth)
739
739
  {
740
740
  VALUE tmp;
741
- switch (TYPE(obj)) {
742
- case T_HASH:
743
- {
744
- char *object_nl = state->object_nl;
745
- long object_nl_len = state->object_nl_len;
746
- char *indent = state->indent;
747
- long indent_len = state->indent_len;
748
- long max_nesting = state->max_nesting;
749
- char *delim = FBUFFER_PTR(state->object_delim);
750
- long delim_len = FBUFFER_LEN(state->object_delim);
751
- char *delim2 = FBUFFER_PTR(state->object_delim2);
752
- long delim2_len = FBUFFER_LEN(state->object_delim2);
753
- int i, j;
754
- VALUE key, key_to_s, keys;
755
- depth++;
756
- if (max_nesting != 0 && depth > max_nesting) {
757
- fbuffer_free(buffer);
758
- rb_raise(eNestingError, "nesting of %ld is too deep", depth);
759
- }
760
- fbuffer_append_char(buffer, '{');
761
- keys = rb_funcall(obj, rb_intern("keys"), 0);
762
- for(i = 0; i < RARRAY_LEN(keys); i++) {
763
- if (i > 0) fbuffer_append(buffer, delim, delim_len);
764
- if (object_nl) {
765
- fbuffer_append(buffer, object_nl, object_nl_len);
766
- }
767
- if (indent) {
768
- for (j = 0; j < depth; j++) {
769
- fbuffer_append(buffer, indent, indent_len);
770
- }
771
- }
772
- key = rb_ary_entry(keys, i);
773
- key_to_s = rb_funcall(key, i_to_s, 0);
774
- Check_Type(key_to_s, T_STRING);
775
- generate_json(buffer, Vstate, state, key_to_s, depth);
776
- fbuffer_append(buffer, delim2, delim2_len);
777
- generate_json(buffer, Vstate, state, rb_hash_aref(obj, key), depth);
778
- }
779
- depth--;
780
- if (object_nl) {
781
- fbuffer_append(buffer, object_nl, object_nl_len);
782
- if (indent) {
783
- for (j = 0; j < depth; j++) {
784
- fbuffer_append(buffer, indent, indent_len);
785
- }
786
- }
741
+ VALUE klass = CLASS_OF(obj);
742
+ if (klass == rb_cHash) {
743
+ char *object_nl = state->object_nl;
744
+ long object_nl_len = state->object_nl_len;
745
+ char *indent = state->indent;
746
+ long indent_len = state->indent_len;
747
+ long max_nesting = state->max_nesting;
748
+ char *delim = FBUFFER_PTR(state->object_delim);
749
+ long delim_len = FBUFFER_LEN(state->object_delim);
750
+ char *delim2 = FBUFFER_PTR(state->object_delim2);
751
+ long delim2_len = FBUFFER_LEN(state->object_delim2);
752
+ int i, j;
753
+ VALUE key, key_to_s, keys;
754
+ depth++;
755
+ if (max_nesting != 0 && depth > max_nesting) {
756
+ fbuffer_free(buffer);
757
+ rb_raise(eNestingError, "nesting of %ld is too deep", depth);
758
+ }
759
+ fbuffer_append_char(buffer, '{');
760
+ keys = rb_funcall(obj, rb_intern("keys"), 0);
761
+ for(i = 0; i < RARRAY_LEN(keys); i++) {
762
+ if (i > 0) fbuffer_append(buffer, delim, delim_len);
763
+ if (object_nl) {
764
+ fbuffer_append(buffer, object_nl, object_nl_len);
765
+ }
766
+ if (indent) {
767
+ for (j = 0; j < depth; j++) {
768
+ fbuffer_append(buffer, indent, indent_len);
787
769
  }
788
- fbuffer_append_char(buffer, '}');
789
770
  }
790
- break;
791
- case T_ARRAY:
792
- {
793
- char *array_nl = state->array_nl;
794
- long array_nl_len = state->array_nl_len;
795
- char *indent = state->indent;
796
- long indent_len = state->indent_len;
797
- long max_nesting = state->max_nesting;
798
- char *delim = FBUFFER_PTR(state->array_delim);
799
- long delim_len = FBUFFER_LEN(state->array_delim);
800
- int i, j;
801
- depth++;
802
- if (max_nesting != 0 && depth > max_nesting) {
803
- fbuffer_free(buffer);
804
- rb_raise(eNestingError, "nesting of %ld is too deep", depth);
771
+ key = rb_ary_entry(keys, i);
772
+ key_to_s = rb_funcall(key, i_to_s, 0);
773
+ Check_Type(key_to_s, T_STRING);
774
+ generate_json(buffer, Vstate, state, key_to_s, depth);
775
+ fbuffer_append(buffer, delim2, delim2_len);
776
+ generate_json(buffer, Vstate, state, rb_hash_aref(obj, key), depth);
777
+ }
778
+ depth--;
779
+ if (object_nl) {
780
+ fbuffer_append(buffer, object_nl, object_nl_len);
781
+ if (indent) {
782
+ for (j = 0; j < depth; j++) {
783
+ fbuffer_append(buffer, indent, indent_len);
805
784
  }
806
- fbuffer_append_char(buffer, '[');
807
- if (array_nl) fbuffer_append(buffer, array_nl, array_nl_len);
808
- for(i = 0; i < RARRAY_LEN(obj); i++) {
809
- if (i > 0) fbuffer_append(buffer, delim, delim_len);
810
- if (indent) {
811
- for (j = 0; j < depth; j++) {
812
- fbuffer_append(buffer, indent, indent_len);
813
- }
814
- }
815
- generate_json(buffer, Vstate, state, rb_ary_entry(obj, i), depth);
785
+ }
786
+ }
787
+ fbuffer_append_char(buffer, '}');
788
+ } else if (klass == rb_cArray) {
789
+ char *array_nl = state->array_nl;
790
+ long array_nl_len = state->array_nl_len;
791
+ char *indent = state->indent;
792
+ long indent_len = state->indent_len;
793
+ long max_nesting = state->max_nesting;
794
+ char *delim = FBUFFER_PTR(state->array_delim);
795
+ long delim_len = FBUFFER_LEN(state->array_delim);
796
+ int i, j;
797
+ depth++;
798
+ if (max_nesting != 0 && depth > max_nesting) {
799
+ fbuffer_free(buffer);
800
+ rb_raise(eNestingError, "nesting of %ld is too deep", depth);
801
+ }
802
+ fbuffer_append_char(buffer, '[');
803
+ if (array_nl) fbuffer_append(buffer, array_nl, array_nl_len);
804
+ for(i = 0; i < RARRAY_LEN(obj); i++) {
805
+ if (i > 0) fbuffer_append(buffer, delim, delim_len);
806
+ if (indent) {
807
+ for (j = 0; j < depth; j++) {
808
+ fbuffer_append(buffer, indent, indent_len);
816
809
  }
817
- depth--;
818
- if (array_nl) {
819
- fbuffer_append(buffer, array_nl, array_nl_len);
820
- if (indent) {
821
- for (j = 0; j < depth; j++) {
822
- fbuffer_append(buffer, indent, indent_len);
823
- }
824
- }
810
+ }
811
+ generate_json(buffer, Vstate, state, rb_ary_entry(obj, i), depth);
812
+ }
813
+ depth--;
814
+ if (array_nl) {
815
+ fbuffer_append(buffer, array_nl, array_nl_len);
816
+ if (indent) {
817
+ for (j = 0; j < depth; j++) {
818
+ fbuffer_append(buffer, indent, indent_len);
825
819
  }
826
- fbuffer_append_char(buffer, ']');
827
820
  }
828
- break;
829
- case T_STRING:
830
- fbuffer_append_char(buffer, '"');
821
+ }
822
+ fbuffer_append_char(buffer, ']');
823
+ } else if (klass == rb_cString) {
824
+ fbuffer_append_char(buffer, '"');
831
825
  #ifdef HAVE_RUBY_ENCODING_H
832
- obj = rb_funcall(obj, i_encode, 1, CEncoding_UTF_8);
826
+ obj = rb_funcall(obj, i_encode, 1, CEncoding_UTF_8);
833
827
  #endif
834
- if (state->ascii_only) {
835
- convert_UTF8_to_JSON_ASCII(buffer, obj);
836
- } else {
837
- convert_UTF8_to_JSON(buffer, obj);
838
- }
839
- fbuffer_append_char(buffer, '"');
840
- break;
841
- case T_NIL:
842
- fbuffer_append(buffer, "null", 4);
843
- break;
844
- case T_FALSE:
845
- fbuffer_append(buffer, "false", 5);
846
- break;
847
- case T_TRUE:
848
- fbuffer_append(buffer, "true", 4);
849
- break;
850
- case T_FIXNUM:
851
- fbuffer_append_long(buffer, FIX2LONG(obj));
852
- break;
853
- case T_BIGNUM:
854
- tmp = rb_funcall(obj, i_to_s, 0);
855
- fbuffer_append(buffer, RSTRING_PAIR(tmp));
856
- break;
857
- case T_FLOAT:
858
- {
859
- double value = RFLOAT_VALUE(obj);
860
- char allow_nan = state->allow_nan;
861
- tmp = rb_funcall(obj, i_to_s, 0);
862
- if (!allow_nan) {
863
- if (isinf(value)) {
864
- fbuffer_free(buffer);
865
- rb_raise(eGeneratorError, "%u: %s not allowed in JSON", __LINE__, StringValueCStr(tmp));
866
- } else if (isnan(value)) {
867
- fbuffer_free(buffer);
868
- rb_raise(eGeneratorError, "%u: %s not allowed in JSON", __LINE__, StringValueCStr(tmp));
869
- }
870
- }
871
- fbuffer_append(buffer, RSTRING_PAIR(tmp));
872
- }
873
- break;
874
- default:
875
- if (rb_respond_to(obj, i_to_json)) {
876
- tmp = rb_funcall(obj, i_to_json, 2, Vstate, INT2FIX(depth + 1));
877
- Check_Type(tmp, T_STRING);
878
- fbuffer_append(buffer, RSTRING_PAIR(tmp));
879
- } else {
880
- tmp = rb_funcall(obj, i_to_s, 0);
881
- Check_Type(tmp, T_STRING);
882
- generate_json(buffer, Vstate, state, tmp, depth + 1);
828
+ if (state->ascii_only) {
829
+ convert_UTF8_to_JSON_ASCII(buffer, obj);
830
+ } else {
831
+ convert_UTF8_to_JSON(buffer, obj);
832
+ }
833
+ fbuffer_append_char(buffer, '"');
834
+ } else if (obj == Qnil) {
835
+ fbuffer_append(buffer, "null", 4);
836
+ } else if (obj == Qfalse) {
837
+ fbuffer_append(buffer, "false", 5);
838
+ } else if (obj == Qtrue) {
839
+ fbuffer_append(buffer, "true", 4);
840
+ } else if (klass == rb_cFixnum) {
841
+ fbuffer_append_long(buffer, FIX2LONG(obj));
842
+ } else if (klass == rb_cBignum) {
843
+ tmp = rb_funcall(obj, i_to_s, 0);
844
+ fbuffer_append(buffer, RSTRING_PAIR(tmp));
845
+ } else if (klass == rb_cFloat) {
846
+ double value = RFLOAT_VALUE(obj);
847
+ char allow_nan = state->allow_nan;
848
+ tmp = rb_funcall(obj, i_to_s, 0);
849
+ if (!allow_nan) {
850
+ if (isinf(value)) {
851
+ fbuffer_free(buffer);
852
+ rb_raise(eGeneratorError, "%u: %s not allowed in JSON", __LINE__, StringValueCStr(tmp));
853
+ } else if (isnan(value)) {
854
+ fbuffer_free(buffer);
855
+ rb_raise(eGeneratorError, "%u: %s not allowed in JSON", __LINE__, StringValueCStr(tmp));
883
856
  }
884
- break;
857
+ }
858
+ fbuffer_append(buffer, RSTRING_PAIR(tmp));
859
+ } else if (rb_respond_to(obj, i_to_json)) {
860
+ tmp = rb_funcall(obj, i_to_json, 2, Vstate, INT2FIX(depth + 1));
861
+ Check_Type(tmp, T_STRING);
862
+ fbuffer_append(buffer, RSTRING_PAIR(tmp));
863
+ } else {
864
+ tmp = rb_funcall(obj, i_to_s, 0);
865
+ Check_Type(tmp, T_STRING);
866
+ generate_json(buffer, Vstate, state, tmp, depth + 1);
885
867
  }
886
868
  }
887
869
 
@@ -1,6 +1,6 @@
1
1
  module JSON
2
2
  # JSON version
3
- VERSION = '1.4.2'
3
+ VERSION = '1.4.3'
4
4
  VERSION_ARRAY = VERSION.split(/\./).map { |x| x.to_i } # :nodoc:
5
5
  VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
6
6
  VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
@@ -173,7 +173,18 @@ class TC_JSON < Test::Unit::TestCase
173
173
  assert_equal({'foo'=>'bar'}, parse(' { "foo" : "bar" } '))
174
174
  end
175
175
 
176
- class SubHash < Hash; end
176
+ class SubHash < Hash
177
+ def to_json(*a)
178
+ {
179
+ JSON.create_id => self.class.name,
180
+ }.merge(self).to_json(*a)
181
+ end
182
+
183
+ def self.json_create(o)
184
+ o.delete JSON.create_id
185
+ new.merge(o)
186
+ end
187
+ end
177
188
 
178
189
  def test_parse_object_custom_class
179
190
  res = parse('{}', :object_class => SubHash)
@@ -181,6 +192,16 @@ class TC_JSON < Test::Unit::TestCase
181
192
  assert_equal(SubHash, res.class)
182
193
  end
183
194
 
195
+ def test_generation_of_core_subclasses
196
+ obj = SubHash.new.merge( "foo" => SubHash.new.merge("bar" => true))
197
+ obj_json = JSON(obj)
198
+ obj_again = JSON(obj_json)
199
+ assert_kind_of SubHash, obj_again
200
+ assert_kind_of SubHash, obj_again['foo']
201
+ assert obj_again['foo']['bar']
202
+ assert_equal obj, obj_again
203
+ end
204
+
184
205
  def test_parser_reset
185
206
  parser = Parser.new(@json)
186
207
  assert_equal(@hash, parser.parse)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_pure
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.2
4
+ version: 1.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Frank
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-04-28 00:00:00 +02:00
12
+ date: 2010-05-05 00:00:00 +02:00
13
13
  default_executable: edit_json.rb
14
14
  dependencies: []
15
15