oj 3.7.11 → 3.9.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,21 +1,21 @@
1
1
  /* val_stack.c
2
2
  * Copyright (c) 2011, Peter Ohler
3
3
  * All rights reserved.
4
- *
4
+ *
5
5
  * Redistribution and use in source and binary forms, with or without
6
6
  * modification, are permitted provided that the following conditions are met:
7
- *
7
+ *
8
8
  * - Redistributions of source code must retain the above copyright notice, this
9
9
  * list of conditions and the following disclaimer.
10
- *
10
+ *
11
11
  * - Redistributions in binary form must reproduce the above copyright notice,
12
12
  * this list of conditions and the following disclaimer in the documentation
13
13
  * and/or other materials provided with the distribution.
14
- *
14
+ *
15
15
  * - Neither the name of Peter Ohler nor the names of its contributors may be
16
16
  * used to endorse or promote products derived from this software without
17
17
  * specific prior written permission.
18
- *
18
+ *
19
19
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
20
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
21
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
@@ -42,7 +42,7 @@ mark(void *ptr) {
42
42
  if (0 == ptr) {
43
43
  return;
44
44
  }
45
- #if HAVE_LIBPTHREAD
45
+ #ifdef HAVE_PTHREAD_MUTEX_INIT
46
46
  pthread_mutex_lock(&stack->mutex);
47
47
  #else
48
48
  rb_mutex_lock(stack->mutex);
@@ -66,7 +66,7 @@ mark(void *ptr) {
66
66
  }
67
67
  }
68
68
  }
69
- #if HAVE_LIBPTHREAD
69
+ #ifdef HAVE_PTHREAD_MUTEX_INIT
70
70
  pthread_mutex_unlock(&stack->mutex);
71
71
  #else
72
72
  rb_mutex_unlock(stack->mutex);
@@ -75,9 +75,9 @@ mark(void *ptr) {
75
75
 
76
76
  VALUE
77
77
  oj_stack_init(ValStack stack) {
78
- #if HAVE_LIBPTHREAD
78
+ #ifdef HAVE_PTHREAD_MUTEX_INIT
79
79
  int err;
80
-
80
+
81
81
  if (0 != (err = pthread_mutex_init(&stack->mutex, 0))) {
82
82
  rb_raise(rb_eException, "failed to initialize a mutex. %s", strerror(err));
83
83
  }
@@ -1,21 +1,21 @@
1
1
  /* val_stack.h
2
2
  * Copyright (c) 2011, Peter Ohler
3
3
  * All rights reserved.
4
- *
4
+ *
5
5
  * Redistribution and use in source and binary forms, with or without
6
6
  * modification, are permitted provided that the following conditions are met:
7
- *
7
+ *
8
8
  * - Redistributions of source code must retain the above copyright notice, this
9
9
  * list of conditions and the following disclaimer.
10
- *
10
+ *
11
11
  * - Redistributions in binary form must reproduce the above copyright notice,
12
12
  * this list of conditions and the following disclaimer in the documentation
13
13
  * and/or other materials provided with the distribution.
14
- *
14
+ *
15
15
  * - Neither the name of Peter Ohler nor the names of its contributors may be
16
16
  * used to endorse or promote products derived from this software without
17
17
  * specific prior written permission.
18
- *
18
+ *
19
19
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
20
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
21
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
@@ -34,7 +34,7 @@
34
34
  #include "ruby.h"
35
35
  #include "odd.h"
36
36
  #include <stdint.h>
37
- #if HAVE_LIBPTHREAD
37
+ #ifdef HAVE_PTHREAD_MUTEX_INIT
38
38
  #include <pthread.h>
39
39
  #endif
40
40
 
@@ -72,7 +72,7 @@ typedef struct _valStack {
72
72
  Val head; // current stack
73
73
  Val end; // stack end
74
74
  Val tail; // pointer to one past last element name on stack
75
- #if HAVE_LIBPTHREAD
75
+ #ifdef HAVE_PTHREAD_MUTEX_INIT
76
76
  pthread_mutex_t mutex;
77
77
  #else
78
78
  VALUE mutex;
@@ -110,7 +110,7 @@ stack_push(ValStack stack, VALUE val, ValNext next) {
110
110
  } else {
111
111
  REALLOC_N(head, struct _val, len + STACK_INC);
112
112
  }
113
- #if HAVE_LIBPTHREAD
113
+ #ifdef HAVE_PTHREAD_MUTEX_INIT
114
114
  pthread_mutex_lock(&stack->mutex);
115
115
  #else
116
116
  rb_mutex_lock(stack->mutex);
@@ -118,7 +118,7 @@ stack_push(ValStack stack, VALUE val, ValNext next) {
118
118
  stack->head = head;
119
119
  stack->tail = stack->head + toff;
120
120
  stack->end = stack->head + len + STACK_INC;
121
- #if HAVE_LIBPTHREAD
121
+ #ifdef HAVE_PTHREAD_MUTEX_INIT
122
122
  pthread_mutex_unlock(&stack->mutex);
123
123
  #else
124
124
  rb_mutex_unlock(stack->mutex);
@@ -95,7 +95,7 @@ module JSON
95
95
  State = ::JSON::Ext::Generator::State unless defined?(::JSON::State)
96
96
 
97
97
  begin
98
- Object.send(:remove_const, :Parser)
98
+ send(:remove_const, :Parser)
99
99
  rescue
100
100
  end
101
101
  Parser = ::JSON::Ext::Parser unless defined?(::JSON::Parser)
@@ -31,7 +31,7 @@ module Oj
31
31
  end
32
32
 
33
33
  # Loads mimic-ed JSON paths. Used by Oj.mimic_JSON().
34
- # @param mimic_path [Array] additional paths to add to the Ruby loaded features.
34
+ # @param mimic_paths [Array] additional paths to add to the Ruby loaded features.
35
35
  def self.mimic_loaded(mimic_paths=[])
36
36
  $LOAD_PATH.each do |d|
37
37
  next unless File.exist?(d)
@@ -1,5 +1,5 @@
1
1
 
2
2
  module Oj
3
3
  # Current version of the module.
4
- VERSION = '3.7.11'
4
+ VERSION = '3.9.1'
5
5
  end
@@ -14,7 +14,7 @@ modes are:
14
14
  - `:object`
15
15
  - `:custom`
16
16
 
17
- Since modes detemine what the JSON output will look like and alternatively
17
+ Since modes determine what the JSON output will look like and alternatively
18
18
  what Oj expects when the `Oj.load()` method is called, mixing the output and
19
19
  input mode formats will most likely not behave as intended. If the object mode
20
20
  is used for producing JSON then use object mode for reading. The same is true
@@ -117,6 +117,7 @@ information.
117
117
  | :object_nl | String | | | x | x | | x | |
118
118
  | :omit_nil | Boolean | x | x | x | x | x | x | |
119
119
  | :quirks_mode | Boolean | | | 6 | | | x | |
120
+ | :safe | String | | | x | | | | |
120
121
  | :second_precision | Fixnum | | | | | x | x | |
121
122
  | :space | String | | | x | x | | x | |
122
123
  | :space_before | String | | | x | x | | x | |
@@ -124,6 +125,7 @@ information.
124
125
  | :trace | Boolean | x | x | x | x | x | x | x |
125
126
  | :time_format | Symbol | | | | | x | x | |
126
127
  | :use_as_json | Boolean | | | | | | x | |
128
+ | :use_raw_json | Boolean | | | x | x | x | x | |
127
129
  | :use_to_hash | Boolean | | | | | | x | |
128
130
  | :use_to_json | Boolean | | | | | | x | |
129
131
  --------------------------------------------------------------------------------------------------------
@@ -151,4 +153,3 @@ information.
151
153
  6. The quirks mode option is no longer supported in the most recent json
152
154
  gem. It is supported by Oj for backward compatibility with older json gem
153
155
  versions.
154
-
@@ -89,6 +89,10 @@ with the key.
89
89
  The :create_id option specifies that key is used for dumping and loading when
90
90
  specifying the class for an encoded object. The default is `json_create`.
91
91
 
92
+ In the `:custom` mode setting the `:create_id` to nil will cause Complex,
93
+ Rational, Range, and Regexp to be output as strings instead of as JSON
94
+ objects.
95
+
92
96
  ### :empty_string [Boolean]
93
97
 
94
98
  If true an empty or all whitespace input will not raise an Exception. The
@@ -149,7 +153,7 @@ integer gives better performance.
149
153
 
150
154
  ### :integer_range [Range]
151
155
 
152
- Dump integers outside range as strings.
156
+ Dump integers outside range as strings.
153
157
  Note: range bounds must be Fixnum.
154
158
 
155
159
  ### :match_string
@@ -211,6 +215,12 @@ Allow single JSON values instead of documents, default is true (allow). This
211
215
  can also be used in :compat mode to be backward compatible with older versions
212
216
  of the json gem.
213
217
 
218
+ ### :safe
219
+
220
+ The JSON gem includes the complete JSON in parse errors with no limit
221
+ on size. To break from the JSON gem behavior for this case set `:safe`
222
+ to true.
223
+
214
224
  ### :second_precision [Fixnum]
215
225
 
216
226
  The number of digits after the decimal when dumping the seconds of time.
@@ -254,6 +264,18 @@ The :time_format when dumping.
254
264
  Call `as_json()` methods on dump, default is false. The option is ignored in
255
265
  the :compat and :rails mode.
256
266
 
267
+
268
+ ### :use_raw_json [Boolean]
269
+
270
+ Call `raw_json()` methods on dump, default is false. The option is
271
+ accepted in the :compat and :rails mode even though it is not
272
+ supported by other JSON gems. It provides a means to optimize dump or
273
+ generate performance. The `raw_json(depth, indent)` method should be
274
+ called only by Oj. It is not intended for any other use. This is mean
275
+ to replace the abused `to_json` methods. Calling `Oj.dump` inside the
276
+ `raw_json` with the object itself when `:use_raw_json` is true will
277
+ result in an infinite loop.
278
+
257
279
  ### :use_to_hash [Boolean]
258
280
 
259
281
  Call `to_hash()` methods on dump, default is false. The option is ignored in
@@ -263,4 +285,3 @@ the :compat and :rails mode.
263
285
 
264
286
  Call `to_json()` methods on dump, default is false. The option is ignored in
265
287
  the :compat and :rails mode.
266
-
@@ -7,7 +7,7 @@ Symbols. The same is true for auto defining classes in all versions of ruby;
7
7
  memory will also be exhausted if too many classes are automatically
8
8
  defined. Auto defining is a useful feature during development and from trusted
9
9
  sources but it allows too many classes to be created in the object load mode and
10
- auto defined is used with an untrusted source. The `Oj.strict_load()` method
10
+ auto defined is used with an untrusted source. The `Oj.safe_load()` method
11
11
  sets and uses the most strict and safest options. It should be used by
12
12
  developers who find it difficult to understand the options available in Oj.
13
13
 
@@ -32,6 +32,9 @@ class CustomJuice < Minitest::Test
32
32
  def to_json(*args)
33
33
  %|{"xx":#{@x},"yy":#{y}}|
34
34
  end
35
+ def raw_json(depth, indent)
36
+ %|{"xxx":#{@x},"yyy":#{y}}|
37
+ end
35
38
  def as_json(*args)
36
39
  {'a' => @x, :b => @y }
37
40
  end
@@ -40,6 +43,40 @@ class CustomJuice < Minitest::Test
40
43
  end
41
44
  end
42
45
 
46
+ class AsJson
47
+ attr_accessor :x, :y
48
+
49
+ def initialize(x, y)
50
+ @x = x
51
+ @y = y
52
+ end
53
+ def ==(o)
54
+ self.class == o.class && @x == o.x && @y = o.y
55
+ end
56
+ def as_json(*args)
57
+ {'a' => @x, :b => @y }
58
+ end
59
+ end
60
+
61
+ class AsRails
62
+ attr_accessor :x, :y
63
+
64
+ def initialize(x, y)
65
+ @x = x
66
+ @y = y
67
+ end
68
+ def ==(o)
69
+ self.class == o.class && @x == o.x && @y = o.y
70
+ end
71
+ def as_json(*args)
72
+ a = @x
73
+ a = a.as_json if a.respond_to?('as_json')
74
+ b = @y
75
+ b = b.as_json if b.respond_to?('as_json')
76
+ {'a' => a, :b => b }
77
+ end
78
+ end
79
+
43
80
  def setup
44
81
  @default_options = Oj.default_options
45
82
  Oj.default_options = { :mode => :custom }
@@ -92,7 +129,7 @@ class CustomJuice < Minitest::Test
92
129
  end
93
130
  assert(false, "*** expected an exception")
94
131
  end
95
-
132
+
96
133
  def test_infinity_dump
97
134
  assert_equal('null', Oj.dump(1/0.0, :nan => :null))
98
135
  assert_equal('3.0e14159265358979323846', Oj.dump(1/0.0, :nan => :huge))
@@ -187,7 +224,7 @@ class CustomJuice < Minitest::Test
187
224
  '19' => {
188
225
  '20' => {}}}}}}}}}}}}}}}}}}}}}, false)
189
226
  end
190
-
227
+
191
228
  def test_hash_escaped_key
192
229
  json = %{{"a\nb":true,"c\td":false}}
193
230
  obj = Oj.load(json)
@@ -232,6 +269,66 @@ class CustomJuice < Minitest::Test
232
269
  assert_equal(%|{"b":true,"n":58}|, json)
233
270
  end
234
271
 
272
+ def test_object_raw_json
273
+ obj = Jeez.new(true, 58)
274
+ json = Oj.dump(obj, :use_to_json => true, :use_as_json => false, :use_raw_json => true, :use_to_hash => false)
275
+ assert_equal(%|{"xxx":true,"yyy":58}|, json)
276
+ end
277
+
278
+ def test_raw_json_stringwriter
279
+ obj = Oj::StringWriter.new(:indent => 0)
280
+ obj.push_array()
281
+ obj.pop()
282
+ json = Oj.dump(obj, :use_raw_json => true)
283
+ assert_equal(%|[]|, json)
284
+ end
285
+
286
+ def test_as_raw_json_stringwriter
287
+ obj = Oj::StringWriter.new(:indent => 0)
288
+ obj.push_array()
289
+ obj.push_value(3)
290
+ obj.pop()
291
+ j = AsJson.new(1, obj)
292
+
293
+ json = Oj.dump(j, use_raw_json: true, use_as_json: true, indent: 2)
294
+ assert_equal(%|{
295
+ "a":1,
296
+ "b":[3]
297
+ }
298
+ |, json)
299
+
300
+ json = Oj.dump(j, use_raw_json: false, use_as_json: true, indent: 2)
301
+ assert_equal(%|{
302
+ "a":1,
303
+ "b":{}
304
+ }
305
+ |, json)
306
+ end
307
+
308
+ def test_rails_as_raw_json_stringwriter
309
+ obj = Oj::StringWriter.new(:indent => 0)
310
+ obj.push_array()
311
+ obj.push_value(3)
312
+ obj.pop()
313
+ j = AsRails.new(1, obj)
314
+ json = Oj.dump(j, mode: :rails, use_raw_json: true, indent: 2)
315
+ assert_equal(%|{
316
+ "a":1,
317
+ "b":{}
318
+ }
319
+ |, json)
320
+
321
+ Oj::Rails.optimize
322
+ json = Oj.dump(j, mode: :rails, use_raw_json: true, indent: 2)
323
+ Oj::Rails.deoptimize
324
+ assert_equal(%|{
325
+ "a":1,
326
+ "b":[3]
327
+ }
328
+ |, json)
329
+
330
+ end
331
+
235
332
  def test_symbol
236
333
  json = Oj.dump(:abc)
237
334
  assert_equal('"abc"', json)
@@ -134,6 +134,7 @@ class Juice < Minitest::Test
134
134
  :use_to_json=>false,
135
135
  :use_to_hash=>false,
136
136
  :use_as_json=>false,
137
+ :use_raw_json=>false,
137
138
  :nilnil=>true,
138
139
  :empty_string=>true,
139
140
  :allow_gc=>false,
@@ -158,6 +159,7 @@ class Juice < Minitest::Test
158
159
  :array_class=>Array,
159
160
  :ignore=>nil,
160
161
  :trace=>true,
162
+ :safe=>true,
161
163
  }
162
164
  Oj.default_options = alt
163
165
  #keys = alt.keys
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oj
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.7.11
4
+ version: 3.9.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Ohler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-03-20 00:00:00.000000000 Z
11
+ date: 2019-08-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake-compiler
@@ -266,14 +266,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
266
266
  requirements:
267
267
  - - ">="
268
268
  - !ruby/object:Gem::Version
269
- version: '2.0'
269
+ version: '2.3'
270
270
  required_rubygems_version: !ruby/object:Gem::Requirement
271
271
  requirements:
272
272
  - - ">="
273
273
  - !ruby/object:Gem::Version
274
274
  version: '0'
275
275
  requirements: []
276
- rubygems_version: 3.0.1
276
+ rubygems_version: 3.0.3
277
277
  signing_key:
278
278
  specification_version: 4
279
279
  summary: A fast JSON parser and serializer.