oj 1.2.4 → 1.2.5

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

Potentially problematic release.


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

data/README.md CHANGED
@@ -24,9 +24,9 @@ A fast JSON parser and Object marshaller as a Ruby gem.
24
24
 
25
25
  ## <a name="release">Release Notes</a>
26
26
 
27
- ### Release 1.2.4
27
+ ### Release 1.2.5
28
28
 
29
- - Removed all use of math.h to get around CentOS 5.4 compile problem.
29
+ - Added support for create_id in Oj and in mimic_JSON mode
30
30
 
31
31
  ## <a name="description">Description</a>
32
32
 
@@ -475,8 +475,8 @@ read_obj(ParseInfo pi) {
475
475
  rb_hash_aset(obj, key, val);
476
476
  }
477
477
  if ((CompatMode == mode || ObjectMode == mode) &&
478
- 0 == json_class_name &&
479
- 0 != ks && 'j' == *ks && 0 == strcmp("json_class", ks) &&
478
+ 0 == json_class_name && 0 != ks &&
479
+ 0 != pi->options->create_id && *pi->options->create_id == *ks && 0 == strcmp(pi->options->create_id, ks) &&
480
480
  T_STRING == rb_type(val)) {
481
481
  json_class_name = StringValuePtr(val);
482
482
  }
@@ -77,6 +77,7 @@ static VALUE ascii_only_sym;
77
77
  static VALUE auto_define_sym;
78
78
  static VALUE circular_sym;
79
79
  static VALUE compat_sym;
80
+ static VALUE create_id_sym;
80
81
  static VALUE indent_sym;
81
82
  static VALUE mode_sym;
82
83
  static VALUE null_sym;
@@ -101,6 +102,8 @@ Cache oj_attr_cache = 0;
101
102
  rb_encoding *oj_utf8_encoding = 0;
102
103
  #endif
103
104
 
105
+ static const char json_class[] = "json_class";
106
+
104
107
  struct _Options oj_default_options = {
105
108
  0, // indent
106
109
  No, // circular
@@ -108,6 +111,7 @@ struct _Options oj_default_options = {
108
111
  No, // sym_key
109
112
  No, // ascii_only
110
113
  ObjectMode, // mode
114
+ json_class, // create_id
111
115
  0, // dump_opts
112
116
  };
113
117
 
@@ -121,6 +125,7 @@ static VALUE define_mimic_json(VALUE self);
121
125
  * - auto_define: [true|false|nil] automatically define classes if they do not exist
122
126
  * - symbol_keys: [true|false|nil] use symbols instead of strings for hash keys
123
127
  * - mode: [:object|:strict|:compat|:null] load and dump modes to use for JSON
128
+ * - create_id: [String|nil] create id for json compatible object encoding, default is 'json_create'
124
129
  * @return [Hash] all current option settings.
125
130
  */
126
131
  static VALUE
@@ -139,6 +144,8 @@ get_def_opts(VALUE self) {
139
144
  case ObjectMode:
140
145
  default: rb_hash_aset(opts, mode_sym, object_sym); break;
141
146
  }
147
+ rb_hash_aset(opts, create_id_sym, (0 == oj_default_options.create_id) ? Qnil : rb_str_new2(oj_default_options.create_id));
148
+
142
149
  return opts;
143
150
  }
144
151
 
@@ -158,7 +165,9 @@ get_def_opts(VALUE self) {
158
165
  * variables if neither is found. The :object mode ignores to_hash()
159
166
  * and to_json() methods and encodes variables using code internal to
160
167
  * the Oj gem. The :null mode ignores non-supported Objects and
161
- * replaces them with a null. @return [nil]
168
+ * replaces them with a null.
169
+ * @param [String|nil] :create_id create id for json compatible object encoding
170
+ * @return [nil]
162
171
  */
163
172
  static VALUE
164
173
  set_def_opts(VALUE self, VALUE opts) {
@@ -194,6 +203,22 @@ set_def_opts(VALUE self, VALUE opts) {
194
203
  rb_raise(rb_eArgError, ":mode must be :object, :strict, :compat, or :null.\n");
195
204
  }
196
205
 
206
+ if (Qtrue == rb_funcall(opts, rb_intern("has_key?"), 1, create_id_sym)) {
207
+ if (0 != oj_default_options.create_id) {
208
+ if (json_class != oj_default_options.create_id) {
209
+ xfree((char*)oj_default_options.create_id);
210
+ }
211
+ oj_default_options.create_id = 0;
212
+ }
213
+ v = rb_hash_lookup(opts, create_id_sym);
214
+ if (Qnil != v) {
215
+ size_t len = RSTRING_LEN(v) + 1;
216
+
217
+ oj_default_options.create_id = ALLOC_N(char, len);
218
+ strcpy((char*)oj_default_options.create_id, StringValuePtr(v));
219
+ }
220
+ }
221
+
197
222
  for (o = ynos; 0 != o->attr; o++) {
198
223
  if (Qtrue != rb_funcall(opts, rb_intern("has_key?"), 1, o->sym)) {
199
224
  continue;
@@ -638,7 +663,7 @@ mimic_parse(int argc, VALUE *argv, VALUE self) {
638
663
  if (Qnil != (v = rb_hash_lookup(ropts, create_additions_sym))) {
639
664
  options.mode = (Qtrue == v) ? CompatMode : StrictMode;
640
665
  }
641
- // :allow_nan is not supported as Oj always allows_nan
666
+ // :allow_nan is not supported as Oj always allows nan
642
667
  // :max_nesting is always set to 100
643
668
  // :object_class is always Hash
644
669
  // :array_class is always Array
@@ -659,6 +684,25 @@ no_op1(VALUE self, VALUE obj) {
659
684
  return Qnil;
660
685
  }
661
686
 
687
+ static VALUE
688
+ mimic_create_id(VALUE self, VALUE id) {
689
+ Check_Type(id, T_STRING);
690
+
691
+ if (0 != oj_default_options.create_id) {
692
+ if (json_class != oj_default_options.create_id) {
693
+ xfree((char*)oj_default_options.create_id);
694
+ }
695
+ oj_default_options.create_id = 0;
696
+ }
697
+ if (Qnil != id) {
698
+ size_t len = RSTRING_LEN(id) + 1;
699
+
700
+ oj_default_options.create_id = ALLOC_N(char, len);
701
+ strcpy((char*)oj_default_options.create_id, StringValuePtr(id));
702
+ }
703
+ return id;
704
+ }
705
+
662
706
  /* call-seq: mimic_JSON() => Module
663
707
  *
664
708
  * Creates the JSON module with methods and classes to mimic the JSON
@@ -679,6 +723,7 @@ define_mimic_json(VALUE self) {
679
723
 
680
724
  rb_define_module_function(mimic, "parser=", no_op1, 1);
681
725
  rb_define_module_function(mimic, "generator=", no_op1, 1);
726
+ rb_define_module_function(mimic, "create_id=", mimic_create_id, 1);
682
727
 
683
728
  rb_define_module_function(mimic, "dump", mimic_dump, -1);
684
729
  rb_define_module_function(mimic, "load", mimic_load, -1);
@@ -745,6 +790,7 @@ void Init_oj() {
745
790
  auto_define_sym = ID2SYM(rb_intern("auto_define")); rb_ary_push(keep, auto_define_sym);
746
791
  circular_sym = ID2SYM(rb_intern("circular")); rb_ary_push(keep, circular_sym);
747
792
  compat_sym = ID2SYM(rb_intern("compat")); rb_ary_push(keep, compat_sym);
793
+ create_id_sym = ID2SYM(rb_intern("create_id")); rb_ary_push(keep, create_id_sym);
748
794
  indent_sym = ID2SYM(rb_intern("indent")); rb_ary_push(keep, indent_sym);
749
795
  mode_sym = ID2SYM(rb_intern("mode")); rb_ary_push(keep, mode_sym);
750
796
  symbol_keys_sym = ID2SYM(rb_intern("symbol_keys")); rb_ary_push(keep, symbol_keys_sym);
@@ -95,6 +95,7 @@ typedef struct _Options {
95
95
  char sym_key; // YesNo
96
96
  char ascii_only; // YesNo
97
97
  char mode; // Mode
98
+ const char *create_id; // 0 or string
98
99
  DumpOpts dump_opts;
99
100
  } *Options;
100
101
 
@@ -1,5 +1,5 @@
1
1
 
2
2
  module Oj
3
3
  # Current version of the module.
4
- VERSION = '1.2.4'
4
+ VERSION = '1.2.5'
5
5
  end
@@ -171,6 +171,11 @@ class Mimic < ::Test::Unit::TestCase
171
171
  assert_equal(jam, obj)
172
172
  obj = JSON.parse(json, :create_additions => false)
173
173
  assert_equal({'json_class' => 'Jam', 'x' => true, 'y' => 58}, obj)
174
+ json.gsub!('json_class', 'kson_class')
175
+ JSON.create_id = 'kson_class'
176
+ obj = JSON.parse(json, :create_additions => true)
177
+ JSON.create_id = 'json_class'
178
+ assert_equal(jam, obj)
174
179
  end
175
180
  def test_parse_bang
176
181
  json = %{{"a":1,"b":[true,false]}}
@@ -106,7 +106,8 @@ class Juice < ::Test::Unit::TestCase
106
106
  :auto_define=>true,
107
107
  :symbol_keys=>false,
108
108
  :ascii_only=>false,
109
- :mode=>:object}, opts)
109
+ :mode=>:object,
110
+ :create_id=>'json_class'}, opts)
110
111
  end
111
112
 
112
113
  def test0_set_options
@@ -116,18 +117,20 @@ class Juice < ::Test::Unit::TestCase
116
117
  :auto_define=>true,
117
118
  :symbol_keys=>false,
118
119
  :ascii_only=>false,
119
- :mode=>:object}
120
+ :mode=>:object,
121
+ :create_id=>'json_class'}
120
122
  o2 = {
121
123
  :indent=>4,
122
124
  :circular=>true,
123
125
  :auto_define=>false,
124
126
  :symbol_keys=>true,
125
127
  :ascii_only=>true,
126
- :mode=>:compat}
128
+ :mode=>:compat,
129
+ :create_id=>nil}
127
130
  o3 = { :indent => 4 }
128
131
  Oj.default_options = o2
129
132
  opts = Oj.default_options()
130
- assert_equal(opts, o2);
133
+ assert_equal(o2, opts);
131
134
  Oj.default_options = o3 # see if it throws an exception
132
135
  Oj.default_options = orig # return to original
133
136
  end
@@ -339,6 +342,14 @@ class Juice < ::Test::Unit::TestCase
339
342
  %{{"json_class":"Jeez","y":58,"x":true}} == json)
340
343
  dump_and_load(obj, false)
341
344
  end
345
+ def test_json_object_create_id
346
+ Oj.default_options = { :mode => :compat, :create_id => 'kson_class' }
347
+ expected = Jeez.new(true, 58)
348
+ json = %{{"kson_class":"Jeez","x":true,"y":58}}
349
+ obj = Oj.load(json)
350
+ assert_equal(expected, obj)
351
+ Oj.default_options = { :create_id => 'json_class' }
352
+ end
342
353
  def test_json_object_object
343
354
  obj = Jeez.new(true, 58)
344
355
  json = Oj.dump(obj, :mode => :object, :indent => 2)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oj
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.4
4
+ version: 1.2.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-13 00:00:00.000000000 Z
12
+ date: 2012-04-18 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: ! 'The fastest JSON parser and object serializer. '
15
15
  email: peter@ohler.com