Class: SpinelKit::Json::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/spinel_kit/json_builder.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBuilder

Returns a new instance of Builder.



38
39
40
41
# File 'lib/spinel_kit/json_builder.rb', line 38

def initialize
  @buf   = "{"
  @first = true
end

Class Method Details

.escape(s) ⇒ Object

Escape a string for inclusion inside a JSON string literal (no surrounding quotes). Handles “, , and the JSON control-char escapes (b f n r t); other control bytes go through u00XX. ASCII-clean input passes through unchanged.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/spinel_kit/json_builder.rb', line 101

def self.escape(s)
  out = ""
  i = 0
  n = s.length
  while i < n
    c = s[i]
    if c == "\""
      out = out + "\\\""
    elsif c == "\\"
      out = out + "\\\\"
    elsif c == "\n"
      out = out + "\\n"
    elsif c == "\r"
      out = out + "\\r"
    elsif c == "\t"
      out = out + "\\t"
    elsif c == "\b"
      out = out + "\\b"
    elsif c == "\f"
      out = out + "\\f"
    elsif c < " "
      b = c.getbyte(0)
      out = out + "\\u00" + Builder.hex2(b)
    else
      out = out + c
    end
    i += 1
  end
  out
end

.hex2(n) ⇒ Object

Two-digit lowercase hex of a byte (0..255).



133
134
135
136
137
138
139
# File 'lib/spinel_kit/json_builder.rb', line 133

def self.hex2(n)
  hex = "0123456789abcdef"
  out = ""
  out = out + hex[(n / 16) % 16, 1]
  out = out + hex[n % 16, 1]
  out
end

.quote(s) ⇒ Object

Wrap a string in JSON quotes, escaping its body.



93
94
95
# File 'lib/spinel_kit/json_builder.rb', line 93

def self.quote(s)
  "\"" + Builder.escape(s) + "\""
end

Instance Method Details

#add_bool(key, value) ⇒ Object

Append ‘“key”:true|false`.



57
58
59
60
# File 'lib/spinel_kit/json_builder.rb', line 57

def add_bool(key, value)
  comma
  @buf = @buf + Builder.quote(key) + ":" + (value ? "true" : "false")
end

#add_num(key, value) ⇒ Object

Append ‘“key”:<number>` – `value.to_s` covers Integer (“5”) and Float (“1.5”). For hardcoded literals prefer add_raw.



51
52
53
54
# File 'lib/spinel_kit/json_builder.rb', line 51

def add_num(key, value)
  comma
  @buf = @buf + Builder.quote(key) + ":" + value.to_s
end

#add_obj(key, child) ⇒ Object

Append ‘“key”:<nested object>` from another Builder.



69
70
71
72
# File 'lib/spinel_kit/json_builder.rb', line 69

def add_obj(key, child)
  comma
  @buf = @buf + Builder.quote(key) + ":" + child.dump
end

#add_raw(key, raw) ⇒ Object

Append ‘“key”:<already-encoded JSON>` – for arrays / numeric literals.



63
64
65
66
# File 'lib/spinel_kit/json_builder.rb', line 63

def add_raw(key, raw)
  comma
  @buf = @buf + Builder.quote(key) + ":" + raw
end

#add_str(key, value) ⇒ Object

Append ‘“key”:“escaped-value”`.



44
45
46
47
# File 'lib/spinel_kit/json_builder.rb', line 44

def add_str(key, value)
  comma
  @buf = @buf + Builder.quote(key) + ":" + Builder.quote(value)
end

#commaObject

Emit a separator before every entry except the first.



80
81
82
83
84
85
86
# File 'lib/spinel_kit/json_builder.rb', line 80

def comma
  if @first
    @first = false
  else
    @buf = @buf + ","
  end
end

#dumpObject

Close the object and return the JSON string.



75
76
77
# File 'lib/spinel_kit/json_builder.rb', line 75

def dump
  @buf + "}"
end