Sha256: f0bbe958d0216bd160e5f313972835fba0c711784c368a326c350298cc810c6a

Contents?: true

Size: 1.55 KB

Versions: 23

Compression:

Stored size: 1.55 KB

Contents

# TODO Document.
#
# What does this abstraction provide?
# Why is it here?
# 
module BEL::JSON

  # TODO Document.
  #
  # Reads JSON data. Leverages JSON adapters which implement the same signature.
  def self.read(data, options = {})
    @adapter ||= self.adapter
    instance = @adapter.new

    if block_given?
      instance.read(data, options) { |obj|
        yield obj
      }
    else
      instance.read(data, options)
    end
  end

  # TODO Document.
  #
  # Writes JSON data. Leverages JSON adapters which implement the same signature.
  def self.write(data, output_io, options = {})
    @adapter ||= self.adapter
    instance = @adapter.new
    instance.write(data, output_io, options)
  end

  # Load the most suitable JSON implementation available within ruby.
  # The load order attempted is:
  # - oj              (provides stream parsing utilizing event callbacks)
  # - multi_json      (simple buffering abstraction over multiple ruby libraries)
  # - json            (stock ruby implementation)
  def self.adapter
    implementations = [
      'json/adapter/oj',
      'json/adapter/multi_json',
      'json/adapter/ruby_json'
    ]

    load_success = implementations.any? { |impl|
      begin
        require_relative impl
        true
      rescue LoadError
        # Could not load +impl_module+; try the next one
        false
      end
    }

    if load_success
      BEL::JSON::Implementation
    else
      mod_s = impl_modules.join(', ')
      msg   = "Could not load any JSON implementation (tried: #{mod_s})."
      raise LoadError.new(msg)
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
bel-1.1.2 lib/bel/json.rb
bel-1.1.1 lib/bel/json.rb
bel-1.1.0 lib/bel/json.rb
bel-1.0.1 lib/bel/json.rb
bel-1.0.0 lib/bel/json.rb
bel-0.7.0 lib/bel/json.rb
bel-0.6.0 lib/bel/json.rb
bel-0.5.0 lib/bel/json.rb
bel-0.4.2 lib/bel/json.rb
bel-0.4.1 lib/bel/json.rb
bel-0.4.0 lib/bel/json.rb
bel-0.4.0.beta.13 lib/bel/json.rb
bel-0.4.0.beta.12 lib/bel/json.rb
bel-0.4.0.beta.11 lib/bel/json.rb
bel-0.4.0.beta.10 lib/bel/json.rb
bel-0.4.0.beta.9 lib/bel/json.rb
bel-0.4.0.beta.8 lib/bel/json.rb
bel-0.4.0.beta.7 lib/bel/json.rb
bel-0.4.0.beta.5 lib/bel/json.rb
bel-0.4.0.beta.4 lib/bel/json.rb