oj 3.7.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +96 -0
- data/ext/oj/buf.h +103 -0
- data/ext/oj/cache8.c +107 -0
- data/ext/oj/cache8.h +48 -0
- data/ext/oj/circarray.c +68 -0
- data/ext/oj/circarray.h +23 -0
- data/ext/oj/code.c +235 -0
- data/ext/oj/code.h +42 -0
- data/ext/oj/compat.c +299 -0
- data/ext/oj/custom.c +1188 -0
- data/ext/oj/dump.c +1232 -0
- data/ext/oj/dump.h +94 -0
- data/ext/oj/dump_compat.c +973 -0
- data/ext/oj/dump_leaf.c +252 -0
- data/ext/oj/dump_object.c +837 -0
- data/ext/oj/dump_strict.c +433 -0
- data/ext/oj/encode.h +45 -0
- data/ext/oj/err.c +57 -0
- data/ext/oj/err.h +70 -0
- data/ext/oj/extconf.rb +47 -0
- data/ext/oj/fast.c +1771 -0
- data/ext/oj/hash.c +163 -0
- data/ext/oj/hash.h +46 -0
- data/ext/oj/hash_test.c +512 -0
- data/ext/oj/mimic_json.c +873 -0
- data/ext/oj/object.c +771 -0
- data/ext/oj/odd.c +231 -0
- data/ext/oj/odd.h +44 -0
- data/ext/oj/oj.c +1694 -0
- data/ext/oj/oj.h +381 -0
- data/ext/oj/parse.c +1085 -0
- data/ext/oj/parse.h +111 -0
- data/ext/oj/rails.c +1485 -0
- data/ext/oj/rails.h +21 -0
- data/ext/oj/reader.c +231 -0
- data/ext/oj/reader.h +151 -0
- data/ext/oj/resolve.c +102 -0
- data/ext/oj/resolve.h +14 -0
- data/ext/oj/rxclass.c +147 -0
- data/ext/oj/rxclass.h +27 -0
- data/ext/oj/saj.c +714 -0
- data/ext/oj/scp.c +224 -0
- data/ext/oj/sparse.c +910 -0
- data/ext/oj/stream_writer.c +363 -0
- data/ext/oj/strict.c +212 -0
- data/ext/oj/string_writer.c +512 -0
- data/ext/oj/trace.c +79 -0
- data/ext/oj/trace.h +28 -0
- data/ext/oj/util.c +136 -0
- data/ext/oj/util.h +19 -0
- data/ext/oj/val_stack.c +118 -0
- data/ext/oj/val_stack.h +185 -0
- data/ext/oj/wab.c +631 -0
- data/lib/oj.rb +21 -0
- data/lib/oj/active_support_helper.rb +41 -0
- data/lib/oj/bag.rb +88 -0
- data/lib/oj/easy_hash.rb +52 -0
- data/lib/oj/error.rb +22 -0
- data/lib/oj/json.rb +176 -0
- data/lib/oj/mimic.rb +267 -0
- data/lib/oj/saj.rb +66 -0
- data/lib/oj/schandler.rb +142 -0
- data/lib/oj/state.rb +131 -0
- data/lib/oj/version.rb +5 -0
- data/pages/Advanced.md +22 -0
- data/pages/Compatibility.md +25 -0
- data/pages/Custom.md +23 -0
- data/pages/Encoding.md +65 -0
- data/pages/JsonGem.md +79 -0
- data/pages/Modes.md +154 -0
- data/pages/Options.md +266 -0
- data/pages/Rails.md +116 -0
- data/pages/Security.md +20 -0
- data/pages/WAB.md +13 -0
- data/test/_test_active.rb +76 -0
- data/test/_test_active_mimic.rb +96 -0
- data/test/_test_mimic_rails.rb +126 -0
- data/test/activerecord/result_test.rb +27 -0
- data/test/activesupport4/decoding_test.rb +108 -0
- data/test/activesupport4/encoding_test.rb +531 -0
- data/test/activesupport4/test_helper.rb +41 -0
- data/test/activesupport5/decoding_test.rb +125 -0
- data/test/activesupport5/encoding_test.rb +485 -0
- data/test/activesupport5/encoding_test_cases.rb +90 -0
- data/test/activesupport5/test_helper.rb +50 -0
- data/test/activesupport5/time_zone_test_helpers.rb +24 -0
- data/test/big.rb +15 -0
- data/test/files.rb +29 -0
- data/test/foo.rb +33 -0
- data/test/helper.rb +26 -0
- data/test/isolated/shared.rb +308 -0
- data/test/isolated/test_mimic_after.rb +13 -0
- data/test/isolated/test_mimic_alone.rb +12 -0
- data/test/isolated/test_mimic_as_json.rb +45 -0
- data/test/isolated/test_mimic_before.rb +13 -0
- data/test/isolated/test_mimic_define.rb +28 -0
- data/test/isolated/test_mimic_rails_after.rb +22 -0
- data/test/isolated/test_mimic_rails_before.rb +21 -0
- data/test/isolated/test_mimic_redefine.rb +15 -0
- data/test/json_gem/json_addition_test.rb +216 -0
- data/test/json_gem/json_common_interface_test.rb +148 -0
- data/test/json_gem/json_encoding_test.rb +107 -0
- data/test/json_gem/json_ext_parser_test.rb +20 -0
- data/test/json_gem/json_fixtures_test.rb +35 -0
- data/test/json_gem/json_generator_test.rb +383 -0
- data/test/json_gem/json_generic_object_test.rb +90 -0
- data/test/json_gem/json_parser_test.rb +470 -0
- data/test/json_gem/json_string_matching_test.rb +42 -0
- data/test/json_gem/test_helper.rb +18 -0
- data/test/mem.rb +35 -0
- data/test/perf.rb +107 -0
- data/test/perf_compat.rb +130 -0
- data/test/perf_fast.rb +164 -0
- data/test/perf_file.rb +64 -0
- data/test/perf_object.rb +138 -0
- data/test/perf_saj.rb +109 -0
- data/test/perf_scp.rb +151 -0
- data/test/perf_simple.rb +287 -0
- data/test/perf_strict.rb +145 -0
- data/test/perf_wab.rb +131 -0
- data/test/sample.rb +54 -0
- data/test/sample/change.rb +14 -0
- data/test/sample/dir.rb +19 -0
- data/test/sample/doc.rb +36 -0
- data/test/sample/file.rb +48 -0
- data/test/sample/group.rb +16 -0
- data/test/sample/hasprops.rb +16 -0
- data/test/sample/layer.rb +12 -0
- data/test/sample/line.rb +20 -0
- data/test/sample/oval.rb +10 -0
- data/test/sample/rect.rb +10 -0
- data/test/sample/shape.rb +35 -0
- data/test/sample/text.rb +20 -0
- data/test/sample_json.rb +37 -0
- data/test/test_compat.rb +509 -0
- data/test/test_custom.rb +406 -0
- data/test/test_debian.rb +53 -0
- data/test/test_fast.rb +470 -0
- data/test/test_file.rb +239 -0
- data/test/test_gc.rb +49 -0
- data/test/test_hash.rb +29 -0
- data/test/test_integer_range.rb +73 -0
- data/test/test_null.rb +376 -0
- data/test/test_object.rb +1018 -0
- data/test/test_saj.rb +186 -0
- data/test/test_scp.rb +433 -0
- data/test/test_strict.rb +410 -0
- data/test/test_various.rb +739 -0
- data/test/test_wab.rb +307 -0
- data/test/test_writer.rb +380 -0
- data/test/tests.rb +24 -0
- data/test/tests_mimic.rb +14 -0
- data/test/tests_mimic_addition.rb +7 -0
- metadata +359 -0
data/pages/Rails.md
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
# Oj Rails Compatibility
|
|
2
|
+
|
|
3
|
+
The `:rails` mode mimics the ActiveSupport version 5 encoder. Rails and
|
|
4
|
+
ActiveSupport are built around the use of the `as_json(*)` method defined for
|
|
5
|
+
a class. Oj attempts to provide the same functionality by being a drop in
|
|
6
|
+
replacement with a few exceptions.
|
|
7
|
+
|
|
8
|
+
```ruby
|
|
9
|
+
require 'oj'
|
|
10
|
+
|
|
11
|
+
Oj::Rails.set_encoder()
|
|
12
|
+
Oj::Rails.set_decoder()
|
|
13
|
+
Oj::Rails.optimize()
|
|
14
|
+
Oj::Rails.mimic_JSON()
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
or simply call
|
|
18
|
+
|
|
19
|
+
```ruby
|
|
20
|
+
Oj.optimize_rails()
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Either of those steps will setup Oj to mimic Rails but it will not change the
|
|
24
|
+
default mode type as the mode type is only used when calling the Oj encoding
|
|
25
|
+
directly. If Rails mode is also desired then use the `Oj.default_options` to
|
|
26
|
+
change the default mode.
|
|
27
|
+
|
|
28
|
+
Some of the Oj options are supported as arguments to the encoder if called
|
|
29
|
+
from Oj::Rails.encode() but when using the Oj::Rails::Encoder class the
|
|
30
|
+
encode() method does not support optional arguments as required by the
|
|
31
|
+
ActiveSupport compliance guidelines. The general approach Rails takes for
|
|
32
|
+
configuring encoding options is to either set global values or to create a new
|
|
33
|
+
instance of the Encoder class and provide options in the initializer.
|
|
34
|
+
|
|
35
|
+
The globals that ActiveSupport uses for encoding are:
|
|
36
|
+
|
|
37
|
+
* ActiveSupport::JSON::Encoding.use_standard_json_time_format
|
|
38
|
+
* ActiveSupport::JSON::Encoding.escape_html_entities_in_json
|
|
39
|
+
* ActiveSupport::JSON::Encoding.time_precision
|
|
40
|
+
* ActiveSupport::JSON::Encoding.json_encoder
|
|
41
|
+
|
|
42
|
+
Those globals are aliased to also be accessed from the ActiveSupport module
|
|
43
|
+
directly so ActiveSupport::JSON::Encoding.time_precision can also be accessed
|
|
44
|
+
from ActiveSupport.time_precision. Oj makes use of these globals in mimicing
|
|
45
|
+
Rails after the Oj::Rails.set_encode() method is called. That also sets the
|
|
46
|
+
ActiveSupport.json_encoder to the Oj::Rails::Encoder class.
|
|
47
|
+
|
|
48
|
+
Options passed into a call to to_json() are passed to the as_json()
|
|
49
|
+
methods. These are mostly ignored by Oj and simply passed on without
|
|
50
|
+
modifications as per the guidelines. The exception to this are the options
|
|
51
|
+
specific to Oj such as the :circular option which it used to detect circular
|
|
52
|
+
references while encoding.
|
|
53
|
+
|
|
54
|
+
By default Oj acts like the ActiveSupport encoder and honors any changes in
|
|
55
|
+
the as_json() methods. There are some optimized Oj encoders for some
|
|
56
|
+
classes. When the optimized encoder it toggled the as_json() methods will not
|
|
57
|
+
be called for that class but instead the optimized version will be called. The
|
|
58
|
+
optimized version is the same as the ActiveSupport default encoding for a
|
|
59
|
+
given class. The optimized versions are toggled with the optimize() and
|
|
60
|
+
deoptimize() methods. There is a default optimized version for every class
|
|
61
|
+
that takes the visible attributes and encodes them but that may not be the
|
|
62
|
+
same as what Rails uses. Trial and error is the best approach for classes not
|
|
63
|
+
listed here.
|
|
64
|
+
|
|
65
|
+
The classes that can be put in optimized mode and are optimized when
|
|
66
|
+
Oj::Rails.optimize is called with no arguments are:
|
|
67
|
+
|
|
68
|
+
* Array
|
|
69
|
+
* BigDecimal
|
|
70
|
+
* Float
|
|
71
|
+
* Hash
|
|
72
|
+
* Range
|
|
73
|
+
* Regexp
|
|
74
|
+
* Time
|
|
75
|
+
* ActiveSupport::TimeWithZone
|
|
76
|
+
* ActionController::Parameters
|
|
77
|
+
* any class inheriting from ActiveRecord::Base
|
|
78
|
+
* any other class where all attributes should be dumped
|
|
79
|
+
|
|
80
|
+
The ActiveSupport decoder is the JSON.parse() method. Calling the
|
|
81
|
+
Oj::Rails.set_decoder() method replaces that method with the Oj equivalent.
|
|
82
|
+
|
|
83
|
+
### Notes:
|
|
84
|
+
|
|
85
|
+
1. Optimized Floats set the significant digits to 16. This is different than
|
|
86
|
+
Ruby which is used by the json gem and by Rails. Ruby varies the
|
|
87
|
+
significant digits which can be either 16 or 17 depending on the value.
|
|
88
|
+
|
|
89
|
+
2. Optimized Hashs do not collapse keys that become the same in the output. As
|
|
90
|
+
an example, a non-String object that has a to_s() method will become the
|
|
91
|
+
return value of the to_s() method in the output without checking to see if
|
|
92
|
+
that has already been used. This could occur is a mix of String and Symbols
|
|
93
|
+
are used as keys or if a other non-String objects such as Numerics are mixed
|
|
94
|
+
with numbers as Strings.
|
|
95
|
+
|
|
96
|
+
3. To verify Oj is being used turn on the Oj `:trace` option. Similar to the
|
|
97
|
+
Ruby Tracer Oj will then print out trace information. Another approach is
|
|
98
|
+
to turn on C extension tracing. Set `tracer = TracePoint.new(:c_call) do
|
|
99
|
+
|tp| p [tp.lineno, tp.event, tp.defined_class, tp.method_id] end` or, in
|
|
100
|
+
older Rubies, set `Tracer.display_c_call = true`.
|
|
101
|
+
|
|
102
|
+
For example:
|
|
103
|
+
|
|
104
|
+
```
|
|
105
|
+
require 'active_support/core_ext'
|
|
106
|
+
require 'active_support/json'
|
|
107
|
+
require 'oj'
|
|
108
|
+
Oj.optimize_rails
|
|
109
|
+
tracer.enable { Time.now.to_json }
|
|
110
|
+
# prints output including
|
|
111
|
+
....
|
|
112
|
+
[20, :c_call, #<Class:Oj::Rails::Encoder>, :new]
|
|
113
|
+
[20, :c_call, Oj::Rails::Encoder, :encode]
|
|
114
|
+
....
|
|
115
|
+
=> "\"2018-02-23T12:13:42.493-06:00\""
|
|
116
|
+
```
|
data/pages/Security.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# Security and Optimization
|
|
2
|
+
|
|
3
|
+
Two settings in Oj are useful for parsing but do expose a vulnerability if used
|
|
4
|
+
from an untrusted source. Symbolized keys can cause memory to be filled with
|
|
5
|
+
previous versions of ruby. Ruby 2.1 and below does not garbage collect
|
|
6
|
+
Symbols. The same is true for auto defining classes in all versions of ruby;
|
|
7
|
+
memory will also be exhausted if too many classes are automatically
|
|
8
|
+
defined. Auto defining is a useful feature during development and from trusted
|
|
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
|
|
11
|
+
sets and uses the most strict and safest options. It should be used by
|
|
12
|
+
developers who find it difficult to understand the options available in Oj.
|
|
13
|
+
|
|
14
|
+
The options in Oj are designed to provide flexibility to the developer. This
|
|
15
|
+
flexibility allows Objects to be serialized and deserialized. No methods are
|
|
16
|
+
ever called on these created Objects but that does not stop the developer from
|
|
17
|
+
calling methods on them. As in any system, check your inputs before working with
|
|
18
|
+
them. Taking an arbitrary `String` from a user and evaluating it is never a good
|
|
19
|
+
idea from an unsecure source. The same is true for `Object` attributes as they
|
|
20
|
+
are not more than `String`s. Always check inputs from untrusted sources.
|
data/pages/WAB.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# WAB mode
|
|
2
|
+
|
|
3
|
+
The `:wab` mode ignores all options except the indent option. Performance of
|
|
4
|
+
this mode is slightly faster than the :strict and :null modes. It is included
|
|
5
|
+
to support the [WABuR](https://github.com/ohler55/wabur) project.
|
|
6
|
+
|
|
7
|
+
Options other than the indentation are not supported since the encoding and
|
|
8
|
+
formats are defined by the API that is used to encode data being passed from
|
|
9
|
+
one components in a WAB system and allowing an option that would break the
|
|
10
|
+
data exchange is best not supported.
|
|
11
|
+
|
|
12
|
+
The mode encodes like the strict mode except the URI, Time, WAB::UUID, and
|
|
13
|
+
BigDecimal are supported.
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# encoding: UTF-8
|
|
3
|
+
|
|
4
|
+
$: << File.dirname(__FILE__)
|
|
5
|
+
%w(lib ext test).each do |dir|
|
|
6
|
+
$LOAD_PATH.unshift File.expand_path("../../#{dir}", __FILE__)
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
require 'minitest'
|
|
10
|
+
require 'minitest/autorun'
|
|
11
|
+
|
|
12
|
+
require 'sqlite3'
|
|
13
|
+
require 'active_record'
|
|
14
|
+
require 'oj'
|
|
15
|
+
|
|
16
|
+
#Oj.mimic_JSON()
|
|
17
|
+
Oj.default_options = {mode: :compat, indent: 2}
|
|
18
|
+
|
|
19
|
+
#ActiveRecord::Base.logger = Logger.new(STDERR)
|
|
20
|
+
|
|
21
|
+
ActiveRecord::Base.establish_connection(
|
|
22
|
+
:adapter => "sqlite3",
|
|
23
|
+
:database => ":memory:"
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
ActiveRecord::Schema.define do
|
|
27
|
+
create_table :users do |table|
|
|
28
|
+
table.column :first_name, :string
|
|
29
|
+
table.column :last_name, :string
|
|
30
|
+
table.column :email, :string
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
class User < ActiveRecord::Base
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
class ActiveTest < Minitest::Test
|
|
38
|
+
|
|
39
|
+
def test_active
|
|
40
|
+
User.find_or_create_by(first_name: "John", last_name: "Smith", email: "john@example.com")
|
|
41
|
+
User.find_or_create_by(first_name: "Joan", last_name: "Smith", email: "joan@example.com")
|
|
42
|
+
|
|
43
|
+
# Single instance.
|
|
44
|
+
assert_equal(%|{
|
|
45
|
+
"id":1,
|
|
46
|
+
"first_name":"John",
|
|
47
|
+
"last_name":"Smith",
|
|
48
|
+
"email":"john@example.com"
|
|
49
|
+
}
|
|
50
|
+
|, Oj.dump(User.first))
|
|
51
|
+
|
|
52
|
+
# Array of instances.
|
|
53
|
+
assert_equal(%|[
|
|
54
|
+
{
|
|
55
|
+
"id":1,
|
|
56
|
+
"first_name":"John",
|
|
57
|
+
"last_name":"Smith",
|
|
58
|
+
"email":"john@example.com"
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
"id":2,
|
|
62
|
+
"first_name":"Joan",
|
|
63
|
+
"last_name":"Smith",
|
|
64
|
+
"email":"joan@example.com"
|
|
65
|
+
}
|
|
66
|
+
]
|
|
67
|
+
|, Oj.dump(User.all))
|
|
68
|
+
|
|
69
|
+
# Single instance as json. (not Oj)
|
|
70
|
+
assert_equal(%|{"id":1,"first_name":"John","last_name":"Smith","email":"john@example.com"}|, User.first.to_json)
|
|
71
|
+
|
|
72
|
+
# Array of instances as json. (not Oj)
|
|
73
|
+
assert_equal(%|[{"id":1,"first_name":"John","last_name":"Smith","email":"john@example.com"},{"id":2,"first_name":"Joan","last_name":"Smith","email":"joan@example.com"}]|, User.all.to_json)
|
|
74
|
+
|
|
75
|
+
end
|
|
76
|
+
end
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# encoding: UTF-8
|
|
3
|
+
|
|
4
|
+
$: << File.dirname(__FILE__)
|
|
5
|
+
%w(lib ext test).each do |dir|
|
|
6
|
+
$LOAD_PATH.unshift File.expand_path("../../#{dir}", __FILE__)
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
require 'minitest'
|
|
10
|
+
require 'minitest/autorun'
|
|
11
|
+
|
|
12
|
+
require 'sqlite3'
|
|
13
|
+
require 'active_record'
|
|
14
|
+
require 'oj'
|
|
15
|
+
|
|
16
|
+
Oj.mimic_JSON()
|
|
17
|
+
Oj.default_options = {mode: :compat, indent: 2}
|
|
18
|
+
|
|
19
|
+
#ActiveRecord::Base.logger = Logger.new(STDERR)
|
|
20
|
+
|
|
21
|
+
ActiveRecord::Base.establish_connection(
|
|
22
|
+
:adapter => "sqlite3",
|
|
23
|
+
:database => ":memory:"
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
ActiveRecord::Schema.define do
|
|
27
|
+
create_table :users do |table|
|
|
28
|
+
table.column :first_name, :string
|
|
29
|
+
table.column :last_name, :string
|
|
30
|
+
table.column :email, :string
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
class User < ActiveRecord::Base
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
class ActiveTest < Minitest::Test
|
|
38
|
+
|
|
39
|
+
def test_active
|
|
40
|
+
User.find_or_create_by(first_name: "John", last_name: "Smith", email: "john@example.com")
|
|
41
|
+
User.find_or_create_by(first_name: "Joan", last_name: "Smith", email: "joan@example.com")
|
|
42
|
+
|
|
43
|
+
# Single instance.
|
|
44
|
+
assert_equal(%|{
|
|
45
|
+
"id":1,
|
|
46
|
+
"first_name":"John",
|
|
47
|
+
"last_name":"Smith",
|
|
48
|
+
"email":"john@example.com"
|
|
49
|
+
}
|
|
50
|
+
|, Oj.dump(User.first))
|
|
51
|
+
|
|
52
|
+
# Array of instances.
|
|
53
|
+
assert_equal(%|[
|
|
54
|
+
{
|
|
55
|
+
"id":1,
|
|
56
|
+
"first_name":"John",
|
|
57
|
+
"last_name":"Smith",
|
|
58
|
+
"email":"john@example.com"
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
"id":2,
|
|
62
|
+
"first_name":"Joan",
|
|
63
|
+
"last_name":"Smith",
|
|
64
|
+
"email":"joan@example.com"
|
|
65
|
+
}
|
|
66
|
+
]
|
|
67
|
+
|, Oj.dump(User.all))
|
|
68
|
+
|
|
69
|
+
# Single instance as json. (not Oj)
|
|
70
|
+
assert_equal(%|{
|
|
71
|
+
"id":1,
|
|
72
|
+
"first_name":"John",
|
|
73
|
+
"last_name":"Smith",
|
|
74
|
+
"email":"john@example.com"
|
|
75
|
+
}
|
|
76
|
+
|, User.first.to_json)
|
|
77
|
+
|
|
78
|
+
# Array of instances as json. (not Oj)
|
|
79
|
+
assert_equal(%|[
|
|
80
|
+
{
|
|
81
|
+
"id":1,
|
|
82
|
+
"first_name":"John",
|
|
83
|
+
"last_name":"Smith",
|
|
84
|
+
"email":"john@example.com"
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
"id":2,
|
|
88
|
+
"first_name":"Joan",
|
|
89
|
+
"last_name":"Smith",
|
|
90
|
+
"email":"joan@example.com"
|
|
91
|
+
}
|
|
92
|
+
]
|
|
93
|
+
|, User.all.to_json)
|
|
94
|
+
|
|
95
|
+
end
|
|
96
|
+
end
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# encoding: UTF-8
|
|
3
|
+
|
|
4
|
+
$: << File.dirname(__FILE__)
|
|
5
|
+
|
|
6
|
+
require 'helper'
|
|
7
|
+
#Oj.mimic_JSON
|
|
8
|
+
require 'rails/all'
|
|
9
|
+
|
|
10
|
+
require 'active_model'
|
|
11
|
+
require 'active_model_serializers'
|
|
12
|
+
require 'active_support/json'
|
|
13
|
+
require 'active_support/time'
|
|
14
|
+
require 'active_support/all'
|
|
15
|
+
|
|
16
|
+
require 'oj/active_support_helper'
|
|
17
|
+
|
|
18
|
+
Oj.mimic_JSON
|
|
19
|
+
|
|
20
|
+
class Category
|
|
21
|
+
include ActiveModel::Model
|
|
22
|
+
include ActiveModel::SerializerSupport
|
|
23
|
+
|
|
24
|
+
attr_accessor :id, :name
|
|
25
|
+
|
|
26
|
+
def initialize(id, name)
|
|
27
|
+
@id = id
|
|
28
|
+
@name = name
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
class CategorySerializer < ActiveModel::Serializer
|
|
33
|
+
attributes :id, :name
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
class MimicRails < Minitest::Test
|
|
37
|
+
|
|
38
|
+
def test_mimic_exception
|
|
39
|
+
begin
|
|
40
|
+
ActiveSupport::JSON.decode("{")
|
|
41
|
+
puts "Failed"
|
|
42
|
+
rescue ActiveSupport::JSON.parse_error
|
|
43
|
+
assert(true)
|
|
44
|
+
rescue Exception
|
|
45
|
+
assert(false, 'Expected a JSON::ParserError')
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def test_dump_string
|
|
50
|
+
Oj.default_options= {:indent => 2}
|
|
51
|
+
json = ActiveSupport::JSON.encode([1, true, nil])
|
|
52
|
+
assert_equal(%{[
|
|
53
|
+
1,
|
|
54
|
+
true,
|
|
55
|
+
null
|
|
56
|
+
]
|
|
57
|
+
}, json)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def test_dump_rational
|
|
61
|
+
Oj.default_options= {:indent => 2}
|
|
62
|
+
json = ActiveSupport::JSON.encode([1, true, Rational(1)])
|
|
63
|
+
assert_equal(%{[
|
|
64
|
+
1,
|
|
65
|
+
true,
|
|
66
|
+
"1/1"
|
|
67
|
+
]
|
|
68
|
+
}, json)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def test_dump_range
|
|
72
|
+
Oj.default_options= {:indent => 2}
|
|
73
|
+
json = ActiveSupport::JSON.encode([1, true, '01'..'12'])
|
|
74
|
+
assert_equal(%{[
|
|
75
|
+
1,
|
|
76
|
+
true,
|
|
77
|
+
"01..12"
|
|
78
|
+
]
|
|
79
|
+
}, json)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def test_dump_object
|
|
83
|
+
Oj.default_options= {:indent => 2}
|
|
84
|
+
category = Category.new(1, 'test')
|
|
85
|
+
serializer = CategorySerializer.new(category)
|
|
86
|
+
|
|
87
|
+
json = serializer.to_json()
|
|
88
|
+
puts "*** serializer.to_json() #{serializer.to_json()}"
|
|
89
|
+
json = serializer.as_json()
|
|
90
|
+
puts "*** serializer.as_json() #{serializer.as_json()}"
|
|
91
|
+
json = JSON.dump(serializer)
|
|
92
|
+
puts "*** JSON.dump(serializer) #{JSON.dump(serializer)}"
|
|
93
|
+
|
|
94
|
+
puts "*** category.to_json() #{category.to_json()}"
|
|
95
|
+
puts "*** category.as_json() #{category.as_json()}"
|
|
96
|
+
puts "*** JSON.dump(serializer) #{JSON.dump(category)}"
|
|
97
|
+
puts "*** Oj.dump(serializer) #{Oj.dump(category)}"
|
|
98
|
+
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def test_dump_object_array
|
|
102
|
+
Oj.default_options= {:indent => 2}
|
|
103
|
+
cat1 = Category.new(1, 'test')
|
|
104
|
+
cat2 = Category.new(2, 'test')
|
|
105
|
+
a = Array.wrap([cat1, cat2])
|
|
106
|
+
|
|
107
|
+
#serializer = CategorySerializer.new(a)
|
|
108
|
+
|
|
109
|
+
puts "*** a.to_json() #{a.to_json()}"
|
|
110
|
+
puts "*** a.as_json() #{a.as_json()}"
|
|
111
|
+
puts "*** JSON.dump(a) #{JSON.dump(a)}"
|
|
112
|
+
puts "*** Oj.dump(a) #{Oj.dump(a)}"
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def test_dump_time
|
|
116
|
+
Oj.default_options= {:indent => 2}
|
|
117
|
+
now = ActiveSupport::TimeZone['America/Chicago'].parse("2014-11-01 13:20:47")
|
|
118
|
+
json = Oj.dump(now, mode: :object, time_format: :xmlschema)
|
|
119
|
+
#puts "*** json: #{json}"
|
|
120
|
+
|
|
121
|
+
oj_dump = Oj.load(json, mode: :object, time_format: :xmlschema)
|
|
122
|
+
#puts "Now: #{now}\n Oj: #{oj_dump}"
|
|
123
|
+
assert_equal("2014-11-01T13:20:47-05:00", oj_dump.xmlschema)
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
end # MimicRails
|