oj 3.10.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (167) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +104 -0
  4. data/ext/oj/buf.h +103 -0
  5. data/ext/oj/cache8.c +107 -0
  6. data/ext/oj/cache8.h +48 -0
  7. data/ext/oj/circarray.c +68 -0
  8. data/ext/oj/circarray.h +23 -0
  9. data/ext/oj/code.c +235 -0
  10. data/ext/oj/code.h +42 -0
  11. data/ext/oj/compat.c +299 -0
  12. data/ext/oj/custom.c +1218 -0
  13. data/ext/oj/dump.c +1249 -0
  14. data/ext/oj/dump.h +96 -0
  15. data/ext/oj/dump_compat.c +975 -0
  16. data/ext/oj/dump_leaf.c +252 -0
  17. data/ext/oj/dump_object.c +844 -0
  18. data/ext/oj/dump_strict.c +434 -0
  19. data/ext/oj/encode.h +45 -0
  20. data/ext/oj/err.c +57 -0
  21. data/ext/oj/err.h +70 -0
  22. data/ext/oj/extconf.rb +53 -0
  23. data/ext/oj/fast.c +1771 -0
  24. data/ext/oj/hash.c +163 -0
  25. data/ext/oj/hash.h +46 -0
  26. data/ext/oj/hash_test.c +512 -0
  27. data/ext/oj/mimic_json.c +890 -0
  28. data/ext/oj/object.c +775 -0
  29. data/ext/oj/odd.c +231 -0
  30. data/ext/oj/odd.h +44 -0
  31. data/ext/oj/oj.c +1723 -0
  32. data/ext/oj/oj.h +387 -0
  33. data/ext/oj/parse.c +1134 -0
  34. data/ext/oj/parse.h +112 -0
  35. data/ext/oj/rails.c +1528 -0
  36. data/ext/oj/rails.h +21 -0
  37. data/ext/oj/reader.c +231 -0
  38. data/ext/oj/reader.h +151 -0
  39. data/ext/oj/resolve.c +102 -0
  40. data/ext/oj/resolve.h +14 -0
  41. data/ext/oj/rxclass.c +147 -0
  42. data/ext/oj/rxclass.h +27 -0
  43. data/ext/oj/saj.c +714 -0
  44. data/ext/oj/scp.c +224 -0
  45. data/ext/oj/sparse.c +924 -0
  46. data/ext/oj/stream_writer.c +363 -0
  47. data/ext/oj/strict.c +212 -0
  48. data/ext/oj/string_writer.c +534 -0
  49. data/ext/oj/trace.c +79 -0
  50. data/ext/oj/trace.h +28 -0
  51. data/ext/oj/util.c +136 -0
  52. data/ext/oj/util.h +19 -0
  53. data/ext/oj/val_stack.c +118 -0
  54. data/ext/oj/val_stack.h +185 -0
  55. data/ext/oj/wab.c +631 -0
  56. data/lib/oj.rb +21 -0
  57. data/lib/oj/active_support_helper.rb +41 -0
  58. data/lib/oj/bag.rb +88 -0
  59. data/lib/oj/easy_hash.rb +52 -0
  60. data/lib/oj/error.rb +22 -0
  61. data/lib/oj/json.rb +176 -0
  62. data/lib/oj/mimic.rb +267 -0
  63. data/lib/oj/saj.rb +66 -0
  64. data/lib/oj/schandler.rb +142 -0
  65. data/lib/oj/state.rb +131 -0
  66. data/lib/oj/version.rb +5 -0
  67. data/pages/Advanced.md +22 -0
  68. data/pages/Compatibility.md +25 -0
  69. data/pages/Custom.md +23 -0
  70. data/pages/Encoding.md +65 -0
  71. data/pages/JsonGem.md +79 -0
  72. data/pages/Modes.md +155 -0
  73. data/pages/Options.md +287 -0
  74. data/pages/Rails.md +155 -0
  75. data/pages/Security.md +20 -0
  76. data/pages/WAB.md +13 -0
  77. data/test/_test_active.rb +76 -0
  78. data/test/_test_active_mimic.rb +96 -0
  79. data/test/_test_mimic_rails.rb +126 -0
  80. data/test/activerecord/result_test.rb +27 -0
  81. data/test/activesupport4/decoding_test.rb +108 -0
  82. data/test/activesupport4/encoding_test.rb +531 -0
  83. data/test/activesupport4/test_helper.rb +41 -0
  84. data/test/activesupport5/abstract_unit.rb +45 -0
  85. data/test/activesupport5/decoding_test.rb +133 -0
  86. data/test/activesupport5/encoding_test.rb +500 -0
  87. data/test/activesupport5/encoding_test_cases.rb +98 -0
  88. data/test/activesupport5/test_helper.rb +72 -0
  89. data/test/activesupport5/time_zone_test_helpers.rb +39 -0
  90. data/test/activesupport6/abstract_unit.rb +44 -0
  91. data/test/activesupport6/decoding_test.rb +133 -0
  92. data/test/activesupport6/encoding_test.rb +507 -0
  93. data/test/activesupport6/encoding_test_cases.rb +98 -0
  94. data/test/activesupport6/test_common.rb +17 -0
  95. data/test/activesupport6/test_helper.rb +163 -0
  96. data/test/activesupport6/time_zone_test_helpers.rb +39 -0
  97. data/test/bar.rb +35 -0
  98. data/test/baz.rb +16 -0
  99. data/test/files.rb +29 -0
  100. data/test/foo.rb +52 -0
  101. data/test/helper.rb +26 -0
  102. data/test/isolated/shared.rb +308 -0
  103. data/test/isolated/test_mimic_after.rb +13 -0
  104. data/test/isolated/test_mimic_alone.rb +12 -0
  105. data/test/isolated/test_mimic_as_json.rb +45 -0
  106. data/test/isolated/test_mimic_before.rb +13 -0
  107. data/test/isolated/test_mimic_define.rb +28 -0
  108. data/test/isolated/test_mimic_rails_after.rb +22 -0
  109. data/test/isolated/test_mimic_rails_before.rb +21 -0
  110. data/test/isolated/test_mimic_redefine.rb +15 -0
  111. data/test/json_gem/json_addition_test.rb +216 -0
  112. data/test/json_gem/json_common_interface_test.rb +148 -0
  113. data/test/json_gem/json_encoding_test.rb +107 -0
  114. data/test/json_gem/json_ext_parser_test.rb +20 -0
  115. data/test/json_gem/json_fixtures_test.rb +35 -0
  116. data/test/json_gem/json_generator_test.rb +383 -0
  117. data/test/json_gem/json_generic_object_test.rb +90 -0
  118. data/test/json_gem/json_parser_test.rb +470 -0
  119. data/test/json_gem/json_string_matching_test.rb +42 -0
  120. data/test/json_gem/test_helper.rb +18 -0
  121. data/test/perf.rb +107 -0
  122. data/test/perf_compat.rb +130 -0
  123. data/test/perf_fast.rb +164 -0
  124. data/test/perf_file.rb +64 -0
  125. data/test/perf_object.rb +138 -0
  126. data/test/perf_saj.rb +109 -0
  127. data/test/perf_scp.rb +151 -0
  128. data/test/perf_simple.rb +287 -0
  129. data/test/perf_strict.rb +145 -0
  130. data/test/perf_wab.rb +131 -0
  131. data/test/prec.rb +23 -0
  132. data/test/sample.rb +54 -0
  133. data/test/sample/change.rb +14 -0
  134. data/test/sample/dir.rb +19 -0
  135. data/test/sample/doc.rb +36 -0
  136. data/test/sample/file.rb +48 -0
  137. data/test/sample/group.rb +16 -0
  138. data/test/sample/hasprops.rb +16 -0
  139. data/test/sample/layer.rb +12 -0
  140. data/test/sample/line.rb +20 -0
  141. data/test/sample/oval.rb +10 -0
  142. data/test/sample/rect.rb +10 -0
  143. data/test/sample/shape.rb +35 -0
  144. data/test/sample/text.rb +20 -0
  145. data/test/sample_json.rb +37 -0
  146. data/test/test_compat.rb +502 -0
  147. data/test/test_custom.rb +527 -0
  148. data/test/test_debian.rb +53 -0
  149. data/test/test_fast.rb +470 -0
  150. data/test/test_file.rb +239 -0
  151. data/test/test_gc.rb +49 -0
  152. data/test/test_hash.rb +29 -0
  153. data/test/test_integer_range.rb +72 -0
  154. data/test/test_null.rb +376 -0
  155. data/test/test_object.rb +1027 -0
  156. data/test/test_rails.rb +26 -0
  157. data/test/test_saj.rb +186 -0
  158. data/test/test_scp.rb +433 -0
  159. data/test/test_strict.rb +433 -0
  160. data/test/test_various.rb +719 -0
  161. data/test/test_wab.rb +307 -0
  162. data/test/test_writer.rb +380 -0
  163. data/test/tests.rb +25 -0
  164. data/test/tests_mimic.rb +14 -0
  165. data/test/tests_mimic_addition.rb +7 -0
  166. data/test/zoo.rb +13 -0
  167. metadata +381 -0
@@ -0,0 +1,155 @@
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
+ ### Usage in Rails 3
84
+
85
+ To support Rails 3 you can create a new module mixin to prepend to controllers:
86
+
87
+ ```ruby
88
+ require 'oj'
89
+
90
+ module OjJsonEncoder
91
+ def render(options = nil, extra_options = {}, &block)
92
+ if options && options.is_a?(Hash) && options[:json]
93
+ obj = options.delete(:json)
94
+ obj = Oj.dump(obj, :mode => :rails) unless obj.is_a?(String)
95
+ options[:text] = obj
96
+ response.content_type ||= Mime::JSON
97
+ end
98
+ super
99
+ end
100
+ end
101
+ ```
102
+
103
+ Usage:
104
+
105
+ ```ruby
106
+ class MyController < ApplicationController
107
+ prepend OjJsonEncoder
108
+ def index
109
+ render :json => { :hello => 'world' }
110
+ end
111
+ end
112
+ ```
113
+
114
+ ### Older Ruby Version Support (Pre 2.3.0)
115
+
116
+ If you are using an older version of Ruby, you can pin `oj` to an earlier version in your Gemfile:
117
+
118
+ ```ruby
119
+ gem 'oj', '3.7.12'
120
+ ```
121
+
122
+ ### Notes:
123
+
124
+ 1. Optimized Floats set the significant digits to 16. This is different than
125
+ Ruby which is used by the json gem and by Rails. Ruby varies the
126
+ significant digits which can be either 16 or 17 depending on the value.
127
+
128
+ 2. Optimized Hashs do not collapse keys that become the same in the output. As
129
+ an example, a non-String object that has a `to_s()` method will become the
130
+ return value of the `to_s()` method in the output without checking to see if
131
+ that has already been used. This could occur is a mix of String and Symbols
132
+ are used as keys or if a other non-String objects such as Numerics are mixed
133
+ with numbers as Strings.
134
+
135
+ 3. To verify Oj is being used turn on the Oj `:trace` option. Similar to the
136
+ Ruby Tracer Oj will then print out trace information. Another approach is
137
+ to turn on C extension tracing. Set `tracer = TracePoint.new(:c_call) do
138
+ |tp| p [tp.lineno, tp.event, tp.defined_class, tp.method_id] end` or, in
139
+ older Rubies, set `Tracer.display_c_call = true`.
140
+
141
+ For example:
142
+
143
+ ```
144
+ require 'active_support/core_ext'
145
+ require 'active_support/json'
146
+ require 'oj'
147
+ Oj.optimize_rails
148
+ tracer.enable { Time.now.to_json }
149
+ # prints output including
150
+ ....
151
+ [20, :c_call, #<Class:Oj::Rails::Encoder>, :new]
152
+ [20, :c_call, Oj::Rails::Encoder, :encode]
153
+ ....
154
+ => "\"2018-02-23T12:13:42.493-06:00\""
155
+ ```
@@ -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.safe_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.
@@ -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