Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for bundled OkJson when no other libraries are available
- Loading branch information
1 parent
c2f4140
commit e89093c
Showing
5 changed files
with
625 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
require "multi_json/vendor/okjson" unless defined?(::OkJson) | ||
|
||
module MultiJson | ||
module Engines | ||
class Okjson | ||
def self.decode(string, options = {}) #:nodoc: | ||
result = OkJson.decode(string) | ||
options[:symbolize_keys] ? symbolize_keys(result) : result | ||
end | ||
|
||
def self.encode(object) #:nodoc: | ||
OkJson.encode(stringify_keys(object)) | ||
end | ||
|
||
def self.symbolize_keys(object) #:nodoc: | ||
return object unless object.is_a?(Hash) | ||
object.inject({}) do |result, (key, value)| | ||
new_key = key.is_a?(String) ? key.to_sym : key | ||
new_value = value.is_a?(Hash) ? symbolize_keys(value) : value | ||
result.merge! new_key => new_value | ||
end | ||
end | ||
|
||
def self.stringify_keys(object) #:nodoc: | ||
return object unless object.is_a?(Hash) | ||
object.inject({}) do |result, (key, value)| | ||
new_key = key.is_a?(Symbol) ? key.to_s : key | ||
new_value = value.is_a?(Hash) ? stringify_keys(value) : value | ||
result.merge! new_key => new_value | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.