summaryrefslogtreecommitdiff
path: root/ext/ffi_yajl/ext/encoder/encoder.c
blob: fdc6f56fb648ab3a8a6f70707b7da4848effc765 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#include <ruby.h>
#include <yajl/yajl_gen.h>

static VALUE mFFI_Yajl, mExt, mEncoder, mEncoder2, cEncodeError;
static VALUE cDate, cTime, cDateTime, cStringIO;
static VALUE cYajl_Gen;

/* FIXME: the json gem does a whole bunch of indirection around monkeypatching...  not sure if we need to as well... */

#define CHECK_STATUS(call) \
      if ((status = (call)) != yajl_gen_status_ok) { rb_funcall(mEncoder2, rb_intern("raise_error_for_status"), 1, INT2FIX(status)); }

static VALUE mEncoder_do_yajl_encode(VALUE self, VALUE obj, VALUE yajl_gen_opts, VALUE json_opts) {
  ID sym_ffi_yajl = rb_intern("ffi_yajl");
  VALUE sym_yajl_gen_beautify = ID2SYM(rb_intern("yajl_gen_beautify"));
  VALUE sym_yajl_gen_validate_utf8 = ID2SYM(rb_intern("yajl_gen_validate_utf8"));
  VALUE sym_yajl_gen_indent_string = ID2SYM(rb_intern("yajl_gen_indent_string"));
  yajl_gen yajl_gen;
  const unsigned char *buf;
  size_t len;
  VALUE state;
  VALUE ret;
  VALUE indent_string;
  VALUE rb_yajl_gen;

  yajl_gen = yajl_gen_alloc(NULL);

  if ( rb_hash_aref(yajl_gen_opts, sym_yajl_gen_beautify) == Qtrue ) {
    yajl_gen_config(yajl_gen, yajl_gen_beautify, 1);
  }
  if ( rb_hash_aref(yajl_gen_opts, sym_yajl_gen_validate_utf8) == Qtrue ) {
    yajl_gen_config(yajl_gen, yajl_gen_validate_utf8, 1);
  }

  indent_string = rb_hash_aref(yajl_gen_opts, sym_yajl_gen_indent_string);
  if (indent_string != Qnil) {
    yajl_gen_config(yajl_gen, yajl_gen_indent_string, RSTRING_PTR(indent_string));
  } else {
    yajl_gen_config(yajl_gen, yajl_gen_indent_string, " ");
  }

  state = rb_hash_new();

  rb_hash_aset(state, rb_str_new2("processing_key"), Qfalse);

  rb_hash_aset(state, rb_str_new2("json_opts"), json_opts);

  rb_yajl_gen = Data_Wrap_Struct(cYajl_Gen, NULL, NULL, yajl_gen);

  rb_funcall(obj, sym_ffi_yajl, 2, rb_yajl_gen, state);

  yajl_gen_get_buf(yajl_gen, &buf, &len);

  ret = rb_str_new2((char *)buf);

  yajl_gen_free(yajl_gen);

  return ret;
}

int rb_cHash_ffi_yajl_callback(VALUE key, VALUE val, VALUE extra) {
  ID sym_ffi_yajl = rb_intern("ffi_yajl");
  VALUE state = rb_hash_aref(extra, rb_str_new2("state"));
  VALUE rb_yajl_gen = rb_hash_aref(extra, rb_str_new2("yajl_gen"));

  rb_hash_aset(state, rb_str_new2("processing_key"), Qtrue);
  rb_funcall(key, sym_ffi_yajl, 2, rb_yajl_gen, state);
  rb_hash_aset(state, rb_str_new2("processing_key"), Qfalse);

  rb_funcall(val, sym_ffi_yajl, 2, rb_yajl_gen, state);

  return 0;
}

static VALUE rb_cHash_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) {
  yajl_gen_status status;
  VALUE extra;
  struct yajl_gen_t *yajl_gen;
  Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen);

  if ( rb_hash_aref(state, rb_str_new2("processing_key")) == Qtrue ) {
    ID sym_to_s = rb_intern("to_s");
    VALUE str = rb_funcall(self, sym_to_s, 0);
    char *cptr = RSTRING_PTR(str);
    int len = RSTRING_LEN(str);

    CHECK_STATUS(
      yajl_gen_string(yajl_gen, (unsigned char *)cptr, len)
    );
  } else {
    extra = rb_hash_new();  /* FIXME: reduce garbage */

    rb_hash_aset(extra, rb_str_new2("yajl_gen"), rb_yajl_gen);

    rb_hash_aset(extra, rb_str_new2("state"), state);

    CHECK_STATUS(
      yajl_gen_map_open(yajl_gen)
    );
    rb_hash_foreach(self, rb_cHash_ffi_yajl_callback, extra);
    CHECK_STATUS(
      yajl_gen_map_close(yajl_gen)
    );
  }

  return Qnil;
}

static VALUE rb_cArray_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) {
  yajl_gen_status status;
  ID sym_ffi_yajl = rb_intern("ffi_yajl");
  long i;
  VALUE val;
  struct yajl_gen_t *yajl_gen;
  Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen);

  if ( rb_hash_aref(state, rb_str_new2("processing_key")) == Qtrue ) {
    ID sym_to_s = rb_intern("to_s");
    VALUE str = rb_funcall(self, sym_to_s, 0);
    char *cptr = RSTRING_PTR(str);
    int len = RSTRING_LEN(str);

    CHECK_STATUS(
      yajl_gen_string(yajl_gen, (unsigned char *)cptr, len)
    );
  } else {
    CHECK_STATUS(
      yajl_gen_array_open(yajl_gen)
    );
    for(i=0; i<RARRAY_LEN(self); i++) {
      val = rb_ary_entry(self, i);
      rb_funcall(val, sym_ffi_yajl, 2, rb_yajl_gen, state);
    }
    CHECK_STATUS(
      yajl_gen_array_close(yajl_gen)
    );
  }

  return Qnil;
}

static VALUE rb_cNilClass_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) {
  yajl_gen_status status;
  struct yajl_gen_t *yajl_gen;
  Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen);

  if ( rb_hash_aref(state, rb_str_new2("processing_key")) == Qtrue ) {
    ID sym_to_s = rb_intern("to_s");
    VALUE str = rb_funcall(self, sym_to_s, 0);
    char *cptr = RSTRING_PTR(str);
    int len = RSTRING_LEN(str);

    CHECK_STATUS(
      yajl_gen_string(yajl_gen, (unsigned char *)cptr, len)
    );
  } else {
    CHECK_STATUS(
      yajl_gen_null(yajl_gen)
    );
  }

  return Qnil;
}

static VALUE rb_cTrueClass_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) {
  yajl_gen_status status;
  struct yajl_gen_t *yajl_gen;
  Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen);

  if ( rb_hash_aref(state, rb_str_new2("processing_key")) == Qtrue ) {
    ID sym_to_s = rb_intern("to_s");
    VALUE str = rb_funcall(self, sym_to_s, 0);
    char *cptr = RSTRING_PTR(str);
    int len = RSTRING_LEN(str);

    CHECK_STATUS(
      yajl_gen_string(yajl_gen, (unsigned char *)cptr, len)
    );
  } else {
    CHECK_STATUS(
      yajl_gen_bool(yajl_gen, 1)
    );
  }

  return Qnil;
}

static VALUE rb_cFalseClass_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) {
  yajl_gen_status status;
  struct yajl_gen_t *yajl_gen;
  Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen);

  if ( rb_hash_aref(state, rb_str_new2("processing_key")) == Qtrue ) {
    ID sym_to_s = rb_intern("to_s");
    VALUE str = rb_funcall(self, sym_to_s, 0);
    char *cptr = RSTRING_PTR(str);
    int len = RSTRING_LEN(str);

    CHECK_STATUS(
      yajl_gen_string(yajl_gen, (unsigned char *)cptr, len)
    );
  } else {
    CHECK_STATUS(
      yajl_gen_bool(yajl_gen, 0)
    );
  }

  return Qnil;
}

static VALUE rb_cFixnum_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) {
  yajl_gen_status status;
  ID sym_to_s = rb_intern("to_s");
  VALUE str = rb_funcall(self, sym_to_s, 0);
  char *cptr = RSTRING_PTR(str);
  int len = RSTRING_LEN(str);
  struct yajl_gen_t *yajl_gen;
  Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen);

  if (memcmp(cptr, "NaN", 3) == 0 || memcmp(cptr, "Infinity", 8) == 0 || memcmp(cptr, "-Infinity", 9) == 0) {
    rb_raise(cEncodeError, "'%s' is an invalid number", cptr);
  }
  if ( rb_hash_aref(state, rb_str_new2("processing_key")) == Qtrue ) {
    CHECK_STATUS(
      yajl_gen_string(yajl_gen, (unsigned char *)cptr, len)
    );
  } else {
    CHECK_STATUS(
      yajl_gen_number(yajl_gen, cptr, len)
    );
  }

  return Qnil;
}

static VALUE rb_cBignum_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) {
  yajl_gen_status status;
  ID sym_to_s = rb_intern("to_s");
  VALUE str = rb_funcall(self, sym_to_s, 0);
  char *cptr = RSTRING_PTR(str);
  int len = RSTRING_LEN(str);
  struct yajl_gen_t *yajl_gen;
  Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen);

  if (memcmp(cptr, "NaN", 3) == 0 || memcmp(cptr, "Infinity", 8) == 0 || memcmp(cptr, "-Infinity", 9) == 0) {
    rb_raise(cEncodeError, "'%s' is an invalid number", cptr);
  }
  if ( rb_hash_aref(state, rb_str_new2("processing_key")) == Qtrue ) {
    CHECK_STATUS(
      yajl_gen_string(yajl_gen, (unsigned char *)cptr, len)
    );
  } else {
    CHECK_STATUS(
      yajl_gen_number(yajl_gen, cptr, len)
    );
  }

  return Qnil;
}

static VALUE rb_cFloat_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) {
  yajl_gen_status status;
  ID sym_to_s = rb_intern("to_s");
  VALUE str = rb_funcall(self, sym_to_s, 0);
  char *cptr = RSTRING_PTR(str);
  int len = RSTRING_LEN(str);
  struct yajl_gen_t *yajl_gen;
  Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen);

  if (memcmp(cptr, "NaN", 3) == 0 || memcmp(cptr, "Infinity", 8) == 0 || memcmp(cptr, "-Infinity", 9) == 0) {
    rb_raise(cEncodeError, "'%s' is an invalid number", cptr);
  }
  if ( rb_hash_aref(state, rb_str_new2("processing_key")) == Qtrue ) {
    CHECK_STATUS(
      yajl_gen_string(yajl_gen, (unsigned char *)cptr, len)
    );
  } else {
    CHECK_STATUS(
      yajl_gen_number(yajl_gen, cptr, len)
    );
  }

  return Qnil;
}

static VALUE rb_cString_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) {
  yajl_gen_status status;
  struct yajl_gen_t *yajl_gen;
  Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen);

  CHECK_STATUS(
    yajl_gen_string(yajl_gen, (unsigned char *)RSTRING_PTR(self), RSTRING_LEN(self))
  );

  return Qnil;
}

static VALUE string_ffi_yajl(VALUE str, VALUE rb_yajl_gen, VALUE state) {
  yajl_gen_status status;
  char *cptr = RSTRING_PTR(str);
  int len = RSTRING_LEN(str);
  struct yajl_gen_t *yajl_gen;
  Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen);

  CHECK_STATUS(
    yajl_gen_string(yajl_gen, (unsigned char *)cptr, len)
  );

  return Qnil;
}

/* calls #to_s on an object to encode it */
static VALUE object_to_s_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) {
  ID sym_to_s = rb_intern("to_s");
  VALUE str = rb_funcall(self, sym_to_s, 0);
  return string_ffi_yajl(str, rb_yajl_gen, state);
}

static VALUE rb_cSymbol_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) {
  return object_to_s_ffi_yajl(self, rb_yajl_gen, state);
}

static VALUE rb_cDate_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) {
  return object_to_s_ffi_yajl(self, rb_yajl_gen, state);
}

static VALUE rb_cTime_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) {
  ID sym_strftime = rb_intern("strftime");
  VALUE str = rb_funcall(self, sym_strftime, 1, rb_str_new2("%Y-%m-%d %H:%M:%S %z"));
  return string_ffi_yajl(str, rb_yajl_gen, state);
}

static VALUE rb_cStringIO_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) {
  ID sym_read = rb_intern("read");
  VALUE str = rb_funcall(self, sym_read, 0);
  return string_ffi_yajl(str, rb_yajl_gen, state);
}

static VALUE rb_cDateTime_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) {
  return object_to_s_ffi_yajl(self, rb_yajl_gen, state);
}

static VALUE rb_cObject_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) {
  yajl_gen_status status;
  ID sym_to_json = rb_intern("to_json");
  VALUE str;

  if ( rb_hash_aref(state, rb_str_new2("processing_key")) != Qtrue && rb_respond_to(self, sym_to_json) ) {
    VALUE json_opts =  rb_hash_aref(state, rb_str_new2("json_opts"));
    struct yajl_gen_t *yajl_gen;
    Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen);

    str = rb_funcall(self, sym_to_json, 1, json_opts);
    CHECK_STATUS(
      yajl_gen_number(yajl_gen, (char *)RSTRING_PTR(str), RSTRING_LEN(str))
    );
  } else {
    object_to_s_ffi_yajl(self, rb_yajl_gen, state);
  }

  return Qnil;
}

void Init_encoder() {
  mFFI_Yajl = rb_define_module("FFI_Yajl");
  mEncoder2 = rb_define_class_under(mFFI_Yajl, "Encoder", rb_cObject);
  cEncodeError = rb_define_class_under(mFFI_Yajl, "EncodeError", rb_eStandardError);
  mExt = rb_define_module_under(mFFI_Yajl, "Ext");
  mEncoder = rb_define_module_under(mExt, "Encoder");
  cYajl_Gen = rb_define_class_under(mEncoder, "YajlGen", rb_cObject);
  rb_define_method(mEncoder, "do_yajl_encode", mEncoder_do_yajl_encode, 3);

  cDate = rb_define_class("Date", rb_cObject);
  cTime = rb_define_class("Time", rb_cObject);
  cDateTime = rb_define_class("DateTime", cDate);
  cStringIO = rb_define_class("StringIO", rb_cData);

  rb_define_method(rb_cHash, "ffi_yajl", rb_cHash_ffi_yajl, 2);
  rb_define_method(rb_cArray, "ffi_yajl", rb_cArray_ffi_yajl, 2);
  rb_define_method(rb_cNilClass, "ffi_yajl", rb_cNilClass_ffi_yajl, 2);
  rb_define_method(rb_cTrueClass, "ffi_yajl", rb_cTrueClass_ffi_yajl, 2);
  rb_define_method(rb_cFalseClass, "ffi_yajl", rb_cFalseClass_ffi_yajl, 2);
  rb_define_method(rb_cFixnum, "ffi_yajl", rb_cFixnum_ffi_yajl, 2);
  rb_define_method(rb_cBignum, "ffi_yajl", rb_cBignum_ffi_yajl, 2);
  rb_define_method(rb_cFloat, "ffi_yajl", rb_cFloat_ffi_yajl, 2);
  rb_define_method(rb_cString, "ffi_yajl", rb_cString_ffi_yajl, 2);
  rb_define_method(rb_cSymbol, "ffi_yajl", rb_cSymbol_ffi_yajl, 2);
  rb_define_method(cDate, "ffi_yajl", rb_cDate_ffi_yajl, 2);
  rb_define_method(cTime, "ffi_yajl", rb_cTime_ffi_yajl, 2);
  rb_define_method(cDateTime, "ffi_yajl", rb_cDateTime_ffi_yajl, 2);
  rb_define_method(cStringIO, "ffi_yajl", rb_cStringIO_ffi_yajl, 2);
  rb_define_method(rb_cObject, "ffi_yajl", rb_cObject_ffi_yajl, 2);
}