summaryrefslogtreecommitdiff
path: root/ext/ffi_yajl/ext/encoder/encoder.c
blob: c57cb49b7d5b14eae715b9e8a8e7ff8544bce94b (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
#include <ruby.h>
#include <yajl/yajl_gen.h>

static VALUE mFFI_Yajl, mExt, mEncoder, mEncoder2, cEncodeError;

/* 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, status); }

typedef struct {
  VALUE json_opts;
  int processing_key;
} ffi_state_t;

static VALUE mEncoder_do_yajl_encode(VALUE self, VALUE obj, VALUE yajl_gen_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;
  ffi_state_t state;
  VALUE ret;
  VALUE indent_string;

  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.processing_key = 0;

  rb_funcall(obj, sym_ffi_yajl, 2, yajl_gen, (VALUE) &state);

  yajl_gen_get_buf(yajl_gen, &buf, &len);

  ret = rb_str_new2((char *)buf);

  yajl_gen_free(yajl_gen);

  return ret;
}

typedef struct {
  VALUE yajl_gen;
  ffi_state_t *state;
} ffs_extra_t;

int rb_cHash_ffi_yajl_callback(VALUE key, VALUE val, VALUE extra) {
  ffs_extra_t *extra_p = (ffs_extra_t *)extra;
  ID sym_ffi_yajl = rb_intern("ffi_yajl");

  extra_p->state->processing_key = 1;
  rb_funcall(key, sym_ffi_yajl, 2, extra_p->yajl_gen, extra_p->state);
  extra_p->state->processing_key = 0;
  rb_funcall(val, sym_ffi_yajl, 2, extra_p->yajl_gen, extra_p->state);

  return 0;
}

static VALUE rb_cHash_ffi_yajl(VALUE self, VALUE yajl_gen, VALUE state) {
  yajl_gen_status status;
  ffs_extra_t extra;

  extra.yajl_gen = yajl_gen;
  extra.state = (ffi_state_t *)state;

  CHECK_STATUS(
    yajl_gen_map_open((struct yajl_gen_t *) yajl_gen)
  );
  rb_hash_foreach(self, rb_cHash_ffi_yajl_callback, (VALUE) &extra);
  CHECK_STATUS(
    yajl_gen_map_close((struct yajl_gen_t *) yajl_gen)
  );

  return Qnil;
}

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

  CHECK_STATUS(
    yajl_gen_array_open((struct yajl_gen_t *) yajl_gen)
  );
  for(i=0; i<RARRAY_LEN(self); i++) {
    val = rb_ary_entry(self, i);
    rb_funcall(val, sym_ffi_yajl, 2, yajl_gen, state);
  }
  CHECK_STATUS(
    yajl_gen_array_close((struct yajl_gen_t *) yajl_gen)
  );

  return Qnil;
}

static VALUE rb_cNilClass_ffi_yajl(VALUE self, VALUE yajl_gen, VALUE state) {
  yajl_gen_status status;
  CHECK_STATUS(
    yajl_gen_null((struct yajl_gen_t *) yajl_gen)
  );
  return Qnil;
}

static VALUE rb_cTrueClass_ffi_yajl(VALUE self, VALUE yajl_gen, VALUE state) {
  yajl_gen_status status;
  CHECK_STATUS(
    yajl_gen_bool((struct yajl_gen_t *) yajl_gen, 1)
  );
  return Qnil;
}

static VALUE rb_cFalseClass_ffi_yajl(VALUE self, VALUE yajl_gen, VALUE state) {
  yajl_gen_status status;
  CHECK_STATUS(
    yajl_gen_bool((struct yajl_gen_t *) yajl_gen, 0)
  );
  return Qnil;
}

static VALUE rb_cFixnum_ffi_yajl(VALUE self, VALUE 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);

  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 ( ((ffi_state_t *)state)->processing_key ) {
    CHECK_STATUS(
      yajl_gen_string((struct yajl_gen_t *) yajl_gen, (unsigned char *)cptr, len)
    );
  } else {
    CHECK_STATUS(
      yajl_gen_integer((struct yajl_gen_t *) yajl_gen, NUM2INT(self))
    );
  }
  return Qnil;
}

static VALUE rb_cBignum_ffi_yajl(VALUE self, VALUE 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);
  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 ( ((ffi_state_t *)state)->processing_key ) {
    CHECK_STATUS(
      yajl_gen_string((struct yajl_gen_t *) yajl_gen, (unsigned char *)cptr, len)
    );
  } else {
    CHECK_STATUS(
      yajl_gen_number((struct yajl_gen_t *) yajl_gen, cptr, len)
    );
  }
  return Qnil;
}

static VALUE rb_cFloat_ffi_yajl(VALUE self, VALUE 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);
  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 ( ((ffi_state_t *)state)->processing_key ) {
    CHECK_STATUS(
      yajl_gen_string((struct yajl_gen_t *) yajl_gen, (unsigned char *)cptr, len)
    );
  } else {
    CHECK_STATUS(
      yajl_gen_number((struct yajl_gen_t *) yajl_gen, cptr, len)
    );
  }
  return Qnil;
}


static VALUE rb_cString_ffi_yajl(VALUE self, VALUE yajl_gen, VALUE state) {
  yajl_gen_status status;
  CHECK_STATUS(
    yajl_gen_string((struct yajl_gen_t *) yajl_gen, (unsigned char *)RSTRING_PTR(self), RSTRING_LEN(self))
  );
  return Qnil;
}

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

  str = rb_funcall(self, sym_to_json, 1, ((ffi_state_t *)state)->json_opts);
  CHECK_STATUS(
    yajl_gen_number((struct yajl_gen_t *) yajl_gen, (char *)RSTRING_PTR(str), RSTRING_LEN(str))
  );
  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");
  rb_define_method(mEncoder, "do_yajl_encode", mEncoder_do_yajl_encode, 2);

  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_cObject, "ffi_yajl", rb_cObject_ffi_yajl, 2);
}