Module: Protobuf::Message::Serialization

Included in:
Protobuf::Message
Defined in:
lib/protobuf/message/serialization.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#decode(bytes) ⇒ Object Also known as: parse_from_string, deserialize

Decode the given non-stream bytes into this message.



30
31
32
# File 'lib/protobuf/message/serialization.rb', line 30

def decode(bytes)
  decode_from(::StringIO.new(bytes))
end

#decode_from(stream) ⇒ Object Also known as: parse_from, deserialize_from

Decode the given stream into this message.



36
37
38
39
40
41
42
# File 'lib/protobuf/message/serialization.rb', line 36

def decode_from(stream)
  ::Protobuf::Decoder.decode_each_field(stream) do |tag, bytes|
    set_field_bytes(tag, bytes)
  end

  self
end

#encodeObject Also known as: to_s, bytes, serialize, serialize_to_string

Encode this message



46
47
48
49
# File 'lib/protobuf/message/serialization.rb', line 46

def encode
  stream = ::StringIO.new.set_encoding(::Protobuf::Field::BytesField::BYTES_ENCODING)
  encode_to(stream).string
end

#encode_to(stream) ⇒ Object Also known as: serialize_to

Encode this message to the given stream.



53
54
55
# File 'lib/protobuf/message/serialization.rb', line 53

def encode_to(stream)
  ::Protobuf::Encoder.encode(self, stream)
end