Class: Wankel::SaxEncoder
- Inherits:
-
Object
- Object
- Wankel::SaxEncoder
- Defined in:
- lib/wankel.rb,
ext/wankel/wankel_sax_encoder.c
Instance Method Summary collapse
- #array_close ⇒ Object
- #array_open ⇒ Object
- #boolean(b) ⇒ Object
- #flush ⇒ Object
-
#initialize(*args) ⇒ Object
constructor
call-seq: new().
- #map_close ⇒ Object
- #map_open ⇒ Object
- #null ⇒ Object
- #number(number) ⇒ Object
- #output=(io) ⇒ Object
- #string(string) ⇒ Object
- #value(val) ⇒ Object
Constructor Details
#initialize(*args) ⇒ Object
call-seq: new()
:beautify generate indented (beautiful) output. Default ‘false`.
:indent_string Set an indent string which is used when yajl_gen_beautify
is enabled. Maybe something like \\t or some number of
spaces. The default is four spaces ' '.
:validate_utf8 Normally the generator does not validate that strings you
pass to it are valid UTF8. Enabling this option will cause
it to do so.
:escape_solidus the forward solidus (slash or ‘/’ in human) is not required
to be escaped in json text. By default, YAJL will not escape
it in the iterest of saving bytes. Setting this flag will
cause YAJL to always escape '/' in generated JSON strings.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'ext/wankel/wankel_sax_encoder.c', line 50
static VALUE wankelSaxEncoder_initialize(int argc, VALUE * argv, VALUE self) {
VALUE defaults = rb_const_get(c_wankel, intern_DEFAULTS);
VALUE io, options;
wankel_encoder * p;
yajl_alloc_funcs alloc_funcs;
rb_scan_args(argc, argv, "11", &io, &options);
if(options == Qnil) {
rb_iv_set(self, "@options", rb_funcall(defaults, intern_clone, 0) );
} else {
Check_Type(options, T_HASH);
rb_iv_set(self, "@options", rb_funcall(defaults, intern_merge, 1, options) );
}
options = rb_iv_get(self, "@options");
if (!rb_respond_to(io, intern_io_write)) {
rb_raise(e_encodeError, "output must be a an IO");
}
Data_Get_Struct(self, wankel_encoder, p);
p->output = io;
alloc_funcs.malloc = yajl_helper_malloc;
alloc_funcs.realloc = yajl_helper_realloc;
alloc_funcs.free = yajl_helper_free;
p->g = yajl_gen_alloc(&alloc_funcs);
yajl_gen_configure(p->g, options);
p->write_buffer_size = FIX2INT(rb_hash_aref(options, ID2SYM(rb_intern("write_buffer_size"))));
return self;
}
|
Instance Method Details
#array_close ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'ext/wankel/wankel_sax_encoder.c', line 206
static VALUE wankelSaxEncoder_array_close(VALUE self) {
wankel_encoder * p;
yajl_gen_status status;
Data_Get_Struct(self, wankel_encoder, p);
status = yajl_gen_array_close(p->g);
yajl_helper_check_gen_status(status);
wankelSaxEncoder_write_buffer(p);
return Qnil;
}
|
#array_open ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'ext/wankel/wankel_sax_encoder.c', line 193
static VALUE wankelSaxEncoder_array_open(VALUE self) {
wankel_encoder * p;
yajl_gen_status status;
Data_Get_Struct(self, wankel_encoder, p);
status = yajl_gen_array_open(p->g);
yajl_helper_check_gen_status(status);
wankelSaxEncoder_write_buffer(p);
return Qnil;
}
|
#boolean(b) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'ext/wankel/wankel_sax_encoder.c', line 154
static VALUE wankelSaxEncoder_boolean(VALUE self, VALUE b) {
wankel_encoder * p;
yajl_gen_status status;
Data_Get_Struct(self, wankel_encoder, p);
status = yajl_gen_bool(p->g, RTEST(b));
yajl_helper_check_gen_status(status);
wankelSaxEncoder_write_buffer(p);
return Qnil;
}
|
#flush ⇒ Object
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'ext/wankel/wankel_sax_encoder.c', line 219
static VALUE wankelSaxEncoder_flush(VALUE self) {
size_t len;
VALUE rbBuffer;
wankel_encoder * p;
yajl_gen_status status;
const unsigned char * buffer;
Data_Get_Struct(self, wankel_encoder, p);
status = yajl_gen_get_buf(p->g, &buffer, &len);
yajl_helper_check_gen_status(status);
rbBuffer = rb_str_new((const char *)buffer, len);
rb_enc_associate(rbBuffer, rb_utf8_encoding());
rb_io_write(p->output, rbBuffer);
yajl_gen_clear(p->g);
return Qnil;
}
|
#map_close ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'ext/wankel/wankel_sax_encoder.c', line 180
static VALUE wankelSaxEncoder_map_close(VALUE self) {
wankel_encoder * p;
yajl_gen_status status;
Data_Get_Struct(self, wankel_encoder, p);
status = yajl_gen_map_close(p->g);
yajl_helper_check_gen_status(status);
wankelSaxEncoder_write_buffer(p);
return Qnil;
}
|
#map_open ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'ext/wankel/wankel_sax_encoder.c', line 167
static VALUE wankelSaxEncoder_map_open(VALUE self) {
wankel_encoder * p;
yajl_gen_status status;
Data_Get_Struct(self, wankel_encoder, p);
status = yajl_gen_map_open(p->g);
yajl_helper_check_gen_status(status);
wankelSaxEncoder_write_buffer(p);
return Qnil;
}
|
#null ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'ext/wankel/wankel_sax_encoder.c', line 141
static VALUE wankelSaxEncoder_null(VALUE self) {
wankel_encoder * p;
yajl_gen_status status;
Data_Get_Struct(self, wankel_encoder, p);
status = yajl_gen_null(p->g);
yajl_helper_check_gen_status(status);
wankelSaxEncoder_write_buffer(p);
return Qnil;
}
|
#number(number) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'ext/wankel/wankel_sax_encoder.c', line 98
static VALUE wankelSaxEncoder_number(VALUE self, VALUE number) {
size_t len;
const char * cptr;
wankel_encoder * p;
yajl_gen_status status;
VALUE str = rb_funcall(number, intern_to_s, 0);
Data_Get_Struct(self, wankel_encoder, p);
cptr = RSTRING_PTR(str);
len = RSTRING_LEN(str);
if (memcmp(cptr, "NaN", 3) == 0 || memcmp(cptr, "Infinity", 8) == 0 || memcmp(cptr, "-Infinity", 9) == 0) {
rb_raise(e_encodeError, "'%s' is an invalid number", cptr);
}
status = yajl_gen_number(p->g, cptr, len);
yajl_helper_check_gen_status(status);
wankelSaxEncoder_write_buffer(p);
return Qnil;
}
|
#output=(io) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'ext/wankel/wankel_sax_encoder.c', line 83
static VALUE wankelSaxEncoder_change_io(VALUE self, VALUE io) {
wankel_encoder * p;
if (!rb_respond_to(io, intern_io_write)) {
rb_raise(e_encodeError, "output must be a an IO");
}
Data_Get_Struct(self, wankel_encoder, p);
wankelSaxEncoder_flush(self);
p->output = io;
return Qnil;
}
|
#string(string) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'ext/wankel/wankel_sax_encoder.c', line 121
static VALUE wankelSaxEncoder_string(VALUE self, VALUE string) {
size_t len;
const char * cptr;
wankel_encoder * p;
yajl_gen_status status;
Check_Type(string, T_STRING);
Data_Get_Struct(self, wankel_encoder, p);
cptr = RSTRING_PTR(string);
len = RSTRING_LEN(string);
status = yajl_gen_string(p->g, (const unsigned char *)cptr, len);
yajl_helper_check_gen_status(status);
wankelSaxEncoder_write_buffer(p);
return Qnil;
}
|
#value(val) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/wankel.rb', line 23 def value(val) case val when NilClass null when TrueClass, FalseClass boolean(val) when Numeric number(val) when String string(val) when Array array_open val.each {|v| value(v) } array_close when Hash map_open val.each {|k, v| string(k.to_s); value(v) } map_close end end |