Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/rbor/array.rb
Instance Method Summary collapse
-
#cbor_deserialize ⇒ Object
Treat the object as CBOR ‘BINARY` data and deserialize it into a Ruby object.
-
#cbor_serialize(options = {}) ⇒ String
Serialize an Array into a CBOR array.
Instance Method Details
#cbor_deserialize ⇒ Object
Treat the object as CBOR ‘BINARY` data and deserialize it into a Ruby object.
Depending on the major type of the CBOR data, this method will return a object of one of the following types:
-
major type 0: Integer
-
major type 1: Integer
-
major type 2: String
-
major type 3: String
-
major type 4: Array
-
major type 5: Hash
-
major type 7: FalseClass, TrueClass, NilClass
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/rbor/array.rb', line 30 def cbor_deserialize raise "empty" if self.empty? case self[0] when 0x00..0x3b v = nil case self[0] & 0x1f when 0x00..0x17 v = self[0] & 0x1f, self[1..] when 0x18 v = self[1], self[2..] when 0x19 v = self[1..2].pack("C*").unpack("S>")[0], self[3..] when 0x1a v = self[1..4].pack("C*").unpack("L>")[0], self[5..] when 0x1b v = self[1..8].pack("C*").unpack("Q>")[0], self[9..] else raise "invalid additional information" end if self[0] & 1 << 5 != 0 v[0] = -v[0] - 1 end return v when 0x40..0x5b old = self[0] & 0b11100000 self[0] &= 0x1f len, rem = self.cbor_deserialize self[0] |= old return String.new(rem[0..len - 1].pack('C*'), encoding: "BINARY"), rem[len..] when 0x60..0x7b old = self[0] & 0b11100000 self[0] &= 0x1f len, rem = self.cbor_deserialize self[0] |= old return String.new(rem[0..len - 1].pack('C*'), encoding: "UTF-8"), rem[len..] when 0x80..0x9b old = self[0] & 0b11100000 self[0] &= 0x1f len, rem = self.cbor_deserialize self[0] |= old out = [] len.times do |n| obj, rem = rem.cbor_deserialize out.append obj end return out, rem when 0xa0..0xbb old = self[0] & 0b11100000 self[0] &= 0x1f len, rem = self.cbor_deserialize self[0] |= old out = {} len.times do |n| k, rem = rem.cbor_deserialize v, rem = rem.cbor_deserialize out[k] = v end return out, rem when 0xf4 return false, self[1..] when 0xf5 return true, self[1..] when 0xf6 return nil, self[1..] else raise "undefined major type" end end |
#cbor_serialize(options = {}) ⇒ String
Serialize an Array into a CBOR array.
7 8 9 10 11 12 13 14 15 |
# File 'lib/rbor/array.rb', line 7 def cbor_serialize( = {}) len = self.length.cbor_serialize.bytes len[0] |= 4 << 5 out = len.pack('C*') self.each do |elem| out += elem.cbor_serialize end return out end |