Class: Wankel

Inherits:
Object
  • Object
show all
Defined in:
lib/wankel.rb,
lib/wankel/stream_encoder.rb,
ext/wankel/wankel.c

Defined Under Namespace

Classes: EncodeError, ParseError, StreamEncoder, StreamParser

Constant Summary collapse

DEFAULTS =
read_buffer_size

The size of the read buffer in bytes. Deafults to 8092.

symbolize_keys

Set the keys of hashes to symbols instead of strings. Defaults to false.

allow_comments

Ignore JavaScript style comments present in JSON input. Defaults to false.

validate_utf8

Verify that all strings in the JSON input is valid UTF-8. Defaults to false.

allow_trailing_garbage

Don’t raise an error there is content after the end of the JSON document. Defaults to false.

multiple_values

Allow multiple JSON documents to be parsed. Every document must be valid and seperated by whitespace of any kind. The parser will continue to operate on input rather going into a completed state. Useful for parsing multiple values from an input stream. Defaults to false.

allow_partial_values

The parser will check the top level value was completely consumed by default, setting this allows partial JSON values. Defaults to false.

mode

The mode used for generate JSON. Setting mode to :strict mode only allows the 7 basic JSON types. Setting the mode to :nil replaces an object that is not a basic JSON type with null. If any other value is passed it will call the object method with the same name to serialize it to JSON. Defaults to :strict

write_buffer_size

The size of the write buffer in bytes. Defaults to 8092.

beautify

Generate indented (beautiful) output. Defaults to false.

indent_string

The indent string used when beautify is set. Defaults to ‘ ’ (4 spaces).

validate_utf8

Verify that all strings in JSON input are valid UTF-8. Defaults to false.

escape_solidus

Alway escape the forward solidus (‘/’), even though it is not required in JSON strings. Defaults to false.

{
  :read_buffer_size => 8092,
  :symbolize_keys => false,
  :allow_comments => false,
  :validate_utf8 => false,
  :allow_trailing_garbage => false,
  :multiple_values => false,
  :allow_partial_values => false,
    
  :mode => :strict,
  :write_buffer_size => 8092,
  :beautify => false,
  :indent_string => '    ',
  :escape_solidus => false
}

Class Method Summary collapse

Class Method Details

.encode(obj, io = nil, opts = nil) ⇒ Object

Returns the obj encoded as a JSON string.

obj

The object to be encoded as JSON

io

Either an IO object to which the encoded JSON is written or the options (opts) hash.

ops

An optional hash to override the encoding options. See DEFAULTS. If there is no IO object this is the second argument. If there is an IO object it is the third argument.



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'ext/wankel/wankel.c', line 55

static VALUE wankel_encode(int argc, VALUE * argv, VALUE klass) {
    VALUE encoder, input, output, options;
    rb_scan_args(argc, argv, "12", &input, &output, &options);
	

	if (TYPE(output) == T_HASH) {
		encoder = rb_funcall(c_wankelEncoder, intern_new, 1, output);
		return rb_funcall(encoder, intern_encode, 1, input);
	} else {
		encoder = rb_funcall(c_wankelEncoder, intern_new, 1, options);
		return rb_funcall(encoder, intern_encode, 2, input, output);
	}
}

.parse(io, opts = nil, &block = nil) ⇒ Object

io

The IO object parse JSON from.

opts

An optional hash of options to override the default parsing options. See DEFAULTS.

&block

An optional callback used with the multiple_values option. For example:

results = []
p = Wankel::Parser.new(:multiple_values => true)
p.parse('[{"abc": 123},{"def": 456}][{"abc": 123},{"def": 456}]') do |data|
  result << data
end


35
36
37
38
39
40
41
# File 'ext/wankel/wankel.c', line 35

static VALUE wankel_parse(int argc, VALUE * argv, VALUE klass) {
    VALUE parser, input, options, callback;
    rb_scan_args(argc, argv, "11&", &input, &options, &callback);
    
    parser = rb_funcall(c_wankelParser, intern_new, 1, options);
    return rb_funcall(parser, intern_parse, 2, input, callback);
}