Module: RapidJSON

Defined in:
lib/rapidjson/json_gem.rb,
ext/rapidjson/cext.cc

Overview

to_json

Defined Under Namespace

Modules: JSONGemCompact Classes: EncodeError, ParseError

Class Method Summary collapse

Class Method Details

.dump(obj) ⇒ Object



21
22
23
24
# File 'ext/rapidjson/cext.cc', line 21

VALUE encode(VALUE _self, VALUE obj) {
    RubyObjectEncoder<DefaultBuffer, Writer<DefaultBuffer>> encoder;
    return encoder.encode(obj);
}

.encode(obj) ⇒ Object



21
22
23
24
# File 'ext/rapidjson/cext.cc', line 21

VALUE encode(VALUE _self, VALUE obj) {
    RubyObjectEncoder<DefaultBuffer, Writer<DefaultBuffer>> encoder;
    return encoder.encode(obj);
}

.load(string) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'ext/rapidjson/cext.cc', line 31

VALUE parse(VALUE _self, VALUE string) {
    RubyObjectHandler handler;
    Reader reader;
    char *cstring = StringValueCStr(string); // fixme?
    StringStream ss(cstring);
    ParseResult ok = reader.Parse(ss, handler);

    if (!ok) {
        rb_raise(rb_eParseError, "JSON parse error: %s (%lu)",
                GetParseError_En(ok.Code()), ok.Offset());
    }

    return handler.GetRoot();
}

.parse(string) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'ext/rapidjson/cext.cc', line 31

VALUE parse(VALUE _self, VALUE string) {
    RubyObjectHandler handler;
    Reader reader;
    char *cstring = StringValueCStr(string); // fixme?
    StringStream ss(cstring);
    ParseResult ok = reader.Parse(ss, handler);

    if (!ok) {
        rb_raise(rb_eParseError, "JSON parse error: %s (%lu)",
                GetParseError_En(ok.Code()), ok.Offset());
    }

    return handler.GetRoot();
}

.pretty_encode(obj) ⇒ Object



26
27
28
29
# File 'ext/rapidjson/cext.cc', line 26

VALUE pretty_encode(VALUE _self, VALUE obj) {
    RubyObjectEncoder<DefaultBuffer, PrettyWriter<DefaultBuffer>> encoder;
    return encoder.encode(obj);
}

.valid_json?(string) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'ext/rapidjson/cext.cc', line 46

VALUE valid_json_p(VALUE _self, VALUE string) {
    NullHandler handler;
    Reader reader;
    char *cstring = StringValueCStr(string); // fixme?
    StringStream ss(cstring);
    ParseResult ok = reader.Parse(ss, handler);

    if (!ok) {
        return Qfalse;
    }

    return Qtrue;
}