Sha256: d93dcec34ec8ce9007e07404496a0292a8830a6c6aa2c4b9dfd8c78bb87fc731

Contents?: true

Size: 1.85 KB

Versions: 32

Compression:

Stored size: 1.85 KB

Contents

module FFI_Yajl
  class EncodeError < StandardError; end
  class Encoder
    attr_accessor :opts

    def encode(obj)
      # initialization that we can do in pure ruby
      yajl_gen_opts = {}

      yajl_gen_opts[:yajl_gen_validate_utf8] = true
      yajl_gen_opts[:yajl_gen_beautify] = false
      yajl_gen_opts[:yajl_gen_indent_string] = " "

      if opts[:pretty]
        yajl_gen_opts[:yajl_gen_beautify] = true
        yajl_gen_opts[:yajl_gen_indent_string] = opts[:indent] ? opts[:indent] : "  "
      end

      # call either the ext or ffi hook
      str = do_yajl_encode(obj, yajl_gen_opts)
      str.force_encoding('UTF-8') if defined? Encoding
      str
    end

    def self.encode(obj, *args)
      new(*args).encode(obj)
    end

    def initialize(opts = {})
      @opts = opts
    end

    def self.raise_error_for_status(status)
      case status
      when 1 # yajl_gen_keys_must_be_strings
        raise FFI_Yajl::EncodeError, "YAJL internal error: attempted use of non-string object as key"
      when 2 # yajl_max_depth_exceeded
        raise FFI_Yajl::EncodeError, "Max nesting depth exceeded"
      when 3 # yajl_gen_in_error_state
        raise FFI_Yajl::EncodeError, "YAJL internal error: a generator function (yajl_gen_XXX) was called while in an error state"
      when 4 # yajl_gen_generation_complete
        raise FFI_Yajl::EncodeError, "YAJL internal error: attempted to encode to an already-complete document"
      when 5 # yajl_gen_invalid_number
        raise FFI_Yajl::EncodeError, "Invalid number: cannot encode Infinity, -Infinity, or NaN"
      when 6 # yajl_gen_no_buf
        raise FFI_Yajl::EncodeError, "YAJL internal error: yajl_gen_get_buf was called, but a print callback was specified, so no internal buffer is available"
      else
        raise FFI_Yajl::EncodeError, "Unknown YAJL Error, please report this as a bug"
      end
    end
  end
end

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
ffi-yajl-1.0.1-universal-java lib/ffi_yajl/encoder.rb
ffi-yajl-1.0.1 lib/ffi_yajl/encoder.rb
ffi-yajl-1.0.0-universal-java lib/ffi_yajl/encoder.rb
ffi-yajl-1.0.0 lib/ffi_yajl/encoder.rb
ffi-yajl-0.2.1-universal-java lib/ffi_yajl/encoder.rb
ffi-yajl-0.2.1 lib/ffi_yajl/encoder.rb
ffi-yajl-0.2.0 lib/ffi_yajl/encoder.rb
ffi-yajl-0.2.0-universal-java lib/ffi_yajl/encoder.rb
ffi-yajl-0.1.7 lib/ffi_yajl/encoder.rb
ffi-yajl-0.1.7-universal-java lib/ffi_yajl/encoder.rb
ffi-yajl-0.1.6 lib/ffi_yajl/encoder.rb
ffi-yajl-0.1.6-universal-java lib/ffi_yajl/encoder.rb
ffi-yajl-0.1.5-universal-java lib/ffi_yajl/encoder.rb
ffi-yajl-0.1.5 lib/ffi_yajl/encoder.rb
ffi-yajl-0.1.4-universal-java lib/ffi_yajl/encoder.rb
ffi-yajl-0.1.4 lib/ffi_yajl/encoder.rb
ffi-yajl-0.1.3 lib/ffi_yajl/encoder.rb
ffi-yajl-0.1.3-universal-java lib/ffi_yajl/encoder.rb
ffi-yajl-0.1.2 lib/ffi_yajl/encoder.rb
ffi-yajl-0.1.2-universal-java lib/ffi_yajl/encoder.rb