This class contains methods for encoding a Ruby object into JSON, streaming it’s output into an IO object. The IO object need only respond to # The JSON stream created is written to the IO in chunks, as it’s being created.
Enables the JSON gem compatibility API
static VALUE rb_yajl_encoder_enable_json_gem_ext(VALUE klass)
A helper method for encode-and-forget use-cases
Examples:
Yajl::Encoder.encode(obj[, io, :pretty => true, :indent => "\t", &block]) output = Yajl::Encoder.encode(obj[, :pretty => true, :indent => "\t", &block])
obj is a ruby object to encode to JSON format
io is the optional IO stream to encode the ruby object to. If io isn’t passed, the resulting JSON string is returned. If io is passed, nil is returned.
The options hash allows you to set two encoding options - :pretty and :indent
:pretty accepts a boolean and will enable/disable “pretty printing” the resulting output
:indent accepts a string and will be used as the indent character(s) during the pretty print process
If a block is passed, it will be used as (and work the same as) the on_progress callback
# File lib/yajl.rb, line 63 63: def self.encode(obj, *args, &block) 64: # TODO: this code smells, any ideas? 65: args.flatten! 66: options = {} 67: io = nil 68: args.each do |arg| 69: if arg.is_a?(Hash) 70: options = arg 71: elsif arg.respond_to?(:read) 72: io = arg 73: end 74: end if args.any? 75: new(options).encode(obj, io, &block) 76: end
:symbolize_keys will turn hash keys into Ruby symbols, defaults to false.
:allow_comments will turn on/off the check for comments inside the JSON stream, defaults to true.
:check_utf8 will validate UTF8 characters found in the JSON stream, defaults to true.
static VALUE rb_yajl_encoder_init(int argc, VALUE * argv, VALUE self)
:symbolize_keys will turn hash keys into Ruby symbols, defaults to false.
:allow_comments will turn on/off the check for comments inside the JSON stream, defaults to true.
:check_utf8 will validate UTF8 characters found in the JSON stream, defaults to true.
static VALUE rb_yajl_encoder_new(int argc, VALUE * argv, VALUE klass)
obj is the Ruby object to encode to JSON
io is an optional IO used to stream the encoded JSON string to. If io isn’t specified, this method will return the resulting JSON string. If io is specified, this method returns nil
If an optional block is passed, it’s called when encoding is complete and passed the resulting JSON string
It should be noted that you can reuse an instance of this class to continue encoding multiple JSON to the same stream. Just continue calling this method, passing it the same IO object with new/different ruby objects to encode. This is how streaming is accomplished.
static VALUE rb_yajl_encoder_encode(int argc, VALUE * argv, VALUE self)
This callback setter allows you to pass a Proc/lambda or any other object that responds to #.
It will pass the caller a chunk of the encode buffer after it’s reached it’s internal max buffer size (defaults to 8kb). For example, encoding a large object that would normally result in 24288 bytes of data will result in 3 calls to this callback (assuming the 8kb default encode buffer).
static VALUE rb_yajl_encoder_set_progress_cb(VALUE self, VALUE callback)
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.