class Yajl::Projector - yajl-ruby-1.4.1 Documentation

# class Yajl::Projector

### Public Class Methods

new(stream, read_bufsize=4096) click to toggle source

    # File lib/yajl.rb, line 27
    def initialize(stream, read_bufsize=4096)
      @stream = stream
      @buffer_size = read_bufsize
    end

### Public Instance Methods

project(p1) click to toggle source

    static VALUE rb_yajl_projector_project(VALUE self, VALUE schema) {
        VALUE stream = rb_iv_get(self, "@stream");

        long buffer_size = FIX2LONG(rb_iv_get(self, "@buffer_size"));
        VALUE buffer = rb_str_new(0, buffer_size);

        struct yajl_event_stream_s parser = {
            .funcs = &rb_alloc_funcs,

            .stream = stream,

            .buffer = buffer,
            .offset = (unsigned int)buffer_size,

            .lexer = yajl_lex_alloc(&rb_alloc_funcs, 0, 1),
        };

        yajl_event_t event = yajl_event_stream_next(&parser, 1);

        RB_GC_GUARD(stream);
        RB_GC_GUARD(buffer);

        VALUE result;
        int state = 0;

        if (event.token == yajl_tok_left_brace || event.token == yajl_tok_left_bracket) {
            VALUE args[3];
            args[0] = (VALUE)&parser;
            args[1] = schema;
            args[2] = (VALUE)&event;
            result = rb_protect(rb_protected_yajl_projector_filter,
                                (VALUE)args,
                                &state);
        } else {
            yajl_lex_free(parser.lexer);
            rb_raise(cParseError, "expected left bracket or brace, actually read %s", yajl_tok_name(event.token));
        }

        yajl_lex_free(parser.lexer);
        if (state) rb_jump_tag(state);

        return result;
    }