Skip to content

Instantly share code, notes, and snippets.

@rjnienaber
Created December 18, 2014 08:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rjnienaber/bf43a677e374504799ab to your computer and use it in GitHub Desktop.
Save rjnienaber/bf43a677e374504799ab to your computer and use it in GitHub Desktop.
Example protocol buffer
require 'protocol_buffers'
class User < ProtocolBuffers::Message
required :string, :name, 1
required :string, :email, 2
optional :int32, :logins, 3
end
class Group < ProtocolBuffers::Message
repeated User, :users, 1
repeated Group, :subgroups, 2
module GroupType
include ProtocolBuffers::Enum
Study = 1
Play = 2
end
optional GroupType, :group_type, 3
end
user = User.new(name: 'Richard', email: 'haha@email.com', logins: 1)
group = Group.new(:group_type => Group::GroupType::Play)
group.users << user
serialized = group.serialize_to_string
# "\n\e\n\aRichard\x12\x0Ehaha@email.com\x18\x01\x18\x02"
puts "SERIALIZED: #{serialized.inspect}"
deserialized = Group.parse(serialized)
#<Group users=[#<User name="Richard" email="haha@email.com" logins=1>] subgroups=[] group_type=Play(2)>
puts "DESERIALIZED: #{deserialized.inspect}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment