Class: MicroJSON::Encoder

Inherits:
Object show all
Defined in:
lib/moon/packages/micro_json/micro_json.rb

Overview

Class for writing JSON streams.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream, options = {}) ⇒ Encoder

Returns a new instance of Encoder.

Parameters:

  • stream (#<<)
  • options (Hash) (defaults to: {})

11
12
13
14
# File 'lib/moon/packages/micro_json/micro_json.rb', line 11

def initialize(stream, options = {})
  @options = options
  @stream = stream
end

Instance Attribute Details

#streamObject (readonly)

Returns the value of attribute stream


7
8
9
# File 'lib/moon/packages/micro_json/micro_json.rb', line 7

def stream
  @stream
end

Class Method Details

.encode(obj, options = {}, depth = 0) ⇒ Object

Dumps provided object as JSON and returns the result.

Parameters:


148
149
150
151
152
# File 'lib/moon/packages/micro_json/micro_json.rb', line 148

def self.encode(obj, options = {}, depth = 0)
  result = ''
  new(result, options).write_value(obj, depth)
  result
end

Instance Method Details

#write(str, depth = 0) ⇒ Object

Write to the underlying stream

Parameters:


20
21
22
# File 'lib/moon/packages/micro_json/micro_json.rb', line 20

def write(str, depth = 0)
  stream << str
end

#write_array(array, depth = 0) ⇒ Object

Writes an Array to the stream.

Parameters:


91
92
93
94
95
96
97
98
99
# File 'lib/moon/packages/micro_json/micro_json.rb', line 91

def write_array(array, depth = 0)
  write('[')
  end_ = array.size - 1
  array.each_with_index do |value, i|
    write_value(value, depth + 1)
    write(',') unless i == end_
  end
  write(']')
end

#write_boolean(bool, depth = 0) ⇒ Object

Writes a Boolean to the stream.

Parameters:


75
76
77
# File 'lib/moon/packages/micro_json/micro_json.rb', line 75

def write_boolean(bool, depth = 0)
  write(bool ? 'true' : 'false')
end

#write_float(flt, depth = 0) ⇒ Object

Writes a Float to the stream.

Parameters:


67
68
69
# File 'lib/moon/packages/micro_json/micro_json.rb', line 67

def write_float(flt, depth = 0)
  write_number(flt, depth + 1)
end

#write_integer(int, depth = 0) ⇒ Object

Writes an Integer to the stream.

Parameters:


59
60
61
# File 'lib/moon/packages/micro_json/micro_json.rb', line 59

def write_integer(int, depth = 0)
  write_number(int, depth + 1)
end

#write_null(null, depth = 0) ⇒ Object

Writes a nil to the stream

Parameters:

  • null (nil)

    not needed, only kept to match the other write methods.

  • depth (Integer) (defaults to: 0)

83
84
85
# File 'lib/moon/packages/micro_json/micro_json.rb', line 83

def write_null(null, depth = 0)
  write('null')
end

#write_number(num, depth = 0) ⇒ Object

Writes a Numeric to the stream.

Parameters:


51
52
53
# File 'lib/moon/packages/micro_json/micro_json.rb', line 51

def write_number(num, depth = 0)
  write(num.to_s, depth + 1)
end

#write_object(object, depth = 0) ⇒ Object

Writes a Hash to the stream.

Parameters:


105
106
107
108
109
110
111
112
113
114
115
# File 'lib/moon/packages/micro_json/micro_json.rb', line 105

def write_object(object, depth = 0)
  write('{')
  end_ = object.size - 1
  object.each_with_index do |pair, i|
    k, v = *pair
    write("\"#{k}\":")
    write_value(v)
    write(',') unless i == end_
  end
  write('}')
end

#write_string(str, depth = 0) ⇒ Object

Write a String to the stream.

Parameters:


28
29
30
# File 'lib/moon/packages/micro_json/micro_json.rb', line 28

def write_string(str, depth = 0)
  write(str.dump, depth + 1)
end

#write_symbol(sym, depth = 0) ⇒ Object

Writes a Symbol to the stream, this is affected by the :symbols option passed into the Encoder. if :symbols is true, Symbol are JSON strings prefixed with :, otherwise they appear as regular strings.

Parameters:


39
40
41
42
43
44
45
# File 'lib/moon/packages/micro_json/micro_json.rb', line 39

def write_symbol(sym, depth = 0)
  if @options[:symbols]
    write_string(":#{sym}", depth = 0)
  else
    write_string(sym.to_s, depth = 0)
  end
end

#write_value(value, depth = 0) ⇒ Object

Writes a ruby Object to the stream, if the Type cannot be matched, the object is dumped using #to_json, if the object does not define a #to_json method a TypeError is raised.

Parameters:


123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/moon/packages/micro_json/micro_json.rb', line 123

def write_value(value, depth = 0)
  case value
  when String      then write_string(value, depth + 1)
  when Symbol      then write_symbol(value, depth + 1)
  when Integer     then write_integer(value, depth + 1)
  when Float       then write_float(value, depth + 1)
  when Array       then write_array(value, depth + 1)
  when Hash        then write_object(value, depth + 1)
  when true, false then write_boolean(value, depth + 1)
  when nil         then write_null(value, depth + 1)
  else
    if value.respond_to?(:to_json)
      write(value.to_json, depth + 1)
    else
      raise TypeError,
            "cannot encode #{value} (of type #{value.class})."
    end
  end
end