Class: Oj::Rails::Encoder

Inherits:
Object
  • Object
show all
Defined in:
ext/oj/mimic_rails.c,
ext/oj/mimic_rails.c

Overview

The Oj ActiveSupport compliant encoder.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(options = nil) ⇒ Object

Creates a new Encoder.

  • options [Hash] formatting options



408
409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'ext/oj/mimic_rails.c', line 408

static VALUE
encoder_new(int argc, VALUE *argv, VALUE self) {
    Encoder	e = ALLOC(struct _Encoder);

    e->opts = oj_default_options;
    e->arg = Qnil;
    copy_opts(&ropts, &e->ropts);
    
    if (1 <= argc && Qnil != *argv) {
	oj_parse_options(*argv, &e->opts);
	e->arg = *argv;
    }
    return Data_Wrap_Struct(encoder_class, encoder_mark, encoder_free, e);
}

Instance Method Details

#deoptimize(*classes) ⇒ Object

Turn off Oj rails optimization on the specified classes.

  • classes [Class] a list of classes to deoptimize



494
495
496
497
498
499
500
501
# File 'ext/oj/mimic_rails.c', line 494

static VALUE
encoder_deoptimize(int argc, VALUE *argv, VALUE self) {
    Encoder	e = (Encoder)DATA_PTR(self);

    optimize(argc, argv, &e->ropts, false);

    return Qnil;
}

#encode(obj) ⇒ Object

  • obj [Object] object to encode

Returns encoded object as a JSON string.



640
641
642
643
644
645
646
647
648
649
650
# File 'ext/oj/mimic_rails.c', line 640

static VALUE
encoder_encode(VALUE self, VALUE obj) {
    Encoder	e = (Encoder)DATA_PTR(self);

    if (Qnil != e->arg) {
	VALUE	argv[1] = { e->arg };
	
	return encode(obj, &e->ropts, &e->opts, 1, argv);
    }
    return encode(obj, &e->ropts, &e->opts, 0, NULL);
}

#optimize(*args) ⇒ Object

Document-method optimize call-seq: optimize(*classes)

Use Oj rails optimized routines to encode the specified classes. This ignores the as_json() method on the class and uses an internal encoding instead. Passing in no classes indicates all should use the optimized version of encoding for all previously optimized classes. Passing in the Object class set a global switch that will then use the optimized behavior for all classes.

  • classes [Class] a list of classes to optimize



459
460
461
462
463
464
465
466
# File 'ext/oj/mimic_rails.c', line 459

static VALUE
encoder_optimize(int argc, VALUE *argv, VALUE self) {
    Encoder	e = (Encoder)DATA_PTR(self);

    optimize(argc, argv, &e->ropts, true);

    return Qnil;
}

#optimized?(clas) ⇒ Boolean

  • clas [Class] Class to check

Returns:

  • (Boolean)

    true if the class is being optimized for rails and false otherwise



524
525
526
527
528
529
530
531
532
533
# File 'ext/oj/mimic_rails.c', line 524

static VALUE
encoder_optimized(VALUE self, VALUE clas) {
    Encoder	e = (Encoder)DATA_PTR(self);
    ROpt	ro = oj_rails_get_opt(&e->ropts, clas);

    if (NULL == ro) {
	return Qfalse;
    }
    return (ro->on) ? Qtrue : Qfalse;
}