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.



34
35
36
# File 'lib/protobuf/message/serialization.rb', line 34

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.



40
41
42
43
44
45
46
# File 'lib/protobuf/message/serialization.rb', line 40

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



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

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

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

Encode this message to the given stream.



59
60
61
# File 'lib/protobuf/message/serialization.rb', line 59

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