Skip to content

Value converters

SolalPirelli edited this page Oct 22, 2014 · 5 revisions

Thrift doesn't have many primitive types, which means some types of data have to be transmitted in less-than-ideal ways. However, it doesn't have to stay that way after transmission.

Enter value converters. They allows you to implement a converter from a Thrift type to another type. These converters can be applied to struct fields, method return types and method parameters using the ThriftConverterAttribute attribute like so:

// inside a class
[ThriftField( 1, true, "date" )]
[ThriftConverter( typeof( ThriftUnixDateConverter )]
public DateTime Date { get; private set; }

This example will serialize and deserialize the DateTime object as an Unix timestamp (i.e. the number of seconds since January 1, 1970).

Available converters

Converter name From type To type Description
ThriftUnixDateConverter i32 System.DateTime 32-bit Unix timestamp.
ThriftUnixDate64Converter i64 System.DateTime Same as above, in 64 bits.
ThriftJavaDateConverter i64 System.DateTime 64-bit Java timestamp.
ThriftUnixDateOffsetConverter i32 System.DateTimeOffset 32-bit Unix timestamp.
ThriftUnixLongDateOffsetConverter i64 System.DateTimeOffset Same as above, in 64 bits.
ThriftJavaDateOffsetConverter i64 System.DateTimeOffset 64-bit Java timestamp.

Implementing your own converter

Implement the ThriftValueConverter<TFrom, TTo> abstract class.
It contains two methods: TTo Convert( TFrom value ) and TFrom ConvertBack( TTo value ).

Clone this wiki locally