Module: Exwiw::ExtJson

Defined in:
lib/exwiw/ext_json.rb,
ext/exwiw/ext_json/ext_json.c

Overview

MongoDB Relaxed Extended JSON encoder for a single dumped document.

‘encode` is the one entry point. When the optional native extension compiled (the common case once `gem install exwiw` builds it), it emits the line in a single C tree-walk; otherwise it falls back to the pure-Ruby path. Both are byte-for-byte identical — the native path delegates every value it does not format itself back to `encode_fragment` (see ext/exwiw/ext_json/ext_json.c).

Class Method Summary collapse

Class Method Details

.encode_fragment(value) ⇒ Object

Pure-Ruby encoder for one value, identical to the historical ‘JSON.generate(doc.as_extended_json(mode: :relaxed))`. Used both as the whole-document fallback and as the native path’s per-value delegate, so the two paths cannot diverge.



20
21
22
# File 'lib/exwiw/ext_json.rb', line 20

def encode_fragment(value)
  JSON.generate(value.respond_to?(:as_extended_json) ? value.as_extended_json(mode: :relaxed) : value)
end

.encode_native(doc) ⇒ Object

Returns one JSONL line (no trailing newline); the caller owns separators.



249
250
251
252
253
254
255
256
257
# File 'ext/exwiw/ext_json/ext_json.c', line 249

static VALUE rb_encode_native(VALUE self, VALUE doc)
{
    resolve_objectid_class();

    VALUE buf = rb_str_buf_new(256);
    rb_enc_associate(buf, rb_utf8_encoding());
    encode_value(buf, doc);
    return buf;
}