From 673fa239c4f3728984dade81b671f58d1ac8b46a Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Tue, 17 Mar 2015 10:22:41 -0700 Subject: Removing JSON gem compatibility layer This causes issues because JSON and ffi-yajl get into fights over who monkeypatched the classes last and this causes random issues with whitespace disagreement between the JSON gem and the underlying libyajl C library. For deterministic output its better not to have fighting monkeypatches, it generates considerable chaos. If anyone wants to extract the json_gem.rb file into a gem of its own and support these monkeypatches going forwards that would be fine (there is, in fact, literally nothing that could be done to stop you), but we do not want to support these. --- lib/ffi_yajl/json_gem.rb | 140 ----------------------------------------------- 1 file changed, 140 deletions(-) delete mode 100644 lib/ffi_yajl/json_gem.rb (limited to 'lib') diff --git a/lib/ffi_yajl/json_gem.rb b/lib/ffi_yajl/json_gem.rb deleted file mode 100644 index bb34d9d..0000000 --- a/lib/ffi_yajl/json_gem.rb +++ /dev/null @@ -1,140 +0,0 @@ - -# JSON compatibility layer, largely plagarized from yajl-ruby - -require 'ffi_yajl' unless defined?(FFI_Yajl::Parser) - -warn "ffi-yajl/json_gem is deprecated, these monkeypatches will be dropped shortly" - -module JSON - class JSONError < StandardError; end unless defined?(JSON::JSONError) - class GeneratorError < JSONError; end unless defined?(JSON::GeneratorError) - class ParserError < JSONError; end unless defined?(JSON::ParserError) - - def self.generate(obj, opts=nil) - opts ||= {} - options_map = {} - if opts.has_key?(:indent) - options_map[:pretty] = true - options_map[:indent] = opts[:indent] - end - FFI_Yajl::Encoder.encode(obj, options_map) - rescue FFI_Yajl::EncodeError => e - raise JSON::GeneratorError, e.message - end - - def self.pretty_generate(obj, opts=nil) - opts ||= {} - options_map = {} - options_map[:pretty] = true - options_map[:indent] = opts[:indent] if opts.has_key?(:indent) - FFI_Yajl::Encoder.encode(obj, options_map).chomp - rescue FFI_Yajl::EncodeError => e - raise JSON::GeneratorError, e.message - end - - def self.dump(obj, io=nil, *args) - FFI_Yajl::Encoder.encode(obj, io) - rescue FFI_Yajl::EncodeError => e - raise JSON::GeneratorError, e.message - end - - def self.default_options - @default_options ||= {:symbolize_keys => false} - end - - def self.parse(str, opts=JSON.default_options) - FFI_Yajl::Parser.parse(str, opts) - rescue FFI_Yajl::ParseError => e - raise JSON::ParserError, e.message - end - - def self.load(input, *args) - FFI_Yajl::Parser.parse(input, default_options) - rescue FFI_Yajl::ParseError => e - raise JSON::ParserError, e.message - end -end - -class Array - def to_json(*opts, &block) - FFI_Yajl::Encoder.encode(self, *opts) - end -end - -class Hash - def to_json(*opts, &block) - FFI_Yajl::Encoder.encode(self, *opts) - end -end - -class Fixnum - def to_json(*opts, &block) - FFI_Yajl::Encoder.encode(self, *opts) - end -end - -class Float - def to_json(*opts, &block) - FFI_Yajl::Encoder.encode(self, *opts) - end -end - -class String - def to_json(*opts, &block) - FFI_Yajl::Encoder.encode(self, *opts) - end -end - -class TrueClass - def to_json(*opts, &block) - FFI_Yajl::Encoder.encode(self, *opts) - end -end - -class FalseClass - def to_json(*opts, &block) - FFI_Yajl::Encoder.encode(self, *opts) - end -end - -class NilClass - def to_json(*opts, &block) - FFI_Yajl::Encoder.encode(self, *opts) - end -end - -class Date - def to_json(*opts, &block) - FFI_Yajl::Encoder.encode(self, *opts) - end -end - -class Time - def to_json(*opts, &block) - FFI_Yajl::Encoder.encode(self, *opts) - end -end - -class DateTime - def to_json(*opts, &block) - FFI_Yajl::Encoder.encode(self, *opts) - end -end - -module ::Kernel - def JSON(object, opts = {}) - if object.respond_to? :to_s - JSON.parse(object.to_s, JSON.default_options.merge(opts)) - else - JSON.generate(object,opts) - end - end -end - -class Object - unless defined?(ActiveSupport) - def to_json(*args, &block) - "\"#{to_s}\"" - end - end -end -- cgit v1.2.1