Class: Travis::Yaml::Serializer::Json

Inherits:
Generic
  • Object
show all
Defined in:
lib/travis/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 Travis::Yaml::Serializer::Generic

Instance Method Details

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



54
55
56
57
# File 'lib/travis/yaml/serializer/json.rb', line 54

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

#lines(wrapper, lines) ⇒ Object



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

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)


13
14
15
# File 'lib/travis/yaml/serializer/json.rb', line 13

def pretty?
  !!options[:pretty]
end

#serialize_binary(value) ⇒ Object

Raises:

  • (NotSupportedError)


38
39
40
# File 'lib/travis/yaml/serializer/json.rb', line 38

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

#serialize_bool(value) ⇒ Object



42
43
44
# File 'lib/travis/yaml/serializer/json.rb', line 42

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

#serialize_decrypted(value) ⇒ Object



26
27
28
# File 'lib/travis/yaml/serializer/json.rb', line 26

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

#serialize_encrypted(value) ⇒ Object



22
23
24
# File 'lib/travis/yaml/serializer/json.rb', line 22

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

#serialize_float(value) ⇒ Object

Raises:

  • (NotSupportedError)


17
18
19
20
# File 'lib/travis/yaml/serializer/json.rb', line 17

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

#serialize_key(value) ⇒ Object



67
68
69
# File 'lib/travis/yaml/serializer/json.rb', line 67

def serialize_key(value)
  value.to_s
end

#serialize_mapping(node) ⇒ Object



46
47
48
# File 'lib/travis/yaml/serializer/json.rb', line 46

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

#serialize_sequence(node) ⇒ Object



50
51
52
# File 'lib/travis/yaml/serializer/json.rb', line 50

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

#serialize_str(value) ⇒ Object



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

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