Class: Transit::Marshaler::BaseJson

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/transit/marshaler/cruby/json.rb

Direct Known Subclasses

Json, VerboseJson

Instance Method Summary collapse

Methods included from Base

#build_handlers, #emit_array, #emit_boolean, #emit_double, #emit_encoded, #emit_map, #emit_nil, #emit_string, #emit_tagged_value, #escape, #find_handler, #marshal, #marshal_top, #parse_options

Constructor Details

#initialize(io, opts) ⇒ BaseJson

Returns a new instance of BaseJson.



22
23
24
25
26
27
28
29
# File 'lib/transit/marshaler/cruby/json.rb', line 22

def initialize(io, opts)
  @oj = Oj::StreamWriter.new(io,opts.delete(:oj_opts) || {})
  @state = []
  @max_int = JSON_MAX_INT
  @min_int = JSON_MIN_INT
  @prefer_strings = true
  parse_options(opts)
end

Instance Method Details

#emit_array_endObject



36
37
38
39
# File 'lib/transit/marshaler/cruby/json.rb', line 36

def emit_array_end
  @state.pop
  @oj.pop
end

#emit_array_start(size) ⇒ Object



31
32
33
34
# File 'lib/transit/marshaler/cruby/json.rb', line 31

def emit_array_start(size)
  @state << :array
  @oj.push_array
end

#emit_int(tag, i, as_map_key, cache) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/transit/marshaler/cruby/json.rb', line 51

def emit_int(tag, i, as_map_key, cache)
  if as_map_key || i > @max_int || i < @min_int
    emit_string(ESC, tag, i, as_map_key, cache)
  else
    emit_value(i, as_map_key)
  end
end

#emit_map_endObject



46
47
48
49
# File 'lib/transit/marshaler/cruby/json.rb', line 46

def emit_map_end
  @state.pop
  @oj.pop
end

#emit_map_start(size) ⇒ Object



41
42
43
44
# File 'lib/transit/marshaler/cruby/json.rb', line 41

def emit_map_start(size)
  @state << :map
  @oj.push_object
end

#emit_value(obj, as_map_key = false) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/transit/marshaler/cruby/json.rb', line 59

def emit_value(obj, as_map_key=false)
  if @state.last == :array
    @oj.push_value(obj)
  else
    as_map_key ? @oj.push_key(obj) : @oj.push_value(obj)
  end
end

#flushObject



67
68
69
# File 'lib/transit/marshaler/cruby/json.rb', line 67

def flush
  # no-op
end