multi_json 1.19.1 → 1.21.1

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.
data/CHANGELOG.md DELETED
@@ -1,228 +0,0 @@
1
- # Changelog
2
-
3
- ## 1.19.1
4
- * [Restore deprecated encode/decode methods](https://github.com/sferik/multi_json/commit/c5bf2fc95dfdde6b30d63fefb0b2f4aa29633969)
5
-
6
- ## 1.19.0
7
- * [Fix serialization of ActiveSupport-enhanced objects](https://github.com/sferik/multi_json/commit/03a367813ebd7ed87eb22ea05249cc6453bb3c10)
8
-
9
- ## 1.18.0
10
- * [Fix conflict between JSON gem and ActiveSupport](https://github.com/intridea/multi_json/issues/222)
11
-
12
- ## 1.17.0
13
- * [Revert minimum ruby version requirement](https://github.com/sferik/multi_json/pull/16)
14
-
15
- ## 1.16.0
16
- * [Remove NSJSONSerialization](https://github.com/sferik/multi_json/commit/0423d3b5886e93405f4c2221687b7e3329bd2940)
17
- * [Stop referencing JSON::PRETTY\_STATE\_PROTOTYPE](https://github.com/sferik/multi_json/commit/58094d7a0583bf1f5052886806a032c00f16ffc5)
18
- * [Drop support for Ruby versions < 3.2](https://github.com/sferik/multi_json/commit/ff3b42c4bc26cd6512914b7e5321976e948985dc)
19
- * [Move repo from @intridea to @sferik](https://github.com/sferik/multi_json/commit/e87aeadbc9b9aa6df79818fa01bfc5fa959d8474)
20
- * [JsonCommon: force encoding to UTF-8, not binary](https://github.com/sferik/multi_json/commit/34dd0247de07f2703c7d42a42d4cefc73635f3cc)
21
- * [Stop setting defaults in JsonCommon](https://github.com/sferik/multi_json/commit/d5f9e6e72b99a7def695f430f72c8365998de625)
22
- * [Make json\_pure an alias of json\_gem](https://github.com/sferik/multi_json/commit/9ff7c3dcbe3650e712b38e636ad19061a4c08d1a)
23
-
24
- ## 1.15.0
25
- * [Improve detection of json_gem adapter](https://github.com/sferik/multi_json/commit/62d54019b17ebf83b28c8deb871a02a122e7d9cf)
26
-
27
- ## 1.14.1
28
- * [Fix a warning in Ruby 2.7](https://github.com/sferik/multi_json/commit/26a94ab8c78a394cc237e2ea292c1de4f6ed30d7)
29
-
30
- ## 1.14.0
31
- * [Support Oj 3.x gem](https://github.com/sferik/multi_json/commit/5d8febdbebc428882811b90d514f3628617a61d5)
32
-
33
- ## 1.13.1
34
- * [Fix missing stdlib set dependency in oj adapter](https://github.com/sferik/multi_json/commit/c4ff66e7bee6fb4f45e54429813d7fada1c152b8)
35
-
36
- ## 1.13.0
37
- * [Make Oj adapter handle JSON::ParseError correctly](https://github.com/sferik/multi_json/commit/275e3ffd8169797c510d23d9ef5b8b07e64c3b42)
38
-
39
- ## 1.12.2
40
- * [Renew gem certificate](https://github.com/sferik/multi_json/commit/57922d898c6eb587cc9a28ba5724c11e81724700)
41
-
42
- ## 1.12.1
43
- * [Prevent memory leak in OptionsCache](https://github.com/sferik/multi_json/commit/aa7498199ad272f3d4a13750d7c568a66047e2ee)
44
-
45
- ## 1.12.0
46
- * [Introduce global options cache to improve peroformance](https://github.com/sferik/multi_json/commit/7aaef2a1bc2b83c95e4208b12dad5d1d87ff20a6)
47
-
48
- ## 1.11.2
49
- * [Only pass one argument to JrJackson when two is not supported](https://github.com/sferik/multi_json/commit/e798fa517c817fc706982d3f3c61129b6651d601)
50
-
51
- ## 1.11.1
52
- * [Dump method passes options throught for JrJackson adapter](https://github.com/sferik/multi_json/commit/3c730fd12135c3e7bf212f878958004908f13909)
53
-
54
- ## 1.11.0
55
- * [Make all adapters read IO object before load](https://github.com/sferik/multi_json/commit/167f559e18d4efee05e1f160a2661d16dbb215d4)
56
-
57
- ## 1.10.1
58
- * [Explicitly require stringio for Gson adapter](https://github.com/sferik/multi_json/commit/623ec8142d4a212fa0db763bb71295789a119929)
59
- * [Do not read StringIO object before passing it to JrJackson](https://github.com/sferik/multi_json/commit/a6dc935df08e7b3d5d701fbb9298384c96df0fde)
60
-
61
- ## 1.10.0
62
- * [Performance tweaks](https://github.com/sferik/multi_json/commit/58724acfed31866d079eaafb1cd824e341ade287)
63
-
64
- ## 1.9.3
65
- * [Convert indent option to Fixnum before passing to Oj](https://github.com/sferik/multi_json/commit/826fc5535b863b74fc9f981dfdda3e26f1ee4e5b)
66
-
67
- ## 1.9.2
68
- * [Enable use_to_json option for Oj adapter by default](https://github.com/sferik/multi_json/commit/76a4aaf697b10bbabd5d535d83cf1149efcfe5c7)
69
-
70
- ## 1.9.1
71
- * [Remove unused LoadError file](https://github.com/sferik/multi_json/commit/65dedd84d59baeefc25c477fedf0bbe85e7ce2cd)
72
-
73
- ## 1.9.0
74
- * [Rename LoadError to ParseError](https://github.com/sferik/multi_json/commit/4abb98fe3a90b2a7b3d1594515c8a06042b4a27d)
75
- * [Adapter load failure throws AdapterError instead of ArgumentError](https://github.com/sferik/multi_json/commit/4da612b617bd932bb6fa1cc4c43210327f98f271)
76
-
77
- ## 1.8.4
78
- * [Make Gson adapter explicitly read StringIO object](https://github.com/sferik/multi_json/commit/b58b498747ff6e94f41488c971b2a30a98760ef2)
79
-
80
- ## 1.8.3
81
- * [Make JrJackson explicitly read StringIO objects](https://github.com/sferik/multi_json/commit/e1f162d5b668e5e4db5afa175361a601a8aa2b05)
82
- * [Prevent calling #downcase on alias symbols](https://github.com/sferik/multi_json/commit/c1cf075453ce0110f7decc4f906444b1233bb67c)
83
-
84
- ## 1.8.2
85
- * [Downcase adapter string name for OS compatibility](https://github.com/sferik/multi_json/commit/b8e15a032247a63f1410d21a18add05035f3fa66)
86
-
87
- ## 1.8.1
88
- * [Let the adapter handle strings with invalid encoding](https://github.com/sferik/multi_json/commit/6af2bf87b89f44eabf2ae9ca96779febc65ea94b)
89
-
90
- ## 1.8.0
91
- * [Raise MultiJson::LoadError on blank input](https://github.com/sferik/multi_json/commit/c44f9c928bb25fe672246ad394b3e5b991be32e6)
92
-
93
- ## 1.7.9
94
- * [Explicitly require json gem code even when constant is defined](https://github.com/sferik/multi_json/commit/36f7906c66477eb4b55b7afeaa3684b6db69eff2)
95
-
96
- ## 1.7.8
97
- * [Reorder JrJackson before json_gem](https://github.com/sferik/multi_json/commit/315b6e460b6e4dcdb6c82e04e4be8ee975d395da)
98
- * [Update vendored OkJson to version 43](https://github.com/sferik/multi_json/commit/99a6b662f6ef4036e3ee94d7eb547fa72fb2ab50)
99
-
100
- ## 1.7.7
101
- * [Fix options caching issues](https://github.com/sferik/multi_json/commit/a3f14c3661688c5927638fa6088c7b46a67e875e)
102
-
103
- ## 1.7.6
104
- * [Bring back MultiJson::VERSION constant](https://github.com/sferik/multi_json/commit/31b990c2725e6673bf8ce57540fe66b57a751a72)
105
-
106
- ## 1.7.5
107
- * [Fix warning '*' interpreted as argument prefix](https://github.com/sferik/multi_json/commit/b698962c7f64430222a1f06430669706a47aff89)
108
- * [Remove stdlib warning](https://github.com/sferik/multi_json/commit/d06eec6b7996ac8b4ff0e2229efd835379b0c30f)
109
-
110
- ## 1.7.4
111
- * [Cache options for better performance](https://github.com/sferik/multi_json/commit/8a26ee93140c4bed36194ed9fb887a1b6919257b)
112
-
113
- ## 1.7.3
114
- * [Require json/ext to ensure extension version gets loaded for json_gem](https://github.com/sferik/multi_json/commit/942686f7e8597418c6f90ee69e1d45242fac07b1)
115
- * [Rename JrJackson](https://github.com/sferik/multi_json/commit/078de7ba8b6035343c3e96b4767549e9ec43369a)
116
- * [Prefer JrJackson to JSON gem if present](https://github.com/sferik/multi_json/commit/af8bd9799a66855f04b3aff1c488485950cec7bf)
117
- * [Print a warning if outdated gem versions are used](https://github.com/sferik/multi_json/commit/e7438e7ba2be0236cfa24c2bb9ad40ee821286d1)
118
- * [Loosen required_rubygems_version for compatibility with Ubuntu 10.04](https://github.com/sferik/multi_json/commit/59fad014e8fe41dbc6f09485ea0dc21fc42fd7a7)
119
-
120
- ## 1.7.2
121
- * [Rename Jrjackson adapter to JrJackson](https://github.com/sferik/multi_json/commit/b36dc915fc0e6548cbad06b5db6f520e040c9c8b)
122
- * [Implement jrjackson -> jr_jackson alias for back-compatability](https://github.com/sferik/multi_json/commit/aa50ab8b7bb646b8b75d5d65dfeadae8248a4f10)
123
- * [Update vendored OkJson module](https://github.com/sferik/multi_json/commit/30a3f474e17dd86a697c3fab04f468d1a4fd69d7)
124
-
125
- ## 1.7.1
126
- * [Fix capitalization of JrJackson class](https://github.com/sferik/multi_json/commit/5373a5e38c647f02427a0477cb8e0e0dafad1b8d)
127
-
128
- ## 1.7.0
129
- * [Add load_options/dump_options to MultiJson](https://github.com/sferik/multi_json/commit/a153956be6b0df06ea1705ce3c1ff0b5b0e27ea5)
130
- * [MultiJson does not modify arguments](https://github.com/sferik/multi_json/commit/58525b01c4c2f6635ba2ac13d6fd987b79f3962f)
131
- * [Enable quirks_mode by default for json_gem/json_pure adapters](https://github.com/sferik/multi_json/commit/1fd4e6635c436515b7d7d5a0bee4548de8571520)
132
- * [Add JrJackson adapter](https://github.com/sferik/multi_json/commit/4dd86fa96300aaaf6d762578b9b31ea82adb056d)
133
- * [Raise ArgumentError on bad adapter input](https://github.com/sferik/multi_json/commit/911a3756bdff2cb5ac06497da3fa3e72199cb7ad)
134
-
135
- ## 1.6.1
136
- * [Revert "Use JSON.generate instead of #to_json"](https://github.com/sferik/multi_json/issues/86)
137
-
138
- ## 1.6.0
139
- * [Add gson.rb support](https://github.com/intridea/multi_json/pull/71)
140
- * [Add MultiJson.default_options](https://github.com/intridea/multi_json/pull/70)
141
- * [Add MultiJson.with_adapter](https://github.com/intridea/multi_json/pull/67)
142
- * [Stringify all possible keys for ok_json](https://github.com/intridea/multi_json/pull/66)
143
- * [Use JSON.generate instead of #to_json](https://github.com/sferik/multi_json/issues/73)
144
- * [Alias MultiJson::DecodeError to MultiJson::LoadError](https://github.com/intridea/multi_json/pull/79)
145
-
146
- ## 1.5.1
147
- * [Do not allow Oj or JSON to create symbols by searching for classes](https://github.com/sferik/multi_json/commit/193e28cf4dc61b6e7b7b7d80f06f74c76df65c41)
148
-
149
- ## 1.5.0
150
- * [Add MultiJson.with\_adapter method](https://github.com/sferik/multi_json/commit/d14c5d28cae96557a0421298621b9499e1f28104)
151
- * [Stringify all possible keys for ok\_json](https://github.com/sferik/multi_json/commit/73998074058e1e58c557ffa7b9541d486d6041fa)
152
-
153
- ## 1.4.0
154
- * [Allow load/dump of JSON fragments](https://github.com/sferik/multi_json/commit/707aae7d48d39c85b38febbd2c210ba87f6e4a36)
155
-
156
- ## 1.3.7
157
- * [Fix rescue clause for MagLev](https://github.com/sferik/multi_json/commit/39abdf50199828c50e85b2ce8f8ba31fcbbc9332)
158
- * [Remove unnecessary check for string version of options key](https://github.com/sferik/multi_json/commit/660101b70e962b3c007d0b90d45944fa47d13ec4)
159
- * [Explicitly set default adapter when adapter is set to nil or false](https://github.com/sferik/multi_json/commit/a9e587d5a63eafb4baee9fb211265e4dd96a26bc)
160
- * [Fix Oj ParseError mapping for Oj 1.4.0](https://github.com/sferik/multi_json/commit/7d9045338cc9029401c16f3c409d54ce97f275e2)
161
-
162
- ## 1.3.6
163
- * [Allow adapter-specific options to be passed through to Oj](https://github.com/sferik/multi_json/commit/d0e5feeebcba0bc69400dd203a295f5c30971223)
164
-
165
- ## 1.3.5
166
- * [Add pretty support to Oj adapter](https://github.com/sferik/multi_json/commit/0c8f75f03020c53bcf4c6be258faf433d24b2c2b)
167
-
168
- ## 1.3.4
169
- * [Use class \<\< self instead of module\_function to create aliases](https://github.com/sferik/multi_json/commit/ba1451c4c48baa297e049889be241a424cb05980)
170
-
171
- ## 1.3.3
172
- * [Remove deprecation warnings](https://github.com/sferik/multi_json/commit/36b524e71544eb0186826a891bcc03b2820a008f)
173
-
174
- ## 1.3.2
175
- * [Add ability to use adapter per call](https://github.com/sferik/multi_json/commit/106bbec469d5d0a832bfa31fffcb8c0f0cdc9bd3)
176
- * [Add and deprecate default\_engine method](https://github.com/sferik/multi_json/commit/fc3df0c7a3e2ab9ce0c2c7e7617a4da97dd13f6e)
177
-
178
- ## 1.3.1
179
- * [Only warn once for each instance a deprecated method is called](https://github.com/sferik/multi_json/commit/e21d6eb7da74b3f283995c1d27d5880e75f0ae84)
180
-
181
- ## 1.3.0
182
- * [Implement load/dump; deprecate decode/encode](https://github.com/sferik/multi_json/commit/e90fd6cb1b0293eb0c73c2f4eb0f7a1764370216)
183
- * [Rename engines to adapters](https://github.com/sferik/multi_json/commit/ae7fd144a7949a9c221dcaa446196ec23db908df)
184
-
185
- ## 1.2.0
186
- * [Add support for Oj](https://github.com/sferik/multi_json/commit/acd06b233edabe6c44f226873db7b49dab560c60)
187
-
188
- ## 1.1.0
189
- * [NSJSONSerialization support for MacRuby](https://github.com/sferik/multi_json/commit/f862e2fc966cac8867fe7da3997fc76e8a6cf5d4)
190
-
191
- ## 1.0.4
192
- * [Set data context to DecodeError exception](https://github.com/sferik/multi_json/commit/19ddafd44029c6681f66fae2a0f6eabfd0f85176)
193
- * [Allow ok\_json to fallback to to\_json](https://github.com/sferik/multi_json/commit/c157240b1193b283d06d1bd4d4b5b06bcf3761f8)
194
- * [Add warning when using ok\_json](https://github.com/sferik/multi_json/commit/dd4b68810c84f826fb98f9713bfb29ab96888d57)
195
- * [Options can be passed to an engine on encode](https://github.com/sferik/multi_json/commit/e0a7ff5d5ff621ffccc61617ed8aeec5816e81f7)
196
-
197
- ## 1.0.3
198
- * [Array support for stringify\_keys](https://github.com/sferik/multi_json/commit/644d1c5c7c7f6a27663b11668527b346094e38b9)
199
- * [Array support for symbolize\_keys](https://github.com/sferik/multi_json/commit/c885377d47a2aa39cb0d971fea78db2d2fa479a7)
200
-
201
- ## 1.0.2
202
- * [Allow encoding of rootless JSON when ok\_json is used](https://github.com/sferik/multi_json/commit/d1cde7de97cb0f6152aef8daf14037521cdce8c6)
203
-
204
- ## 1.0.1
205
- * [Correct an issue with ok\_json not being returned as the default engine](https://github.com/sferik/multi_json/commit/d33c141619c54cccd770199694da8fd1bd8f449d)
206
-
207
- ## 1.0.0
208
- * [Remove ActiveSupport::JSON support](https://github.com/sferik/multi_json/commit/c2f4140141d785a24b3f56e58811b0e561b37f6a)
209
- * [Fix @engine ivar warning](https://github.com/sferik/multi_json/commit/3b978a8995721a8dffedc3b75a7f49e5494ec553)
210
- * [Only rescue from parsing errors during decoding, not any StandardError](https://github.com/sferik/multi_json/commit/391d00b5e85294d42d41347605d8d46b4a7f66cc)
211
- * [Rename okjson engine and vendored lib to ok\_json](https://github.com/sferik/multi_json/commit/5bd1afc977a8208ddb0443e1d57cb79665c019f1)
212
- * [Add StringIO support to json gem and ok\_json](https://github.com/sferik/multi_json/commit/1706b11568db7f50af451fce5f4d679aeb3bbe8f)
213
-
214
- ## 0.0.5
215
- * [Trap all JSON decoding errors; raise MultiJson::DecodeError](https://github.com/sferik/multi_json/commit/dea9a1aef6dd1212aa1e5a37ab1669f9b045b732)
216
-
217
- ## 0.0.4
218
- * [Fix default\_engine check for json gem](https://github.com/sferik/multi_json/commit/caced0c4e8c795922a109ebc00c3c4fa8635bed8)
219
- * [Make requirement mapper an Array to preserve order in Ruby versions \< 1.9](https://github.com/sferik/multi_json/commit/526f5f29a42131574a088ad9bbb43d7f48439b2c)
220
-
221
- ## 0.0.3
222
- * [Improve defaulting and documentation](https://github.com/sferik/twitter/commit/3a0e41b9e4b0909201045fa47704b78c9d949b73)
223
-
224
- ## 0.0.2
225
- * [Rename to multi\_json](https://github.com/sferik/twitter/commit/461ab89ce071c8c9fabfc183581e0ec523788b62)
226
-
227
- ## 0.0.1
228
- * [Initial commit](https://github.com/sferik/twitter/commit/518c21ab299c500527491e6c049ab2229e22a805)
data/CONTRIBUTING.md DELETED
@@ -1,52 +0,0 @@
1
- ## Contributing
2
- In the spirit of [free software][free-sw], **everyone** is encouraged to help
3
- improve this project.
4
-
5
- [free-sw]: http://www.fsf.org/licensing/essays/free-sw.html
6
-
7
- Here are some ways *you* can contribute:
8
-
9
- * by using alpha, beta, and prerelease versions
10
- * by reporting bugs
11
- * by suggesting new features
12
- * by writing or editing documentation
13
- * by writing tests
14
- * by writing code (**no patch is too small**: fix typos, add comments, clean up
15
- inconsistent whitespace)
16
- * by refactoring code
17
- * by closing [issues][]
18
- * by reviewing patches
19
-
20
- [issues]: https://github.com/sferik/multi_json/issues
21
-
22
- ## Submitting an Issue
23
- We use the [GitHub issue tracker][issues] to track bugs and features. Before
24
- submitting a bug report or feature request, check to make sure it hasn't
25
- already been submitted. When submitting a bug report, please include a
26
- [Gist][gist] that includes a stack trace and any details that may be necessary
27
- to reproduce the bug, including your gem version, Ruby version, and operating
28
- system. Ideally, a bug report should include a pull request with failing tests.
29
-
30
- [gist]: https://gist.github.com/
31
-
32
- ## Submitting a Pull Request
33
- 1. [Fork the repository.][fork]
34
- 2. [Create a topic branch.][branch]
35
- 3. Add tests for your unimplemented feature or bug fix.
36
- 4. Run `bundle exec rake test`. If your tests pass, return to step 3.
37
- 5. Implement your feature or bug fix.
38
- 6. Run `bundle exec rake test`. If your tests fail, return to step 5.
39
- 7. Run `open coverage/index.html`. If your changes are not completely covered
40
- by your tests, return to step 3.
41
- 8. Run `bundle exec rake mutant` and kill any surviving mutants.
42
- 9. Run `bundle exec rake lint` and fix any offenses.
43
- 10. Run `bundle exec rake yardstick` and add any missing documentation.
44
- 11. Add, commit, and push your changes.
45
- 12. [Submit a pull request][pr] that summarizes *what* changes you made and
46
- *why* you made them. If you made any significant decisions along the way,
47
- describe the options you considered and how you thought about the
48
- tradeoffs.
49
-
50
- [fork]: http://help.github.com/fork-a-repo/
51
- [branch]: http://learn.github.com/p/branching.html
52
- [pr]: http://help.github.com/send-pull-requests/
@@ -1,37 +0,0 @@
1
- require "gson"
2
- require_relative "../adapter"
3
-
4
- module MultiJson
5
- module Adapters
6
- # Use the gson.rb library to dump/load.
7
- class Gson < Adapter
8
- ParseError = ::Gson::DecodeError
9
-
10
- # Parse a JSON string into a Ruby object
11
- #
12
- # @api private
13
- # @param string [String] JSON string to parse
14
- # @param options [Hash] parsing options
15
- # @return [Object] parsed Ruby object
16
- #
17
- # @example Parse JSON string
18
- # adapter.load('{"key":"value"}') #=> {"key" => "value"}
19
- def load(string, options = {})
20
- ::Gson::Decoder.new(options).decode(string)
21
- end
22
-
23
- # Serialize a Ruby object to JSON
24
- #
25
- # @api private
26
- # @param object [Object] object to serialize
27
- # @param options [Hash] serialization options
28
- # @return [String] JSON string
29
- #
30
- # @example Serialize object to JSON
31
- # adapter.dump({key: "value"}) #=> '{"key":"value"}'
32
- def dump(object, options = {})
33
- ::Gson::Encoder.new(options).encode(object)
34
- end
35
- end
36
- end
37
- end
@@ -1,52 +0,0 @@
1
- require "jrjackson" unless defined?(JrJackson)
2
- require_relative "../adapter"
3
-
4
- module MultiJson
5
- module Adapters
6
- # Use the jrjackson.rb library to dump/load.
7
- class JrJackson < Adapter
8
- ParseError = ::JrJackson::ParseError
9
-
10
- # Parse a JSON string into a Ruby object
11
- #
12
- # @api private
13
- # @param string [String] JSON string to parse
14
- # @param options [Hash] parsing options
15
- # @return [Object] parsed Ruby object
16
- #
17
- # @example Parse JSON string
18
- # adapter.load('{"key":"value"}') #=> {"key" => "value"}
19
- def load(string, options = {})
20
- ::JrJackson::Json.load(string, options)
21
- end
22
-
23
- if ::JrJackson::Json.method(:dump).arity == 1
24
- # Serialize a Ruby object to JSON
25
- #
26
- # @api private
27
- # @param object [Object] object to serialize
28
- # @param _ [Hash] serialization options (unused in this version)
29
- # @return [String] JSON string
30
- #
31
- # @example Serialize object to JSON
32
- # adapter.dump({key: "value"}) #=> '{"key":"value"}'
33
- def dump(object, _)
34
- ::JrJackson::Json.dump(object)
35
- end
36
- else
37
- # Serialize a Ruby object to JSON
38
- #
39
- # @api private
40
- # @param object [Object] object to serialize
41
- # @param options [Hash] serialization options
42
- # @return [String] JSON string
43
- #
44
- # @example Serialize object to JSON
45
- # adapter.dump({key: "value"}) #=> '{"key":"value"}'
46
- def dump(object, options = {})
47
- ::JrJackson::Json.dump(object, options)
48
- end
49
- end
50
- end
51
- end
52
- end
@@ -1,43 +0,0 @@
1
- require_relative "../adapter"
2
- require_relative "../convertible_hash_keys"
3
- require_relative "../vendor/okjson"
4
-
5
- module MultiJson
6
- module Adapters
7
- # Use the vendored OkJson library to dump/load.
8
- class OkJson < Adapter
9
- include ConvertibleHashKeys
10
-
11
- ParseError = ::MultiJson::OkJson::Error
12
-
13
- # Parse a JSON string into a Ruby object
14
- #
15
- # @api private
16
- # @param string [String] JSON string to parse
17
- # @param options [Hash] parsing options
18
- # @return [Object] parsed Ruby object
19
- #
20
- # @example Parse JSON string
21
- # adapter.load('{"key":"value"}') #=> {"key" => "value"}
22
- def load(string, options = {})
23
- result = ::MultiJson::OkJson.decode("[#{string}]").first
24
- options[:symbolize_keys] ? symbolize_keys(result) : result
25
- rescue ArgumentError # invalid byte sequence in UTF-8
26
- raise ParseError
27
- end
28
-
29
- # Serialize a Ruby object to JSON
30
- #
31
- # @api private
32
- # @param object [Object] object to serialize
33
- # @param _ [Hash] serialization options (unused)
34
- # @return [String] JSON string
35
- #
36
- # @example Serialize object to JSON
37
- # adapter.dump({key: "value"}) #=> '{"key":"value"}'
38
- def dump(object, _ = {})
39
- ::MultiJson::OkJson.valenc(stringify_keys(object))
40
- end
41
- end
42
- end
43
- end
@@ -1,66 +0,0 @@
1
- module MultiJson
2
- # Mixin for converting hash keys between symbols and strings
3
- #
4
- # @api private
5
- module ConvertibleHashKeys
6
- SIMPLE_OBJECT_CLASSES = [String, Numeric, TrueClass, FalseClass, NilClass].freeze
7
- private_constant :SIMPLE_OBJECT_CLASSES
8
-
9
- private
10
-
11
- # Converts hash keys to symbols recursively
12
- #
13
- # @api private
14
- # @param value [Object] value to convert
15
- # @return [Object] value with symbolized keys
16
- def symbolize_keys(value)
17
- convert_hash_keys(value) { |key| key.respond_to?(:to_sym) ? key.to_sym : key }
18
- end
19
-
20
- # Converts hash keys to strings recursively
21
- #
22
- # @api private
23
- # @param value [Object] value to convert
24
- # @return [Object] value with stringified keys
25
- def stringify_keys(value)
26
- convert_hash_keys(value) { |key| key.respond_to?(:to_s) ? key.to_s : key }
27
- end
28
-
29
- # Recursively converts hash keys using the given block
30
- #
31
- # @api private
32
- # @param value [Object] value to convert
33
- # @yield [key] block to transform each key
34
- # @return [Object] converted value
35
- def convert_hash_keys(value, &key_modifier)
36
- case value
37
- when Hash
38
- value.to_h { |k, v| [key_modifier.call(k), convert_hash_keys(v, &key_modifier)] }
39
- when Array
40
- value.map { |v| convert_hash_keys(v, &key_modifier) }
41
- else
42
- convert_simple_object(value)
43
- end
44
- end
45
-
46
- # Converts non-hash objects to a JSON-safe format
47
- #
48
- # @api private
49
- # @param obj [Object] object to convert
50
- # @return [Object] converted object
51
- def convert_simple_object(obj)
52
- return obj if simple_object?(obj) || obj.respond_to?(:to_json)
53
-
54
- obj.respond_to?(:to_s) ? obj.to_s : obj
55
- end
56
-
57
- # Checks if an object is a simple JSON-safe type
58
- #
59
- # @api private
60
- # @param obj [Object] object to check
61
- # @return [Boolean] true if object is a simple type
62
- def simple_object?(obj)
63
- SIMPLE_OBJECT_CLASSES.any? { |klass| obj.is_a?(klass) }
64
- end
65
- end
66
- end