Beispiel #1
0
        internal byte[] Serialize(object value)
        {
            if (value == Unset.Value)
            {
                if (!_protocolVersion.SupportsUnset())
                {
                    throw new InvalidTypeException("Unset is not supported by this Cassandra version");
                }
                //Return a buffer that represents the value Unset
                return(UnsetBuffer);
            }
            if (value == null)
            {
                return(null);
            }
            var             type = value.GetType();
            ITypeSerializer typeSerializer;

            if (_primitiveSerializers.TryGetValue(type, out typeSerializer))
            {
                return(typeSerializer.Serialize((byte)_protocolVersion, value));
            }
            if (_customSerializers.Count > 0 && _customSerializers.TryGetValue(type, out typeSerializer))
            {
                return(typeSerializer.Serialize((byte)_protocolVersion, value));
            }
            if (typeof(IEnumerable).GetTypeInfo().IsAssignableFrom(type))
            {
                if (typeof(IDictionary).GetTypeInfo().IsAssignableFrom(type))
                {
                    return(_dictionarySerializer.Serialize((byte)_protocolVersion, (IDictionary)value));
                }
                return(_collectionSerializer.Serialize((byte)_protocolVersion, (IEnumerable)value));
            }
            if (typeof(IStructuralComparable).GetTypeInfo().IsAssignableFrom(type) && type.FullName.StartsWith("System.Tuple"))
            {
                return(_tupleSerializer.Serialize((byte)_protocolVersion, (IStructuralEquatable)value));
            }
            var buffer = _udtSerializer.Serialize((byte)_protocolVersion, value);

            if (buffer != null)
            {
                return(buffer);
            }
            throw new InvalidTypeException("Unknown Cassandra target type for CLR type " + type.FullName);
        }