Class: CC::Yaml::Serializer::Json

Inherits:
Generic
  • Object
show all
Defined in:
lib/cc/yaml/serializer/json.rb

Constant Summary collapse

MAP =

mapping stolen from json gem

{ # mapping stolen from json gem
  "\x0"  => '\u0000', "\x1"  => '\u0001', "\x2"  => '\u0002', "\x3"  => '\u0003', "\x4"  => '\u0004', "\x5"  => '\u0005',
  "\x6"  => '\u0006', "\x7"  => '\u0007', "\b"   =>  '\b',    "\t"   =>  '\t',    "\n"   => '\n',     "\xb"  => '\u000b',
  "\f"   => '\f',     "\r"   =>  '\r',    "\xe"  => '\u000e', "\xf"  => '\u000f', "\x10" => '\u0010', "\x11" => '\u0011',
  "\x12" => '\u0012', "\x13" => '\u0013', "\x14" => '\u0014', "\x15" => '\u0015', "\x16" => '\u0016', "\x17" => '\u0017',
  "\x18" => '\u0018', "\x19" => '\u0019', "\x1a" => '\u001a', "\x1b" => '\u001b', "\x1c" => '\u001c', "\x1d" => '\u001d',
  "\x1e" => '\u001e', "\x1f" => '\u001f', '"'    =>  '\"',    '\\'   =>  '\\\\'
}

Instance Attribute Summary

Attributes inherited from Generic

#options

Instance Method Summary collapse

Methods inherited from Generic

#initialize, serialize, #serialize, #serialize_regexp, #serialize_root, #serialize_scalar, #serialize_secure, #serialize_time, #serialize_value, #symbol_keys?

Constructor Details

This class inherits a constructor from CC::Yaml::Serializer::Generic

Instance Method Details

#key_value(key, value, wrapper = "%s") ⇒ Object



55
56
57
58
# File 'lib/cc/yaml/serializer/json.rb', line 55

def key_value(key, value, wrapper = "%s")
  space = pretty? ? " " : ""
  wrapper % "#{serialize_str(key)}:#{space}#{value}"
end

#lines(wrapper, lines) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/cc/yaml/serializer/json.rb', line 60

def lines(wrapper, lines)
  return wrapper % lines.join(",") unless pretty?
  return wrapper % "" if lines.empty?
  return wrapper % " #{lines.first} " unless lines.size > 1 or  lines.first.include?("\n") or lines.first.size > 50
  lines = "\n  " + lines.join(",\n").strip.gsub("\n", "\n  ") + "\n"
  wrapper % lines
end

#pretty?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/cc/yaml/serializer/json.rb', line 14

def pretty?
  !!options[:pretty]
end

#serialize_binary(value) ⇒ Object

Raises:



39
40
41
# File 'lib/cc/yaml/serializer/json.rb', line 39

def serialize_binary(value)
  raise NotSupportedError, "cannot serialize binary data as JSON"
end

#serialize_bool(value) ⇒ Object



43
44
45
# File 'lib/cc/yaml/serializer/json.rb', line 43

def serialize_bool(value)
  value ? "true" : "false"
end

#serialize_decrypted(value) ⇒ Object



27
28
29
# File 'lib/cc/yaml/serializer/json.rb', line 27

def serialize_decrypted(value)
  serialize_str(value.decrypted_string)
end

#serialize_encrypted(value) ⇒ Object



23
24
25
# File 'lib/cc/yaml/serializer/json.rb', line 23

def serialize_encrypted(value)
  key_value("secure", serialize_str(value.encrypted_string), "{%s}")
end

#serialize_float(value) ⇒ Object

Raises:



18
19
20
21
# File 'lib/cc/yaml/serializer/json.rb', line 18

def serialize_float(value)
  raise NotSupportedError, "cannot serialize infinity as JSON" if value.infinite?
  "#{value}"
end

#serialize_key(value) ⇒ Object



68
69
70
# File 'lib/cc/yaml/serializer/json.rb', line 68

def serialize_key(value)
  value.to_s
end

#serialize_mapping(node) ⇒ Object



47
48
49
# File 'lib/cc/yaml/serializer/json.rb', line 47

def serialize_mapping(node)
  lines("{%s}", super.map { |key, value| key_value(key, value) })
end

#serialize_sequence(node) ⇒ Object



51
52
53
# File 'lib/cc/yaml/serializer/json.rb', line 51

def serialize_sequence(node)
  lines("[%s]", super)
end

#serialize_str(value) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/cc/yaml/serializer/json.rb', line 31

def serialize_str(value)
  string = value.encode("utf-8")
  string.force_encoding("binary")
  string.gsub!(/["\\\x0-\x1f]/) { MAP[$&] }
  string.force_encoding("utf-8")
  "\"#{string}\""
end