Class: FFI_Yajl::Encoder
- Includes:
- FFI_Yajl::Ext::Encoder, FFI::Encoder
- Defined in:
- lib/ffi_yajl/ext.rb,
lib/ffi_yajl/ffi.rb,
lib/ffi_yajl/encoder.rb,
ext/ffi_yajl/ext/encoder/encoder.c
Instance Attribute Summary collapse
-
#opts ⇒ Object
Returns the value of attribute opts.
Class Method Summary collapse
Instance Method Summary collapse
- #encode(obj) ⇒ Object
-
#initialize(opts = {}) ⇒ Encoder
constructor
A new instance of Encoder.
Methods included from FFI::Encoder
Methods included from FFI_Yajl::Ext::Encoder
Constructor Details
#initialize(opts = {}) ⇒ Encoder
Returns a new instance of Encoder.
30 31 32 33 |
# File 'lib/ffi_yajl/encoder.rb', line 30 def initialize(opts = {}) @opts = opts @opts ||= {} end |
Instance Attribute Details
#opts ⇒ Object
Returns the value of attribute opts.
5 6 7 |
# File 'lib/ffi_yajl/encoder.rb', line 5 def opts @opts end |
Class Method Details
.encode(obj, *args) ⇒ Object
26 27 28 |
# File 'lib/ffi_yajl/encoder.rb', line 26 def self.encode(obj, *args) new(*args).encode(obj) end |
.raise_error_for_status(status) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/ffi_yajl/encoder.rb', line 35 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" when 7 # yajl_gen_invalid_string raise FFI_Yajl::EncodeError, "Invalid UTF-8 string: cannot encode to UTF-8" else raise FFI_Yajl::EncodeError, "Unknown YAJL Error (#{status}), please report this as a bug" end end |
Instance Method Details
#encode(obj) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/ffi_yajl/encoder.rb', line 7 def encode(obj) # initialization that we can do in pure ruby yajl_gen_opts = {} yajl_gen_opts[:yajl_gen_validate_utf8] = @opts[:validate_utf8] == false ? false : 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, opts) str.force_encoding('UTF-8') if defined? Encoding str end |