Module: OkJson
Overview
Some parts adapted from golang.org/src/encoding/json/decode.go and golang.org/src/unicode/utf8/utf8.go
Defined Under Namespace
Constant Summary collapse
- Upstream =
'45'.freeze
Instance Method Summary collapse
-
#decode(s) ⇒ Object
Decodes a json document in string s and returns the corresponding ruby value.
-
#encode(x) ⇒ Object
Encodes x into a json text.
- #valenc(x) ⇒ Object
Instance Method Details
#decode(s) ⇒ Object
Decodes a json document in string s and returns the corresponding ruby value. String s must be valid UTF-8. If you have a string in some other encoding, convert it first.
String values in the resulting structure will be UTF-8.
42 43 44 45 46 47 |
# File 'lib/puppet/jenkins/okjson.rb', line 42 def decode(s) ts = lex(s) v, ts = textparse(ts) raise Error, 'trailing garbage' unless ts.empty? v end |
#encode(x) ⇒ Object
Encodes x into a json text. It may contain only Array, Hash, String, Numeric, true, false, nil. (Note, this list excludes Symbol.) X itself must be an Array or a Hash. No other value can be encoded, and an error will be raised if x contains any other value, such as Nan, Infinity, Symbol, and Proc, or if a Hash key is not a String. Strings contained in x must be valid UTF-8.
58 59 60 61 62 63 64 65 |
# File 'lib/puppet/jenkins/okjson.rb', line 58 def encode(x) case x when Hash then objenc(x) when Array then arrenc(x) else raise Error, 'root value must be an Array or a Hash' end end |
#valenc(x) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/puppet/jenkins/okjson.rb', line 67 def valenc(x) case x when Hash then objenc(x) when Array then arrenc(x) when String then strenc(x) when Numeric then numenc(x) when true then 'true' when false then 'false' when nil then 'null' else raise Error, "cannot encode #{x.class}: #{x.inspect}" end end |