Sha256: 3126b34e927d06f4b8e7a8f3e0b807968227c4d8759278b3e38066124272f961

Contents?: true

Size: 1.38 KB

Versions: 7

Compression:

Stored size: 1.38 KB

Contents

module RestCore; end
module RestCore::Json
  module MultiJson
    def self.extended mod
      require 'rest-core/patch/multi_json'
      mod.const_set(:ParseError, ::MultiJson::DecodeError)
    end
    def encode hash
      ::MultiJson.dump(hash)
    end
    def decode json
      ::MultiJson.load(json)
    end
  end

  module YajlRuby
    def self.extended mod
      mod.const_set(:ParseError, Yajl::ParseError)
    end
    def encode hash
      Yajl::Encoder.encode(hash)
    end
    def decode json
      Yajl::Parser.parse(json)
    end
  end

  module Json
    def self.extended mod
      mod.const_set(:ParseError, JSON::ParserError)
    end
    def encode hash
      JSON.dump(hash)
    end
    def decode json
      JSON.parse(json, :quirks_mode => true)
    end
  end

  def self.select_json! mod, picked=false
    if    Object.const_defined?(:MultiJson)
      mod.send(:extend, MultiJson)
    elsif Object.const_defined?(:Yajl)
      mod.send(:extend, YajlRuby)
    elsif Object.const_defined?(:JSON)
      mod.send(:extend, Json)
    elsif picked
      raise LoadError.new(
        'No JSON library found. Tried: multi_json, yajl-ruby, json.')
    else
      # pick a json gem if available
      %w[multi_json yajl json].each{ |json|
        begin
          require json
          break
        rescue LoadError
        end
      }
      select_json!(mod, true)
    end
  end

  select_json!(self)
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rest-core-3.3.3 lib/rest-core/util/json.rb
rest-core-3.3.2 lib/rest-core/util/json.rb
rest-core-3.3.1 lib/rest-core/util/json.rb
rest-core-3.3.0 lib/rest-core/util/json.rb
rest-core-3.2.0 lib/rest-core/util/json.rb
rest-core-3.1.1 lib/rest-core/util/json.rb
rest-core-3.1.0 lib/rest-core/util/json.rb