Sha256: 07edb9ebb147d92f4cf916c9b31c4ef238e10707416fdb0288a2d31480031fd0

Contents?: true

Size: 1.34 KB

Versions: 7

Compression:

Stored size: 1.34 KB

Contents

require 'yajl' unless defined?(Yajl::Parser)

# NOTE: this is probably temporary until I can split out the JSON compat C code into it's own
# extension that can be included when this file is.
Yajl::Encoder.enable_json_gem_compatability

# Our fallback to_json definition
unless defined?(ActiveSupport)
  class Object
    def to_json(*args, &block)
      "\"#{to_s}\""
    end
  end
end

module JSON
  class JSONError < StandardError; end unless defined?(JSON::JSONError)
  class GeneratorError < JSONError; end unless defined?(JSON::GeneratorError)

  def self.generate(obj, opts={})
    begin
      options_map = {}
      if opts.has_key?(:indent)
        options_map[:pretty] = true
        options_map[:indent] = opts[:indent]
      end
      Yajl::Encoder.encode(obj, options_map)
    rescue Yajl::EncodeError => e
      raise JSON::GeneratorError, e.message
    end
  end

  def self.pretty_generate(obj, opts={})
    begin
      options_map = {}
      options_map[:pretty] = true
      options_map[:indent] = opts[:indent] if opts.has_key?(:indent)
      Yajl::Encoder.encode(obj, options_map)
    rescue Yajl::EncodeError => e
      raise JSON::GeneratorError, e.message
    end
  end

  def self.dump(obj, io=nil, *args)
    begin
      Yajl::Encoder.encode(obj, io)
    rescue Yajl::EncodeError => e
      raise JSON::GeneratorError, e.message
    end
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
yajl-ruby-maglev--1.1.0 lib/yajl/json_gem/encoding.rb
yajl-ruby-1.1.0-x86-mswin32-60 lib/yajl/json_gem/encoding.rb
yajl-ruby-1.1.0-x86-mingw32 lib/yajl/json_gem/encoding.rb
yajl-ruby-1.1.0 lib/yajl/json_gem/encoding.rb
yajl-ruby-1.0.0-x86-mswin32-60 lib/yajl/json_gem/encoding.rb
yajl-ruby-1.0.0-x86-mingw32 lib/yajl/json_gem/encoding.rb
yajl-ruby-1.0.0 lib/yajl/json_gem/encoding.rb