DefaultTranscoder

enyim edited this page Jul 12, 2011 · 10 revisions

DefaultTranscoder

The DefaultTranscoder uses binary serialization to store items in Memcached (Membase, Couchbase, etc.). Type information is stored in the 'Flags' member of the protocol header. (See Section 4.3 of the binary protocol definition.)

Data Storage

  • Null is stored with type flag 0 (Empty), and it has no data.
  • Byte arrays are stored as is. They also have a special type flag: 0xfa52.
  • Numbers are serialized using the System.BitConverter class. The serialized data will be the same long as the original (ie. Int32 -> 4 bytes), and in little endian byte order. This does not apply to decimals.
  • Strings are encoded into UTF-8 byte array
  • All other types are stored using the BinaryFormatter class. This requires the value to be marked as serializable.

Type flags

The type code is derived from the .NET TypeCode assigned to the input data using the following formula: 0x100 | (int)typeCode.

E.g.: a string value has the flag 0x112

	public enum TypeCode
	{
		//     A null reference.
		Empty = 0,
		//     A general type representing any reference or value type not explicitly represented
		//     by another TypeCode.
		Object = 1,
		//     A database null (column) value.
		DBNull = 2,
		//     A simple type representing Boolean values of true or false.
		Boolean = 3,
		//     An integral type representing unsigned 16-bit integers with values between
		//     0 and 65535. The set of possible values for the System.TypeCode.Char type
		//     corresponds to the Unicode character set.
		Char = 4,
		//     An integral type representing signed 8-bit integers with values between -128
		//     and 127.
		SByte = 5,
		//     An integral type representing unsigned 8-bit integers with values between
		//     0 and 255.
		Byte = 6,
		//     An integral type representing signed 16-bit integers with values between
		//     -32768 and 32767.
		Int16 = 7,
		//     An integral type representing unsigned 16-bit integers with values between
		//     0 and 65535.
		UInt16 = 8,
		//     An integral type representing signed 32-bit integers with values between
		//     -2147483648 and 2147483647.
		Int32 = 9,
		//     An integral type representing unsigned 32-bit integers with values between
		//     0 and 4294967295.
		UInt32 = 10,
		//     An integral type representing signed 64-bit integers with values between
		//     -9223372036854775808 and 9223372036854775807.
		Int64 = 11,
		//     An integral type representing unsigned 64-bit integers with values between
		//     0 and 18446744073709551615.
		UInt64 = 12,
		//     A floating point type representing values ranging from approximately 1.5
		//     x 10 -45 to 3.4 x 10 38 with a precision of 7 digits.
		Single = 13,
		//     A floating point type representing values ranging from approximately 5.0
		//     x 10 -324 to 1.7 x 10 308 with a precision of 15-16 digits.
		Double = 14,
		//     A simple type representing values ranging from 1.0 x 10 -28 to approximately
		//     7.9 x 10 28 with 28-29 significant digits.
		Decimal = 15,
		//     A type representing a date and time value.
		DateTime = 16,
		//     A sealed class type representing Unicode character strings.
		String = 18,
	}

Deserialization

To increase the compatibility with other clients, the DefaultTranscoder applies the followings during deserialization

  1. Flag == RawDataFlag? Returns the data as it is.
  2. Flag == 0? If the server send any data, it will be treated as string. Otherwise it will be treated as null.
  3. Flag == Known TypeCode? Deserialize the data.
  4. Exception