spinel_kit 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +48 -0
- data/LICENSE +21 -0
- data/README.md +135 -0
- data/docs/adoption.md +114 -0
- data/docs/gem-audit-first.md +76 -0
- data/docs/spinel-discipline.md +106 -0
- data/docs/spinelgems-issues.md +74 -0
- data/lib/spinel_kit/git.rb +73 -0
- data/lib/spinel_kit/json.rb +149 -0
- data/lib/spinel_kit/json_builder.rb +142 -0
- data/lib/spinel_kit/json_decoder.rb +394 -0
- data/lib/spinel_kit/log.rb +87 -0
- data/lib/spinel_kit/version.rb +6 -0
- data/lib/spinel_kit.rb +39 -0
- data/sig/spinel_kit/git.rbs +8 -0
- data/sig/spinel_kit/json.rbs +15 -0
- data/sig/spinel_kit/json_builder.rbs +19 -0
- data/sig/spinel_kit/json_decoder.rbs +21 -0
- data/sig/spinel_kit/log.rbs +19 -0
- data/sig/spinel_kit/version.rbs +3 -0
- data/spinel-ext.json +1 -0
- metadata +75 -0
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
# SpinelKit::Json -- Spinel-safe JSON ENCODERS (stateless).
|
|
2
|
+
#
|
|
3
|
+
# This file holds the encode half of the codec; the decode half lives in
|
|
4
|
+
# spinel_kit/json_decoder.rb (also `SpinelKit::Json`), and the incremental
|
|
5
|
+
# object builder in spinel_kit/json_builder.rb (`SpinelKit::Json::Builder`).
|
|
6
|
+
# The three are split because Spinel has no tree-shaking: every loaded method
|
|
7
|
+
# is compiled, and a set of uncalled methods can degrade each other's params
|
|
8
|
+
# (e.g. the dead decoder walkers collectively widening `escape`'s string arg
|
|
9
|
+
# to int, which silently miscompiled string keys to ""). Keeping
|
|
10
|
+
# encode/decode/build in separate files means a consumer compiles only the
|
|
11
|
+
# surface it calls, and each surface is independently warning-clean. Require
|
|
12
|
+
# only what you use:
|
|
13
|
+
#
|
|
14
|
+
# require "spinel_kit/json" # encoders (this file)
|
|
15
|
+
# require "spinel_kit/json_decoder" # decoders
|
|
16
|
+
# require "spinel_kit/json_builder" # builder
|
|
17
|
+
#
|
|
18
|
+
# WHY HAND-ROLLED. Spinel cannot lower the stdlib `json` gem (C-ext fast path
|
|
19
|
+
# + metaprogrammed pure fallback); `oj`/`yajl`/`multi_json` are C extensions.
|
|
20
|
+
# The spinelgems catalog confirms no verified pure-Ruby JSON gem exists. This
|
|
21
|
+
# is tep's encoder, standardized (the `j_`/`tj_` prefixes that worked around a
|
|
22
|
+
# now-fixed Spinel inference bug are gone -- see docs/spinel-discipline.md).
|
|
23
|
+
#
|
|
24
|
+
# Compose objects in user code by concatenation:
|
|
25
|
+
#
|
|
26
|
+
# "{" + SpinelKit::Json.encode_pair_str("name", name) + "," +
|
|
27
|
+
# SpinelKit::Json.encode_pair_int("age", age) + "}"
|
|
28
|
+
module SpinelKit
|
|
29
|
+
class Json
|
|
30
|
+
# Escape a string for inclusion inside a JSON string literal (does NOT
|
|
31
|
+
# add the surrounding quotes -- use `quote(s)` for that). Handles ", \,
|
|
32
|
+
# and the JSON-required control-char escapes (\b, \f, \n, \r, \t);
|
|
33
|
+
# other control bytes go through \u00XX. Forward slash is left
|
|
34
|
+
# unescaped (legal either way; unescaped is shorter/readable).
|
|
35
|
+
def self.escape(s)
|
|
36
|
+
out = ""
|
|
37
|
+
i = 0
|
|
38
|
+
n = s.length
|
|
39
|
+
while i < n
|
|
40
|
+
c = s[i]
|
|
41
|
+
if c == "\""
|
|
42
|
+
out = out + "\\\""
|
|
43
|
+
elsif c == "\\"
|
|
44
|
+
out = out + "\\\\"
|
|
45
|
+
elsif c == "\n"
|
|
46
|
+
out = out + "\\n"
|
|
47
|
+
elsif c == "\r"
|
|
48
|
+
out = out + "\\r"
|
|
49
|
+
elsif c == "\t"
|
|
50
|
+
out = out + "\\t"
|
|
51
|
+
elsif c == "\b"
|
|
52
|
+
out = out + "\\b"
|
|
53
|
+
elsif c == "\f"
|
|
54
|
+
out = out + "\\f"
|
|
55
|
+
elsif c < " "
|
|
56
|
+
# Other control byte -- emit \u00XX. c.getbyte(0) is the raw
|
|
57
|
+
# byte value, mapped to two hex digits.
|
|
58
|
+
b = c.getbyte(0)
|
|
59
|
+
out = out + "\\u00" + Json.hex2(b)
|
|
60
|
+
else
|
|
61
|
+
out = out + c
|
|
62
|
+
end
|
|
63
|
+
i += 1
|
|
64
|
+
end
|
|
65
|
+
out
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Two-digit lowercase hex of a byte (0..255).
|
|
69
|
+
def self.hex2(n)
|
|
70
|
+
hex = "0123456789abcdef"
|
|
71
|
+
out = ""
|
|
72
|
+
out = out + hex[(n / 16) % 16, 1]
|
|
73
|
+
out = out + hex[n % 16, 1]
|
|
74
|
+
out
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# Wrap a string in JSON quotes, escaping its body.
|
|
78
|
+
def self.quote(s)
|
|
79
|
+
"\"" + Json.escape(s) + "\""
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Encode a single key/value pair as `"k":"v"` (escaped both sides).
|
|
83
|
+
def self.encode_pair_str(k, v)
|
|
84
|
+
Json.quote(k) + ":" + Json.quote(v)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# Same shape, integer value side. `v` is rendered via `.to_s` so
|
|
88
|
+
# JSON-numeric output without quoting.
|
|
89
|
+
def self.encode_pair_int(k, v)
|
|
90
|
+
Json.quote(k) + ":" + v.to_s
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# Encode a Hash<String,String> as a JSON object.
|
|
94
|
+
def self.from_str_hash(h)
|
|
95
|
+
out = "{"
|
|
96
|
+
first = true
|
|
97
|
+
h.each do |k, v|
|
|
98
|
+
if !first
|
|
99
|
+
out = out + ","
|
|
100
|
+
end
|
|
101
|
+
first = false
|
|
102
|
+
out = out + Json.quote(k) + ":" + Json.quote(v)
|
|
103
|
+
end
|
|
104
|
+
out + "}"
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# Same shape with integer values. JSON-numeric, no quoting.
|
|
108
|
+
def self.from_int_hash(h)
|
|
109
|
+
out = "{"
|
|
110
|
+
first = true
|
|
111
|
+
h.each do |k, v|
|
|
112
|
+
if !first
|
|
113
|
+
out = out + ","
|
|
114
|
+
end
|
|
115
|
+
first = false
|
|
116
|
+
out = out + Json.quote(k) + ":" + v.to_s
|
|
117
|
+
end
|
|
118
|
+
out + "}"
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# Encode a string array as a JSON array of quoted strings.
|
|
122
|
+
def self.from_str_array(a)
|
|
123
|
+
out = "["
|
|
124
|
+
i = 0
|
|
125
|
+
while i < a.length
|
|
126
|
+
if i > 0
|
|
127
|
+
out = out + ","
|
|
128
|
+
end
|
|
129
|
+
out = out + Json.quote(a[i])
|
|
130
|
+
i += 1
|
|
131
|
+
end
|
|
132
|
+
out + "]"
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
# Encode an int array as a JSON array of numbers.
|
|
136
|
+
def self.from_int_array(a)
|
|
137
|
+
out = "["
|
|
138
|
+
i = 0
|
|
139
|
+
while i < a.length
|
|
140
|
+
if i > 0
|
|
141
|
+
out = out + ","
|
|
142
|
+
end
|
|
143
|
+
out = out + a[i].to_s
|
|
144
|
+
i += 1
|
|
145
|
+
end
|
|
146
|
+
out + "]"
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
end
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
# SpinelKit::Json::Builder -- an incremental, ordered JSON-object builder.
|
|
2
|
+
#
|
|
3
|
+
# WHY A SEPARATE FILE/CLASS. This is toy's hand-rolled builder; the codec
|
|
4
|
+
# (SpinelKit::Json, encode/decode) is tep's. They are split so a consumer
|
|
5
|
+
# compiles only the surface it uses: Spinel has no tree-shaking, so an
|
|
6
|
+
# uncalled method is still compiled and degrades its params, emitting benign
|
|
7
|
+
# but gate-tripping `emitting 0` warnings. A builder-only consumer requires
|
|
8
|
+
# THIS file and never pays for the decoder walkers; a codec-only consumer
|
|
9
|
+
# requires spinel_kit/json and never pays for the builder.
|
|
10
|
+
#
|
|
11
|
+
# To stay self-contained, Builder carries its OWN escape/quote/hex2 rather
|
|
12
|
+
# than calling SpinelKit::Json.* (which would drag the whole codec, decoders
|
|
13
|
+
# included, into a builder-only compile). These are byte-identical to the
|
|
14
|
+
# codec's; the small duplication buys surface isolation. The same-named
|
|
15
|
+
# methods across the two classes are safe -- the Spinel name-collision bug
|
|
16
|
+
# that once made that dangerous is fixed (see docs/spinel-discipline.md).
|
|
17
|
+
#
|
|
18
|
+
# USAGE (mutating appenders, NOT method-chaining -- chaining is a Spinel
|
|
19
|
+
# poly-degradation risk):
|
|
20
|
+
#
|
|
21
|
+
# j = SpinelKit::Json::Builder.new
|
|
22
|
+
# j.add_str("kind", "run_start")
|
|
23
|
+
# j.add_num("t", now) # int OR float via .to_s
|
|
24
|
+
# host = SpinelKit::Json::Builder.new
|
|
25
|
+
# host.add_str("name", host_name)
|
|
26
|
+
# j.add_obj("host", host) # nest a sub-builder
|
|
27
|
+
# j.add_raw("lr", "0.001") # already-encoded JSON literal
|
|
28
|
+
# ev = j.dump # "{...}"
|
|
29
|
+
#
|
|
30
|
+
# NUMERIC NOTE: if a single compiled program passes BOTH Integer and Float to
|
|
31
|
+
# `add_num`'s `value`, Spinel unifies the param to Float and an Integer
|
|
32
|
+
# renders as "N.0". For hardcoded numeric literals where byte-exact output
|
|
33
|
+
# matters, use `add_raw` with an already-encoded string instead of relying on
|
|
34
|
+
# `value.to_s`.
|
|
35
|
+
module SpinelKit
|
|
36
|
+
class Json
|
|
37
|
+
class Builder
|
|
38
|
+
def initialize
|
|
39
|
+
@buf = "{"
|
|
40
|
+
@first = true
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Append `"key":"escaped-value"`.
|
|
44
|
+
def add_str(key, value)
|
|
45
|
+
comma
|
|
46
|
+
@buf = @buf + Builder.quote(key) + ":" + Builder.quote(value)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Append `"key":<number>` -- `value.to_s` covers Integer ("5") and
|
|
50
|
+
# Float ("1.5"). For hardcoded literals prefer add_raw.
|
|
51
|
+
def add_num(key, value)
|
|
52
|
+
comma
|
|
53
|
+
@buf = @buf + Builder.quote(key) + ":" + value.to_s
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# Append `"key":true|false`.
|
|
57
|
+
def add_bool(key, value)
|
|
58
|
+
comma
|
|
59
|
+
@buf = @buf + Builder.quote(key) + ":" + (value ? "true" : "false")
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Append `"key":<already-encoded JSON>` -- for arrays / numeric literals.
|
|
63
|
+
def add_raw(key, raw)
|
|
64
|
+
comma
|
|
65
|
+
@buf = @buf + Builder.quote(key) + ":" + raw
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Append `"key":<nested object>` from another Builder.
|
|
69
|
+
def add_obj(key, child)
|
|
70
|
+
comma
|
|
71
|
+
@buf = @buf + Builder.quote(key) + ":" + child.dump
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# Close the object and return the JSON string.
|
|
75
|
+
def dump
|
|
76
|
+
@buf + "}"
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# Emit a separator before every entry except the first.
|
|
80
|
+
def comma
|
|
81
|
+
if @first
|
|
82
|
+
@first = false
|
|
83
|
+
else
|
|
84
|
+
@buf = @buf + ","
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# ---- self-contained string escaping (byte-identical to
|
|
89
|
+
# SpinelKit::Json.escape/quote/hex2; kept local so a builder-only
|
|
90
|
+
# compile never pulls in the codec) ----
|
|
91
|
+
|
|
92
|
+
# Wrap a string in JSON quotes, escaping its body.
|
|
93
|
+
def self.quote(s)
|
|
94
|
+
"\"" + Builder.escape(s) + "\""
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# Escape a string for inclusion inside a JSON string literal (no
|
|
98
|
+
# surrounding quotes). Handles ", \, and the JSON control-char escapes
|
|
99
|
+
# (\b \f \n \r \t); other control bytes go through \u00XX. ASCII-clean
|
|
100
|
+
# input passes through unchanged.
|
|
101
|
+
def self.escape(s)
|
|
102
|
+
out = ""
|
|
103
|
+
i = 0
|
|
104
|
+
n = s.length
|
|
105
|
+
while i < n
|
|
106
|
+
c = s[i]
|
|
107
|
+
if c == "\""
|
|
108
|
+
out = out + "\\\""
|
|
109
|
+
elsif c == "\\"
|
|
110
|
+
out = out + "\\\\"
|
|
111
|
+
elsif c == "\n"
|
|
112
|
+
out = out + "\\n"
|
|
113
|
+
elsif c == "\r"
|
|
114
|
+
out = out + "\\r"
|
|
115
|
+
elsif c == "\t"
|
|
116
|
+
out = out + "\\t"
|
|
117
|
+
elsif c == "\b"
|
|
118
|
+
out = out + "\\b"
|
|
119
|
+
elsif c == "\f"
|
|
120
|
+
out = out + "\\f"
|
|
121
|
+
elsif c < " "
|
|
122
|
+
b = c.getbyte(0)
|
|
123
|
+
out = out + "\\u00" + Builder.hex2(b)
|
|
124
|
+
else
|
|
125
|
+
out = out + c
|
|
126
|
+
end
|
|
127
|
+
i += 1
|
|
128
|
+
end
|
|
129
|
+
out
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
# Two-digit lowercase hex of a byte (0..255).
|
|
133
|
+
def self.hex2(n)
|
|
134
|
+
hex = "0123456789abcdef"
|
|
135
|
+
out = ""
|
|
136
|
+
out = out + hex[(n / 16) % 16, 1]
|
|
137
|
+
out = out + hex[n % 16, 1]
|
|
138
|
+
out
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
end
|
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
# SpinelKit::Json -- Spinel-safe JSON DECODERS (flat-key, top-level only).
|
|
2
|
+
#
|
|
3
|
+
# The decode half of the codec; encoders are in spinel_kit/json.rb. Split out
|
|
4
|
+
# so an encode-only consumer never compiles these walkers (their dead-code
|
|
5
|
+
# degradation otherwise widens the encoders' string args to int -- see the
|
|
6
|
+
# header of json.rb and docs/spinel-discipline.md).
|
|
7
|
+
#
|
|
8
|
+
# `get_str(s, key)` finds the entry for `key` in the top-level object literal
|
|
9
|
+
# `s` and returns its value as a string. Returns "" when `key` is absent or
|
|
10
|
+
# the value isn't a string. Same shape for `get_int`. `has_key?(s, key)`
|
|
11
|
+
# returns a boolean independent of value type. The parser is a hand-rolled
|
|
12
|
+
# state machine that walks one `{ "k": <value>, ... }` pair at a time,
|
|
13
|
+
# skipping over any value (including nested objects / arrays) it doesn't need.
|
|
14
|
+
# Strings inside values are honoured for escape sequences so that `\"` doesn't
|
|
15
|
+
# terminate the string and corrupt the walk. Decodes the escape sequences
|
|
16
|
+
# `SpinelKit::Json.escape` produces.
|
|
17
|
+
module SpinelKit
|
|
18
|
+
class Json
|
|
19
|
+
def self.get_str(s, key)
|
|
20
|
+
pos = Json.find_value_start(s, key)
|
|
21
|
+
if pos < 0
|
|
22
|
+
return ""
|
|
23
|
+
end
|
|
24
|
+
Json.parse_str_value(s, pos)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def self.get_int(s, key)
|
|
28
|
+
pos = Json.find_value_start(s, key)
|
|
29
|
+
if pos < 0
|
|
30
|
+
return 0
|
|
31
|
+
end
|
|
32
|
+
Json.parse_int_value(s, pos)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Decode a JSON number value at `key` -> Float. Accepts both
|
|
36
|
+
# integer-literal (`42`) and float-literal (`3.14`, `-0.5`, `1e2`)
|
|
37
|
+
# JSON-number syntax; the integer form returns N.0. Missing key or
|
|
38
|
+
# malformed value returns 0.0 (consistent with the other getters'
|
|
39
|
+
# missing-key defaults).
|
|
40
|
+
#
|
|
41
|
+
# Implementation: delegates the value-span walking to skip_value (already
|
|
42
|
+
# handles all JSON-number syntax + structural-char boundaries), then
|
|
43
|
+
# String#to_f on the substring. Inlined rather than factored into a
|
|
44
|
+
# parse_float_value helper because spinel's type inference mis-widens `s`
|
|
45
|
+
# to int through the indirection. NOTE: that is a value-walk indirection
|
|
46
|
+
# concern, NOT the name-collision bug (which was fixed) -- keep it inlined.
|
|
47
|
+
def self.get_float(s, key)
|
|
48
|
+
pos = Json.find_value_start(s, key)
|
|
49
|
+
if pos < 0
|
|
50
|
+
return 0.0
|
|
51
|
+
end
|
|
52
|
+
pos = Json.skip_ws(s, pos)
|
|
53
|
+
if pos >= s.length
|
|
54
|
+
return 0.0
|
|
55
|
+
end
|
|
56
|
+
end_pos = Json.skip_value(s, pos)
|
|
57
|
+
if end_pos <= pos
|
|
58
|
+
return 0.0
|
|
59
|
+
end
|
|
60
|
+
s[pos, end_pos - pos].to_f
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def self.has_key?(s, key)
|
|
64
|
+
Json.find_value_start(s, key) >= 0
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# Decode a flat JSON array of integers at `key` -> Array[Integer].
|
|
68
|
+
# A missing or non-array value yields [] (the typed-empty-array idiom);
|
|
69
|
+
# non-int elements are skipped.
|
|
70
|
+
def self.get_int_array(s, key)
|
|
71
|
+
out = [0]
|
|
72
|
+
out.delete_at(0)
|
|
73
|
+
pos = Json.find_value_start(s, key)
|
|
74
|
+
if pos < 0
|
|
75
|
+
return out
|
|
76
|
+
end
|
|
77
|
+
pos = Json.skip_ws(s, pos)
|
|
78
|
+
if pos >= s.length || s[pos] != "["
|
|
79
|
+
return out
|
|
80
|
+
end
|
|
81
|
+
pos += 1
|
|
82
|
+
while pos < s.length
|
|
83
|
+
pos = Json.skip_ws(s, pos)
|
|
84
|
+
if pos >= s.length
|
|
85
|
+
return out
|
|
86
|
+
end
|
|
87
|
+
c = s[pos]
|
|
88
|
+
if c == "]"
|
|
89
|
+
return out
|
|
90
|
+
elsif c == ","
|
|
91
|
+
pos += 1
|
|
92
|
+
elsif (c >= "0" && c <= "9") || c == "-"
|
|
93
|
+
out.push(Json.parse_int_value(s, pos))
|
|
94
|
+
# Advance past the number parse_int_value just consumed
|
|
95
|
+
# (optional '-' then digits).
|
|
96
|
+
if s[pos] == "-"
|
|
97
|
+
pos += 1
|
|
98
|
+
end
|
|
99
|
+
while pos < s.length && s[pos] >= "0" && s[pos] <= "9"
|
|
100
|
+
pos += 1
|
|
101
|
+
end
|
|
102
|
+
else
|
|
103
|
+
# Non-int element (string / object / etc.): skip it.
|
|
104
|
+
pos = Json.skip_value(s, pos)
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
out
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# ---- Internal helpers ----
|
|
111
|
+
|
|
112
|
+
# Skip whitespace starting at `pos`, return the new position.
|
|
113
|
+
def self.skip_ws(s, pos)
|
|
114
|
+
while pos < s.length
|
|
115
|
+
c = s[pos]
|
|
116
|
+
if c == " " || c == "\t" || c == "\n" || c == "\r"
|
|
117
|
+
pos += 1
|
|
118
|
+
else
|
|
119
|
+
return pos
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
pos
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# Walk a JSON-quoted string starting at `pos` (which must point at the
|
|
126
|
+
# opening `"`). Returns the position one past the closing `"`. Returns
|
|
127
|
+
# -1 on malformed input.
|
|
128
|
+
def self.skip_str(s, pos)
|
|
129
|
+
if pos >= s.length || s[pos] != "\""
|
|
130
|
+
return -1
|
|
131
|
+
end
|
|
132
|
+
pos += 1
|
|
133
|
+
while pos < s.length
|
|
134
|
+
c = s[pos]
|
|
135
|
+
if c == "\\"
|
|
136
|
+
# Skip the escape and the escaped character. \uXXXX spans 6
|
|
137
|
+
# chars total but skipping 2 still keeps us inside the string
|
|
138
|
+
# for the rest of the walk -- the remaining 4 hex digits look
|
|
139
|
+
# like ordinary string bytes and won't terminate the literal.
|
|
140
|
+
pos += 2
|
|
141
|
+
elsif c == "\""
|
|
142
|
+
return pos + 1
|
|
143
|
+
else
|
|
144
|
+
pos += 1
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
-1
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
# Walk a JSON value starting at `pos` (which must point at the first
|
|
151
|
+
# non-ws char of the value). Returns the position one past the value
|
|
152
|
+
# (or the input length on truncation).
|
|
153
|
+
def self.skip_value(s, pos)
|
|
154
|
+
pos = Json.skip_ws(s, pos)
|
|
155
|
+
if pos >= s.length
|
|
156
|
+
return pos
|
|
157
|
+
end
|
|
158
|
+
c = s[pos]
|
|
159
|
+
if c == "\""
|
|
160
|
+
return Json.skip_str(s, pos)
|
|
161
|
+
end
|
|
162
|
+
if c == "{" || c == "["
|
|
163
|
+
return Json.skip_container(s, pos)
|
|
164
|
+
end
|
|
165
|
+
# number / true / false / null -- read until the next structural /
|
|
166
|
+
# whitespace char.
|
|
167
|
+
while pos < s.length
|
|
168
|
+
c = s[pos]
|
|
169
|
+
if c == "," || c == "}" || c == "]" ||
|
|
170
|
+
c == " " || c == "\t" || c == "\n" || c == "\r"
|
|
171
|
+
return pos
|
|
172
|
+
end
|
|
173
|
+
pos += 1
|
|
174
|
+
end
|
|
175
|
+
pos
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
# Walk a balanced { ... } or [ ... ] starting at `pos`. Honours string
|
|
179
|
+
# literals so that `{` / `}` inside a value-string don't confuse the
|
|
180
|
+
# brace counter. Returns position one past the matching closer.
|
|
181
|
+
def self.skip_container(s, pos)
|
|
182
|
+
open_c = s[pos]
|
|
183
|
+
close_c = open_c == "{" ? "}" : "]"
|
|
184
|
+
depth = 1
|
|
185
|
+
pos += 1
|
|
186
|
+
while pos < s.length && depth > 0
|
|
187
|
+
c = s[pos]
|
|
188
|
+
if c == "\""
|
|
189
|
+
# whole nested string -- skip past it
|
|
190
|
+
npos = Json.skip_str(s, pos)
|
|
191
|
+
if npos < 0
|
|
192
|
+
return s.length
|
|
193
|
+
end
|
|
194
|
+
pos = npos
|
|
195
|
+
elsif c == open_c
|
|
196
|
+
depth += 1
|
|
197
|
+
pos += 1
|
|
198
|
+
elsif c == close_c
|
|
199
|
+
depth -= 1
|
|
200
|
+
pos += 1
|
|
201
|
+
else
|
|
202
|
+
pos += 1
|
|
203
|
+
end
|
|
204
|
+
end
|
|
205
|
+
pos
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
# Read a JSON-quoted string at `pos` and return its decoded contents
|
|
209
|
+
# (no surrounding quotes). Decodes the same escape sequences that
|
|
210
|
+
# `escape` produces. Returns "" on malformed input.
|
|
211
|
+
def self.parse_str_value(s, pos)
|
|
212
|
+
pos = Json.skip_ws(s, pos)
|
|
213
|
+
if pos >= s.length || s[pos] != "\""
|
|
214
|
+
return ""
|
|
215
|
+
end
|
|
216
|
+
pos += 1
|
|
217
|
+
out = ""
|
|
218
|
+
while pos < s.length
|
|
219
|
+
c = s[pos]
|
|
220
|
+
if c == "\""
|
|
221
|
+
return out
|
|
222
|
+
end
|
|
223
|
+
if c == "\\"
|
|
224
|
+
if pos + 1 >= s.length
|
|
225
|
+
return out
|
|
226
|
+
end
|
|
227
|
+
esc = s[pos + 1]
|
|
228
|
+
if esc == "\""
|
|
229
|
+
out = out + "\""
|
|
230
|
+
elsif esc == "\\"
|
|
231
|
+
out = out + "\\"
|
|
232
|
+
elsif esc == "/"
|
|
233
|
+
out = out + "/"
|
|
234
|
+
elsif esc == "n"
|
|
235
|
+
out = out + "\n"
|
|
236
|
+
elsif esc == "r"
|
|
237
|
+
out = out + "\r"
|
|
238
|
+
elsif esc == "t"
|
|
239
|
+
out = out + "\t"
|
|
240
|
+
elsif esc == "b"
|
|
241
|
+
out = out + "\b"
|
|
242
|
+
elsif esc == "f"
|
|
243
|
+
out = out + "\f"
|
|
244
|
+
elsif esc == "u"
|
|
245
|
+
# \u00XX -> map the two-digit hex back to a byte. Wider
|
|
246
|
+
# codepoints (U+0100+ or surrogate pairs) aren't decoded; the
|
|
247
|
+
# byte we emit is the low byte of the codepoint, which
|
|
248
|
+
# round-trips ASCII at minimum.
|
|
249
|
+
if pos + 5 < s.length
|
|
250
|
+
h1 = Json.hex_nibble(s[pos + 4])
|
|
251
|
+
h2 = Json.hex_nibble(s[pos + 5])
|
|
252
|
+
if h1 >= 0 && h2 >= 0
|
|
253
|
+
# rebuild the byte and push it -- spinel strings are
|
|
254
|
+
# byte-blobs, so this works for ASCII; for non-ASCII the
|
|
255
|
+
# original encoder would have used a passthrough byte
|
|
256
|
+
# anyway.
|
|
257
|
+
b = h1 * 16 + h2
|
|
258
|
+
out = out + Json.byte_to_chr(b)
|
|
259
|
+
pos += 6
|
|
260
|
+
next
|
|
261
|
+
end
|
|
262
|
+
end
|
|
263
|
+
out = out + "?"
|
|
264
|
+
pos += 2
|
|
265
|
+
next
|
|
266
|
+
else
|
|
267
|
+
out = out + esc
|
|
268
|
+
end
|
|
269
|
+
pos += 2
|
|
270
|
+
else
|
|
271
|
+
out = out + c
|
|
272
|
+
pos += 1
|
|
273
|
+
end
|
|
274
|
+
end
|
|
275
|
+
out
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
def self.hex_nibble(c)
|
|
279
|
+
if c >= "0" && c <= "9"
|
|
280
|
+
return c.getbyte(0) - "0".getbyte(0)
|
|
281
|
+
end
|
|
282
|
+
if c >= "a" && c <= "f"
|
|
283
|
+
return c.getbyte(0) - "a".getbyte(0) + 10
|
|
284
|
+
end
|
|
285
|
+
if c >= "A" && c <= "F"
|
|
286
|
+
return c.getbyte(0) - "A".getbyte(0) + 10
|
|
287
|
+
end
|
|
288
|
+
-1
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
# Build a single-byte string from an integer 0..255. Spinel doesn't
|
|
292
|
+
# expose `n.chr` for arbitrary bytes uniformly; the table covers the
|
|
293
|
+
# ASCII printable range and falls back to "?" for anything else (the
|
|
294
|
+
# JSON encoder side never produces non-ASCII via \u, so the fallback
|
|
295
|
+
# is reachable only for malformed input).
|
|
296
|
+
def self.byte_to_chr(n)
|
|
297
|
+
printable = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
|
|
298
|
+
if n >= 32 && n < 127
|
|
299
|
+
return printable[n - 32, 1]
|
|
300
|
+
end
|
|
301
|
+
if n == 9
|
|
302
|
+
return "\t"
|
|
303
|
+
end
|
|
304
|
+
if n == 10
|
|
305
|
+
return "\n"
|
|
306
|
+
end
|
|
307
|
+
if n == 13
|
|
308
|
+
return "\r"
|
|
309
|
+
end
|
|
310
|
+
"?"
|
|
311
|
+
end
|
|
312
|
+
|
|
313
|
+
# Read an integer at `pos`. Accepts an optional leading `-`. Returns 0
|
|
314
|
+
# on no-digit / non-numeric input (caller can use `has_key?` first if
|
|
315
|
+
# 0-vs-absent matters).
|
|
316
|
+
def self.parse_int_value(s, pos)
|
|
317
|
+
pos = Json.skip_ws(s, pos)
|
|
318
|
+
if pos >= s.length
|
|
319
|
+
return 0
|
|
320
|
+
end
|
|
321
|
+
neg = false
|
|
322
|
+
if s[pos] == "-"
|
|
323
|
+
neg = true
|
|
324
|
+
pos += 1
|
|
325
|
+
end
|
|
326
|
+
n = 0
|
|
327
|
+
saw_digit = false
|
|
328
|
+
while pos < s.length
|
|
329
|
+
c = s[pos]
|
|
330
|
+
if c >= "0" && c <= "9"
|
|
331
|
+
n = n * 10 + (c.getbyte(0) - "0".getbyte(0))
|
|
332
|
+
saw_digit = true
|
|
333
|
+
pos += 1
|
|
334
|
+
else
|
|
335
|
+
break
|
|
336
|
+
end
|
|
337
|
+
end
|
|
338
|
+
if !saw_digit
|
|
339
|
+
return 0
|
|
340
|
+
end
|
|
341
|
+
neg ? -n : n
|
|
342
|
+
end
|
|
343
|
+
|
|
344
|
+
# Walk the top-level object looking for the entry whose key matches
|
|
345
|
+
# `target_key`; return the position of the value's first non-ws
|
|
346
|
+
# character. Returns -1 if not found.
|
|
347
|
+
def self.find_value_start(s, target_key)
|
|
348
|
+
pos = Json.skip_ws(s, 0)
|
|
349
|
+
if pos >= s.length || s[pos] != "{"
|
|
350
|
+
return -1
|
|
351
|
+
end
|
|
352
|
+
pos += 1
|
|
353
|
+
while pos < s.length
|
|
354
|
+
pos = Json.skip_ws(s, pos)
|
|
355
|
+
if pos >= s.length
|
|
356
|
+
return -1
|
|
357
|
+
end
|
|
358
|
+
if s[pos] == "}"
|
|
359
|
+
return -1
|
|
360
|
+
end
|
|
361
|
+
# Read a key.
|
|
362
|
+
if s[pos] != "\""
|
|
363
|
+
return -1
|
|
364
|
+
end
|
|
365
|
+
key_start = pos
|
|
366
|
+
pos = Json.skip_str(s, pos)
|
|
367
|
+
if pos < 0
|
|
368
|
+
return -1
|
|
369
|
+
end
|
|
370
|
+
# Decode the key for comparison (handles \" inside keys).
|
|
371
|
+
key = Json.parse_str_value(s, key_start)
|
|
372
|
+
# Skip ws, ":".
|
|
373
|
+
pos = Json.skip_ws(s, pos)
|
|
374
|
+
if pos >= s.length || s[pos] != ":"
|
|
375
|
+
return -1
|
|
376
|
+
end
|
|
377
|
+
pos += 1
|
|
378
|
+
pos = Json.skip_ws(s, pos)
|
|
379
|
+
if key == target_key
|
|
380
|
+
return pos
|
|
381
|
+
end
|
|
382
|
+
# Skip the value, then the comma (if any).
|
|
383
|
+
pos = Json.skip_value(s, pos)
|
|
384
|
+
pos = Json.skip_ws(s, pos)
|
|
385
|
+
if pos < s.length && s[pos] == ","
|
|
386
|
+
pos += 1
|
|
387
|
+
elsif pos < s.length && s[pos] == "}"
|
|
388
|
+
return -1
|
|
389
|
+
end
|
|
390
|
+
end
|
|
391
|
+
-1
|
|
392
|
+
end
|
|
393
|
+
end
|
|
394
|
+
end
|