Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove safe_yaml. Psych.safe_load is fine for modern rubies #59

Merged
merged 1 commit into from Dec 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 0 additions & 2 deletions crack.gemspec
Expand Up @@ -15,6 +15,4 @@ Gem::Specification.new do |gem|
gem.require_paths = ["lib"]
gem.version = Crack::VERSION
gem.license = "MIT"

gem.add_dependency "safe_yaml", "~> 1.0.0"
end
23 changes: 5 additions & 18 deletions lib/crack/json.rb
Expand Up @@ -3,34 +3,21 @@
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
# 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 'safe_yaml/load'
require 'strscan'

module Crack
class JSON
def self.parser_exceptions
@parser_exceptions ||= begin
exceptions = [ArgumentError]

if const_defined?(:Psych)
if Psych.const_defined?(:SyntaxError)
exceptions << Psych::SyntaxError
end
end

exceptions
end
@parser_exceptions ||= [ArgumentError, Psych::SyntaxError]
end

def self.parse(json)
args = [unescape(convert_json_to_yaml(json))]
args << nil if SafeYAML::MULTI_ARGUMENT_YAML_LOAD
args << { :whitelisted_tags => ['!ruby/regexp'] }

SafeYAML.load(*args)

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

protected
Expand Down