Module: Fusion::Serializer
- Defined in:
- lib/fusion.rb
Overview
JSON I/O (minimal, with NULL and ErrorVal handling)
Class Method Summary collapse
Class Method Details
.string_json(s) ⇒ Object
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 |
# File 'lib/fusion.rb', line 1104 def self.string_json(s) out = +'"' s.each_char do |c| out << case c when '"' then '\\"' when "\\" then "\\\\" when "\n" then "\\n" when "\t" then "\\t" when "\r" then "\\r" else c end end out << '"' out end |
.to_json(v) ⇒ Object
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 |
# File 'lib/fusion.rb', line 1084 def self.to_json(v) case v when NULL then "null" when true then "true" when false then "false" when Integer then v.to_s when Float then v.to_s when String then string_json(v) when Array then "[" + v.map { |x| to_json(x) }.join(",") + "]" when Hash then "{" + v.map { |k, x| "#{string_json(k.to_s)}:#{to_json(x)}" }.join(",") + "}" when Func, NativeFunc then '"<function>"' when ErrorVal # Errors render as `!<payload-json>`. This form is NOT valid JSON; the CLI # prints the payload (as JSON) to stderr and nothing to stdout on error. "!" + to_json(v.payload) else v.inspect end end |