Exemplo n.º 1
0
        TypeSerializationInfo GetTypeSerializationInfo(string OrleansTypeString, Type Type)
        {
            if (!typeInfoCache.TryGetValue(Type, out var typeInfo))
            {
                foreach (var providerInfo in options.SerializationProviders.Values)
                {
                    if (providerInfo.provider.IsSupportedType(Type))
                    {
                        typeInfo = new TypeSerializationInfo()
                        {
                            typeString   = providerInfo.provider.GetTypeString(OrleansTypeString, Type),
                            providerInfo = providerInfo
                        };
                        if (typeInfo.typeString.Contains("!"))
                        {
                            throw new ArgumentException($"TypeString returned from storage serialization providers may not contain the special character '!', got {typeInfo.typeString}");
                        }
                        break;
                    }
                }

                typeInfoCache.TryAdd(Type, typeInfo);
            }

            return(typeInfo);
        }
 static TypeSerializationInfo DoTypeRemap(TypeSerializationInfo info, Dictionary <TypeSerializationInfo, TypeSerializationInfo> remapper)
 {
     if (remapper.TryGetValue(info, out var foundInfo))
     {
         return(foundInfo);
     }
     return(info);
 }
        private static Type GetTypeFromSerializedString(TypeSerializationInfo typeInfo)
        {
            if (!typeInfo.IsValid())
            {
                return(null);
            }

            return(Type.GetType(typeInfo.SearchString()));
        }
Exemplo n.º 4
0
        private void WriteTypeInfo(TypeSerializationInfo typeInfo, string propName = "$type")
        {
            if (typeInfo == null)
            {
                return;
            }

            if (propName != null)
            {
                _jsonWriter.WritePropertyName(propName);
            }

            if (typeInfo == TypeSerializationInfo.Self)
            {
                _jsonWriter.WriteValue("$type");
            }
            else if (typeInfo.Assembly == null && typeInfo.GenericArgs == null)
            {
                _jsonWriter.WriteValue(typeInfo.Name);
            }
            else
            {
                _jsonWriter.WriteStartObject();

                _jsonWriter.WritePropertyName(nameof(TypeSerializationInfo.Name));
                _jsonWriter.WriteValue(typeInfo.Name);

                if (typeInfo.GenericArgs != null)
                {
                    _jsonWriter.WritePropertyName(nameof(TypeSerializationInfo.GenericArgs));
                    _jsonWriter.WriteStartArray();
                    foreach (var genericArgType in typeInfo.GenericArgs)
                    {
                        WriteTypeInfo(genericArgType, propName: null);
                    }
                    _jsonWriter.WriteEndArray();
                }

                if (typeInfo.Assembly != null)
                {
                    _jsonWriter.WritePropertyName(nameof(TypeSerializationInfo.Assembly));

                    if (typeInfo.Assembly.Token == null && typeInfo.Assembly.Version == null)
                    {
                        _jsonWriter.WriteValue(typeInfo.Assembly.Name);
                    }
                    else
                    {
                        _jsonSerializer.Serialize(_jsonWriter, typeInfo.Assembly);
                    }
                }

                _jsonWriter.WriteEndObject();
            }
        }
 private NamedEntityAttributes GetNamedEntityAttributesFrom(Type type)
 {
     AvroContractResolver resolver = this.settings.Resolver;
     TypeSerializationInfo typeInfo = resolver.ResolveType(type);
     var name = new SchemaName(typeInfo.Name, typeInfo.Namespace);
     var aliases = typeInfo
         .Aliases
         .Select(alias => string.IsNullOrEmpty(name.Namespace) || alias.Contains(".") ? alias : name.Namespace + "." + alias)
         .ToList();
     return new NamedEntityAttributes(name, aliases, typeInfo.Doc);
 }
Exemplo n.º 6
0
        public object Compose(IValueContainer container, Type valueType)
        {
            var c = (EntityProjectionContainer)container;
            var projetionInterface = _typeSerializerHelper.ResolveType(TypeSerializationInfo.Parse(c.Type));
            var result             = EntityProjection.CreateInstance(projetionInterface);

            if (c.Properties != null)
            {
                foreach (var pair in c.Properties)
                {
                    // BAD CODE
                    // The problem is in JSON serialization, so we have to perform some extra type checks and conversion.
                    // I know, it's bad, just need to make it work as a quick-fix without going back to the drawing board.
                    var value = pair.Value;
                    if (value != null)
                    {
                        var expectedValueType = projetionInterface.GetProperty(pair.Key).PropertyType;
                        var actualValueType   = value.GetType();
                        if (actualValueType != expectedValueType)
                        {
                            if ((expectedValueType == typeof(Guid) || expectedValueType == typeof(Guid?)) && actualValueType == typeof(string))
                            {
                                value = Guid.Parse((string)value);
                            }
                            else if (expectedValueType == typeof(Uri) && actualValueType == typeof(string))
                            {
                                value = new Uri((string)value);
                            }
                            else if (expectedValueType.IsEnum)
                            {
                                value = Enum.ToObject(expectedValueType, value);
                            }
                            else if (expectedValueType.IsGenericType && !expectedValueType.IsClass && expectedValueType.Name == "Nullable`1")
                            {
                                var nullableValueType = expectedValueType.GetGenericArguments()[0];
                                if (nullableValueType != actualValueType)
                                {
                                    value = Convert.ChangeType(value, nullableValueType);
                                }
                                value = Activator.CreateInstance(expectedValueType, value);
                            }
                            else
                            {
                                value = Convert.ChangeType(value, expectedValueType);
                            }
                        }
                    }
                    // BAD CODE

                    EntityProjection.SetValue(result, pair.Key, value);
                }
            }
            return(result);
        }
        public static T Deserialize <T>(JSONSerializedElement item, Dictionary <TypeSerializationInfo, TypeSerializationInfo> remapper, params object[] constructorArgs) where T : class
        {
            if (!item.typeInfo.IsValid() || string.IsNullOrEmpty(item.JSONnodeData))
            {
                throw new ArgumentException(string.Format("Can not deserialize {0}, it is invalid", item));
            }

            TypeSerializationInfo info = item.typeInfo;

            info.fullName = info.fullName.Replace("UnityEngine.MaterialGraph", "UnityEditor.ShaderGraph");
            info.fullName = info.fullName.Replace("UnityEngine.Graphing", "UnityEditor.Graphing");
            if (remapper != null)
            {
                info = DoTypeRemap(info, remapper);
            }

            var type = GetTypeFromSerializedString(info);

            if (type == null)
            {
                throw new ArgumentException(string.Format("Can not deserialize ({0}), type is invalid", info.fullName));
            }

            T instance;

            try
            {
                var culture = CultureInfo.CurrentCulture;
                var flags   = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
                instance = Activator.CreateInstance(type, flags, null, constructorArgs, culture) as T;
            }
            catch (Exception e)
            {
                throw new Exception(string.Format("Could not construct instance of: {0}", type), e);
            }

            if (instance != null)
            {
                JsonUtility.FromJsonOverwrite(item.JSONnodeData, instance);
                return(instance);
            }
            return(null);
        }
Exemplo n.º 8
0
        static Type GetTypeFromSerializedString(TypeSerializationInfo typeInfo)
        {
            if (!typeInfo.IsValid())
            {
                return(null);
            }

            var assemblies = AppDomain.CurrentDomain.GetAssemblies();

            foreach (var assembly in assemblies)
            {
                var type = assembly.GetType(typeInfo.fullName);
                if (type != null)
                {
                    return(type);
                }
            }

            return(null);
        }
        public static T Deserialize <T>(string data, TypeSerializationInfo typeInfo, Dictionary <TypeSerializationInfo, TypeSerializationInfo> remapper, params object[] constructorArgs) where T : class
        {
            if (!typeInfo.IsValid() || string.IsNullOrEmpty(data))
            {
                throw new ArgumentException($"Can not deserialize {data} of type {typeInfo}, it is invalid");
            }

            typeInfo.fullName = typeInfo.fullName.Replace("UnityEngine.MaterialGraph", "UnityEditor.ShaderGraph");
            typeInfo.fullName = typeInfo.fullName.Replace("UnityEngine.Graphing", "UnityEditor.Graphing");
            if (remapper != null)
            {
                typeInfo = DoTypeRemap(typeInfo, remapper);
            }

            var type = GetTypeFromSerializedString(typeInfo);

            if (type == null)
            {
                throw new ArgumentException($"Can not deserialize ({typeInfo.fullName}), type is invalid");
            }

            T instance;

            try
            {
                instance = Activator.CreateInstance(type, constructorArgs) as T;
            }
            catch (Exception e)
            {
                throw new Exception($"Could not construct instance of: {type}", e);
            }

            if (instance != null)
            {
                JsonUtility.FromJsonOverwrite(data, instance);
                return(instance);
            }
            return(null);
        }
Exemplo n.º 10
0
        public static void Deserialize <T>(IDictionary <int, T> dictionary, IEnumerable <JSONSerializedIndexedElement> list, Dictionary <TypeSerializationInfo, TypeSerializationInfo> remapper, params object[] constructorArgs) where T : class
        {
            foreach (var element in list)
            {
                try
                {
                    T existingElement;
                    if (dictionary.TryGetValue(element.index, out existingElement))
                    {
                        TypeSerializationInfo info = element.typeInfo;
                        var type = GetTypeFromSerializedString(info);
                        if (type == null)
                        {
                            throw new ArgumentException(string.Format("Can not deserialize ({0}), type is invalid", info.fullName));
                        }

                        if (!type.IsInstanceOfType(existingElement))
                        {
                            dictionary[element.index] = Deserialize <T>(element, remapper, constructorArgs);
                        }
                        else
                        {
                            JsonUtility.FromJsonOverwrite(element.JSONnodeData, existingElement);
                        }
                    }
                    else
                    {
                        dictionary.Add(element.index, Deserialize <T>(element, remapper));
                    }
                }
                catch (Exception e)
                {
                    Debug.LogException(e);
                    Debug.LogError(element.JSONnodeData);
                }
            }
        }
Exemplo n.º 11
0
        /// <summary>
        /// Is known type.
        /// </summary>
        private static bool IsKnownType(Type type)
        {
            TypeSerializationInfo info = KnownTypes.GetTypeSerializationInfo(type);

            return(info != null);
        }
Exemplo n.º 12
0
        private TypeSerializationInfo ConvertToTypeSerializationInfo(JToken typeObject)
        {
            if (typeObject.Type == JTokenType.String)
            {
                var typeName = (string)typeObject;

                if (typeName == "$type")
                {
                    return(TypeSerializationInfo.Self);
                }

                return(new TypeSerializationInfo
                {
                    Name = typeName
                });
            }
            else if (typeObject.Type == JTokenType.Object)
            {
                var result = new TypeSerializationInfo
                {
                    Name = typeObject.Value <string>(nameof(TypeSerializationInfo.Name))
                };

                var assemblyValue = typeObject[nameof(TypeSerializationInfo.Assembly)];
                if (assemblyValue != null)
                {
                    if (assemblyValue.Type == JTokenType.String)
                    {
                        result.Assembly = new AssemblySerializationInfo
                        {
                            Name = assemblyValue.Value <string>()
                        };
                    }
                    else if (assemblyValue.Type == JTokenType.Object)
                    {
                        result.Assembly = assemblyValue.ToObject <AssemblySerializationInfo>();
                    }
                    else
                    {
                        throw new Exception($"Invalid assembly token: '{assemblyValue.Type}'");
                    }
                }

                var genericArgsValues = typeObject[nameof(TypeSerializationInfo.GenericArgs)];
                if (genericArgsValues != null)
                {
                    if (genericArgsValues.Type == JTokenType.Array)
                    {
                        var genericArguments = new List <TypeSerializationInfo>();
                        foreach (var genericArgTypeValue in genericArgsValues)
                        {
                            var genericArgument = ConvertToTypeSerializationInfo(genericArgTypeValue);
                            genericArguments.Add(genericArgument);
                        }
                        result.GenericArgs = genericArguments.ToArray();
                    }
                    else
                    {
                        throw new Exception($"Invalid generic arguments token: '{genericArgsValues.Type}'");
                    }
                }

                return(result);
            }
            else
            {
                throw new Exception($"Invalid type info token: '{typeObject.Type}'");
            }
        }