コード例 #1
0
        private static Dictionary <string, dynamic> GetDefinitionByType(
            Dictionary <string, dynamic> dictionary, IGenericSerializer genericSerializer, ColumnTypeCode typeCode, IColumnInfo typeInfo)
        {
            if (typeInfo is UdtColumnInfo udtTypeInfo)
            {
                var udtMap = genericSerializer.GetUdtMapByName(udtTypeInfo.Name);
                if (udtMap == null)
                {
                    throw new InvalidOperationException($"Could not find UDT mapping for {udtTypeInfo.Name}");
                }
                return(GetUdtTypeDefinition(dictionary, genericSerializer.GetUdtMapByName(udtTypeInfo.Name), genericSerializer));
            }

            dictionary.Add("cqlType", typeCode.ToString().ToLower());

            if (typeInfo is TupleColumnInfo tupleColumnInfo)
            {
                dictionary.Add(
                    "definition",
                    tupleColumnInfo.Elements.Select(c => GetDefinitionByType(genericSerializer, c.TypeCode, c.TypeInfo)));
            }

            if (typeInfo is MapColumnInfo mapColumnInfo)
            {
                dictionary.Add(
                    "definition",
                    new[] {
                    GetDefinitionByType(genericSerializer, mapColumnInfo.KeyTypeCode, mapColumnInfo.KeyTypeInfo),
                    GetDefinitionByType(genericSerializer, mapColumnInfo.ValueTypeCode, mapColumnInfo.ValueTypeInfo)
                });
            }

            if (typeInfo is ListColumnInfo listColumnInfo)
            {
                dictionary.Add(
                    "definition",
                    new[] { GetDefinitionByType(
                                genericSerializer, listColumnInfo.ValueTypeCode, listColumnInfo.ValueTypeInfo) });
            }

            if (typeInfo is SetColumnInfo setColumnInfo)
            {
                dictionary.Add(
                    "definition",
                    new[] { GetDefinitionByType(
                                genericSerializer, setColumnInfo.KeyTypeCode, setColumnInfo.KeyTypeInfo) });
            }

            return(dictionary);
        }
コード例 #2
0
        /// <inheritdoc />
        public dynamic Objectify(
            JToken graphsonObject, Type type, IGraphTypeSerializer serializer, IGenericSerializer genericSerializer)
        {
            var keyspace = serializer.FromDb <string>(graphsonObject["keyspace"]);
            var name     = serializer.FromDb <string>(graphsonObject["name"]);
            var values   = (JArray)graphsonObject["value"];

            var  targetTypeIsDictionary = false;
            Type elementType            = null;

            if (type.GetTypeInfo().IsGenericType &&
                (type.GetGenericTypeDefinition() == typeof(IReadOnlyDictionary <,>) ||
                 type.GetGenericTypeDefinition() == typeof(Dictionary <,>) ||
                 type.GetGenericTypeDefinition() == typeof(IDictionary <,>)))
            {
                targetTypeIsDictionary = true;
                var genericArgs = type.GetTypeInfo().GetGenericArguments();
                if (genericArgs[0] != typeof(string))
                {
                    throw new InvalidOperationException(
                              "Deserializing UDT to Dictionary is only supported when the dictionary key is of type \"string\".");
                }
                elementType = genericArgs[1];
            }

            UdtMap udtMap = null;
            bool   readToDictionary;

            if (targetTypeIsDictionary)
            {
                readToDictionary = true;
            }
            else
            {
                udtMap = genericSerializer.GetUdtMapByName($"{keyspace}.{name}");
                if (udtMap != null)
                {
                    readToDictionary = false;
                }
                else
                {
                    readToDictionary = true;
                    elementType      = typeof(object);
                }
            }

            var obj = readToDictionary
                ? ToDictionary(serializer, elementType, (JArray)graphsonObject["definition"], values)
                : ToObject(serializer, udtMap, values);

            if (!serializer.ConvertFromDb(obj, type, out var result))
            {
                throw new InvalidOperationException($"Could not convert UDT from type {obj.GetType().FullName} to {type.FullName}");
            }

            return(result);
        }
コード例 #3
0
ファイル: Serializer.cs プロジェクト: uzbekdev1/csharp-driver
 public UdtMap GetUdtMapByName(string name)
 {
     return(_serializer.GetUdtMapByName(name));
 }