Encodes hashes to thrift messages and decodes thrift messages to hashes.
Add this line to your application's Gemfile:
gem "rt-thrift-serializer", :github => "renderedtext/thrift-serializer", :require => "thrift_serializer"user = User.new
user.name = "John Smith"
user.age = 42
ThriftSerializer.encode(user)
# => "\v\x00\x01\x00\x00\x00\nJohn Smith\b\x00\x02\x00\x00\x00*\x00"ThriftSerializer.decode(user, User.new)
# => { :name => "John Smith", :age => 32 }NOTE: Make sure that app using ThriftSerializer have compiled .thrift file
Hashes are validated against corresponding struct defined in .thrift file in app that use ThriftSerializer
Let your .thrift file have following struct:
struct User {
1: required string name
2: required i32 age
}Exception is raised in case:
- Required field is missing
user = User.new
user.name = "John Smith"
# Required field 'age' is missing
ThriftSerializer.encode(user)
# => raises Thrift::ProtocolException- If there are any extra fields
user = User.new
user.name = "John Smith"
user.age = 42
user.id = 101
# => raises NoMethodError- Invalid struct field type
user = User.new
user.name = "John Smith"
user.age = "42" # `age` is type of `i32`, got `string`
ThriftSerializer.encode(user)
# => raises ThriftSerializerError