Module: Raven::OkJson

Extended by:
OkJson
Included in:
OkJson
Defined in:
lib/raven/okjson.rb

Defined Under Namespace

Classes: Error, Utf8Error

Constant Summary collapse

Upstream =
'42'

Instance Method Summary collapse

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.



45
46
47
48
49
50
51
52
# File 'lib/raven/okjson.rb', line 45

def decode(s)
  ts = lex(s)
  v, ts = textparse(ts)
  if ts.length > 0
    raise Error, 'trailing garbage'
  end
  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.



64
65
66
67
68
69
70
71
72
# File 'lib/raven/okjson.rb', line 64

def encode(x)
  visited = []
  case x
  when Hash    then objenc(x, visited)
  when Array   then arrenc(x, visited)
  else
    raise Error, 'root value must be an Array or a Hash'
  end
end

#valenc(x, visited) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/raven/okjson.rb', line 75

def valenc(x, visited)
  case x
  when Hash    then objenc(x, visited)
  when Array   then arrenc(x, visited)
  when String  then strenc(x)
  when Symbol  then strenc(x.to_s)
  when Numeric then numenc(x)
  when true    then "true"
  when false   then "false"
  when nil     then "null"
  else
    strenc((x.inspect rescue $!.to_s))
  end
end