Module: JSONExtractor

Defined in:
lib/json_extractor.rb,
ext/json_extractor/json_extractor.c

Class Method Summary collapse

Class Method Details

.extract_subdocument(str, key) ⇒ Object



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
# File 'ext/json_extractor/json_extractor.c', line 181

static VALUE rb_extract_subdocument(VALUE self, VALUE str, VALUE key) {
  char *data, *substr;
  VALUE result;

  // No str?
  if(str == Qnil) {
    return Qnil;
  }

  // No key?
  if(key == Qnil) {
    return Qnil;
  }

  data = read_all(RSTRING_PTR(str));
  substr = extract_subdocument(data, RSTRING_PTR(key));
  free(data);

  if(substr == NULL) {
    return Qnil;
  }

  result = rb_str_new2(substr);

  // TODO: Figure out if this is right. It behaves properly, so I'm assuming
  // it's OK.
  free(substr);

  return result;
}

.subdocument(filename, key) ⇒ Object



6
7
8
# File 'lib/json_extractor.rb', line 6

def self.subdocument(filename, key)
  JSON.parse(extract_subdocument(filename, key))
end