Class: Yajl::Encoder

Inherits:
Object
  • Object
show all
Defined in:
ext/yajl_ext.c,
lib/yajl.rb,
ext/yajl_ext.c

Overview

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 #write(str) The JSON stream created is written to the IO in chunks, as it’s being created.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object

call-seq: initialize([:pretty => false[, :indent => ‘ ’]])

:pretty will enable/disable beautifying or “pretty priting” the output string.

:indent is the character(s) used to indent the output string.



475
476
477
# File 'ext/yajl_ext.c', line 475

static VALUE rb_yajl_encoder_init(int argc, VALUE * argv, VALUE self) {
    return self;
}

Class Method Details

.enable_json_gem_compatabilityObject

call-seq: enable_json_gem_compatability

Enables the JSON gem compatibility API



726
727
728
729
730
731
732
733
734
735
736
737
# File 'ext/yajl_ext.c', line 726

static VALUE rb_yajl_encoder_enable_json_gem_ext(VALUE klass) {
    rb_define_method(rb_cObject, "to_json", rb_yajl_json_ext_object_to_json, -1);
    rb_define_method(rb_cHash, "to_json", rb_yajl_json_ext_hash_to_json, -1);
    rb_define_method(rb_cArray, "to_json", rb_yajl_json_ext_array_to_json, -1);
    rb_define_method(rb_cFixnum, "to_json", rb_yajl_json_ext_fixnum_to_json, -1);
    rb_define_method(rb_cFloat, "to_json", rb_yajl_json_ext_float_to_json, -1);
    rb_define_method(rb_cString, "to_json", rb_yajl_json_ext_string_to_json, -1);
    rb_define_method(rb_cTrueClass, "to_json", rb_yajl_json_ext_true_to_json, -1);
    rb_define_method(rb_cFalseClass, "to_json", rb_yajl_json_ext_false_to_json, -1);
    rb_define_method(rb_cNilClass, "to_json", rb_yajl_json_ext_nil_to_json, -1);
    return Qnil;
}

.encode(obj, *args, &block) ⇒ Object

A helper method for encode-and-forget use-cases

Examples:

Yajl::Encoder.encode(obj[, io, :pretty => true, :indent => "\t"])

output = Yajl::Encoder.encode(obj[, :pretty => true, :indent => "\t"])

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



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/yajl.rb', line 51

def self.encode(obj, *args, &block)
  # TODO: this code smells, any ideas?
  options = {}
  io = nil
  args.each do |arg|
    if arg.is_a?(Hash)
      options = arg
    elsif arg.respond_to?(:read)
      io = arg
    end
  end if args.any?
  new(options).encode(obj, io, &block)
end

.new(*args) ⇒ Object

call-seq: new([:pretty => false[, :indent => ‘ ’]])

:pretty will enable/disable beautifying or “pretty priting” the output string.

:indent is the character(s) used to indent the output string.



438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# File 'ext/yajl_ext.c', line 438

static VALUE rb_yajl_encoder_new(int argc, VALUE * argv, VALUE klass) {
    yajl_gen_config cfg;
    yajl_gen encoder;
    VALUE opts, obj, indent;
    const char * indentString = "  ";
    int beautify = 0;
    
    // Scan off config vars
    if (rb_scan_args(argc, argv, "01", &opts) == 1) {
        Check_Type(opts, T_HASH);
        
        if (rb_hash_aref(opts, ID2SYM(sym_pretty)) == Qtrue) {
            beautify = 1;
            indent = rb_hash_aref(opts, ID2SYM(sym_indent));
            if (indent != Qnil) {
                Check_Type(indent, T_STRING);
                indentString = RSTRING_PTR(indent);
            }
        }
    }
    cfg = (yajl_gen_config){beautify, indentString};
    
    encoder = yajl_gen_alloc(&cfg, NULL);
    obj = Data_Wrap_Struct(klass, 0, yajl_gen_free, encoder);
    rb_obj_call_init(obj, 0, 0);
    return obj;
}

Instance Method Details

#encode(*args) ⇒ Object

call-seq: encode(obj[, io[, &block]])

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.



495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
# File 'ext/yajl_ext.c', line 495

static VALUE rb_yajl_encoder_encode(int argc, VALUE * argv, VALUE self) {
    yajl_gen encoder;
    const unsigned char * buffer;
    unsigned int len;
    VALUE obj, io, blk, outBuff;
    
    GetEncoder(self, encoder);
    
    rb_scan_args(argc, argv, "11&", &obj, &io, &blk);
    
    // begin encode process
    yajl_encode_part(encoder, obj, io);

    // just make sure we output the remaining buffer
    yajl_gen_get_buf(encoder, &buffer, &len);
    outBuff = rb_str_new((const char *)buffer, len);
    yajl_gen_clear(encoder);
    if (io != Qnil) {
        rb_io_write(io, outBuff);
        return Qnil;
    } else if (blk != Qnil) {
        rb_funcall(blk, intern_call, 1, outBuff);
        return Qnil;
    } else {
        return outBuff;
    }
    return Qnil;
}