Skip to content

Commit

Permalink
Merge pull request #74 from dmorehouse/master
Browse files Browse the repository at this point in the history
Add Ruby 3.1 support that is backwards compatible to Ruby 1.9.3
  • Loading branch information
kiskoza authored Jan 26, 2024
2 parents 5944de6 + d44085d commit a5cc81c
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions lib/crack/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,32 @@
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

require 'strscan'
require 'psych'

module Crack
class JSON
def self.parser_exceptions
@parser_exceptions ||= [ArgumentError, Psych::SyntaxError]
end

def self.parse(json)
yaml = unescape(convert_json_to_yaml(json))
YAML.safe_load(yaml, [Regexp, Date, Time])
rescue *parser_exceptions
raise ParseError, "Invalid JSON string"
rescue Psych::DisallowedClass
yaml
if Gem::Version.new(Psych::VERSION) >= Gem::Version.new('3.1.0')
def self.parse(json)
yaml = unescape(convert_json_to_yaml(json))
YAML.safe_load(yaml, permitted_classes: [Regexp, Date, Time])
rescue *parser_exceptions
raise ParseError, "Invalid JSON string"
rescue Psych::DisallowedClass
yaml
end
else # Ruby < 2.6
def self.parse(json)
yaml = unescape(convert_json_to_yaml(json))
YAML.safe_load(yaml, [Regexp, Date, Time])
rescue *parser_exceptions
raise ParseError, "Invalid JSON string"
rescue Psych::DisallowedClass
yaml
end
end

protected
Expand Down

0 comments on commit a5cc81c

Please sign in to comment.