Class: Nauvisian::Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/nauvisian/serializer.rb

Instance Method Summary collapse

Constructor Details

#initialize(stream) ⇒ Serializer

Returns a new instance of Serializer.

Raises:

  • (ArgumentError)


5
6
7
8
9
# File 'lib/nauvisian/serializer.rb', line 5

def initialize(stream)
  raise ArgumentError, "can't read from the given argument" unless stream.respond_to?(:write)

  @stream = stream
end

Instance Method Details

#write_bool(bool) ⇒ Object

def read_u16_tuple(length) = Array.new(length) { read_u16 } def read_optim_tuple(bit_size, length) = Array.new(length) { read_optim(bit_size) }



45
# File 'lib/nauvisian/serializer.rb', line 45

def write_bool(bool) = write_u8(bool ? 0x01 : 0x00)

#write_bytes(data) ⇒ Object

Raises:

  • (ArgumentError)


11
12
13
14
15
16
# File 'lib/nauvisian/serializer.rb', line 11

def write_bytes(data)
  raise ArgumentError if data.nil?
  return if data.empty?

  @stream.write(data)
end

#write_dictionary(dict) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/nauvisian/serializer.rb', line 76

def write_dictionary(dict)
  write_u32(dict.size)
  dict.each do |(key, value)|
    write_str_property(key)
    write_property_tree(value)
  end
end

#write_double(dbl) ⇒ Object



63
# File 'lib/nauvisian/serializer.rb', line 63

def write_double(dbl) = write_bytes([dbl].pack("d"))

#write_list(list) ⇒ Object



70
71
72
73
# File 'lib/nauvisian/serializer.rb', line 70

def write_list(list)
  write_optim_u32(list.size)
  list.each {|e| write_property_tree(e) }
end

#write_optim_u16(uint16) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/nauvisian/serializer.rb', line 23

def write_optim_u16(uint16)
  if uint16 < 0xFF
    write_u8(uint16 & 0xFF)
  else
    write_u8(0xFF)
    write_u16(uint16)
  end
end

#write_optim_u32(uint32) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/nauvisian/serializer.rb', line 33

def write_optim_u32(uint32)
  if uint32 < 0xFF
    write_u8(uint32 & 0xFF)
  else
    write_u8(0xFF)
    write_u32(uint32)
  end
end

#write_property_tree(obj) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/nauvisian/serializer.rb', line 84

def write_property_tree(obj)
  case obj
  in true | false => bool
    write_u8(1)
    write_bool(false)
    write_bool(bool)
  in Float => dbl
    write_u8(2)
    write_bool(false)
    write_double(dbl)
  in String => str
    write_u8(3)
    write_bool(false)
    write_str_property(str)
  in Array => list
    write_u8(4)
    write_bool(false)
    write_list(list)
  in Hash => dict
    write_u8(5)
    write_bool(false)
    write_dictionary(dict)
  else
    raise Nauvisian::UnknownPropertyType, obj.class
  end
end

#write_str(str) ⇒ Object



47
48
49
50
# File 'lib/nauvisian/serializer.rb', line 47

def write_str(str)
  write_optim_u32(str.length)
  write_bytes(str.b)
end

#write_str_property(str) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/nauvisian/serializer.rb', line 53

def write_str_property(str)
  if str.empty?
    write_bool(true)
  else
    write_bool(false)
    write_str(str)
  end
end

#write_u16(uint16) ⇒ Object



19
# File 'lib/nauvisian/serializer.rb', line 19

def write_u16(uint16) = write_bytes([uint16].pack("v"))

#write_u32(uint32) ⇒ Object



20
# File 'lib/nauvisian/serializer.rb', line 20

def write_u32(uint32) = write_bytes([uint32].pack("V"))

#write_u8(uint8) ⇒ Object



18
# File 'lib/nauvisian/serializer.rb', line 18

def write_u8(uint8) = write_bytes([uint8].pack("C"))

#write_version24(v24) ⇒ Object



67
# File 'lib/nauvisian/serializer.rb', line 67

def write_version24(v24) = v24.to_a.each {|u16| write_optim_u16(u16) }

#write_version64(v64) ⇒ Object



65
# File 'lib/nauvisian/serializer.rb', line 65

def write_version64(v64) = v64.to_a.each {|u16| write_u16(u16) }