Exemple #1
0
            public SerializationToken SerializeProperty(object graph, PropertyInfo property)
            {
                if (graph == null)
                {
                    return new SerializationToken {
                               TokenType = SerializationTokenType.Null
                    }
                }
                ;
                if (_tokens.ContainsKey(graph))
                {
                    return(_tokens[graph]);
                }
                SerializationToken result = new SerializationToken();

                _tokens[graph] = result;
                ISerializerToken serializerToken = null;

                if (property != null)
                {
                    ConverterAttribute converterAttribute = property.GetCustomAttribute <ConverterAttribute>();
                    if (converterAttribute != null)
                    {
                        serializerToken =
                            (ISerializerToken)Activator.CreateInstance(converterAttribute.SerializerTokenType);
                    }
                }
                if (serializerToken == null)
                {
                    serializerToken = Resolve(graph.GetType());
                }
                serializerToken.Serialize(graph, this, result);
                _tokens[graph] = result;
                return(result);
            }
Exemple #2
0
        public void Serialize(object graph, IGenericSerialierToken serializerToken, SerializationToken resultSerializationToken)
        {
            IEnumerable enumerable = (IEnumerable)graph;
            List <SerializationToken> serializationTokens = new List <SerializationToken>();

            foreach (object item in enumerable)
            {
                serializationTokens.Add(serializerToken.Serialize(item));
            }
            resultSerializationToken.Type                = graph.GetType();
            resultSerializationToken.TokenType           = SerializationTokenType.Array;
            resultSerializationToken.SerializationTokens = serializationTokens.ToArray();
        }
        public void Serialize(object graph, IGenericSerialierToken serializerToken, SerializationToken resultSerializationToken)
        {
            IDictionary dictionary = (IDictionary)graph;
            List <SerializationToken> serializationTokens = new List <SerializationToken>();

            foreach (object key in dictionary.Keys)
            {
                serializationTokens.Add(serializerToken.Serialize(key));
                serializationTokens.Add(serializerToken.Serialize(dictionary[key]));
            }
            resultSerializationToken.Type                = graph.GetType();
            resultSerializationToken.TokenType           = SerializationTokenType.Dictionary;
            resultSerializationToken.SerializationTokens = serializationTokens.ToArray();
        }
Exemple #4
0
            public DeserializeResult DeserializeProperty(SerializationToken serializationToken, PropertyInfo property)
            {
                if (serializationToken.Type == null)
                {
                    return new DeserializeResult {
                               Use = false
                    }
                }
                ;
                if (serializationToken.TokenType == SerializationTokenType.Null)
                {
                    return(new DeserializeResult());
                }
                if (_reverseTokens.ContainsKey(serializationToken))
                {
                    return new DeserializeResult {
                               Result = _reverseTokens[serializationToken]
                    }
                }
                ;
                DeserializeResult result          = new DeserializeResult();
                ISerializerToken  serializerToken = null;

                if (property != null)
                {
                    ConverterAttribute converterAttribute = property.GetCustomAttribute <ConverterAttribute>();

                    if (converterAttribute != null)
                    {
                        serializerToken =
                            (ISerializerToken)Activator.CreateInstance(converterAttribute.SerializerTokenType);
                    }
                }
                if (serializerToken == null)
                {
                    serializerToken = Resolve(serializationToken.Type);
                }

                serializerToken.Deserialize(serializationToken, this, result);
                if (result.Use)
                {
                    _reverseTokens[serializationToken] = result.Result;
                }
                return(result);
            }
        public void Deserialize(SerializationToken serializationToken, IGenericSerialierToken serializerToken, DeserializeResult deserializeResult)
        {
            Type result = null;

            try
            {
                result = Type.GetType((string)serializationToken.Data);
            }
            catch (Exception)
            {
                // ignored
            }
            if (result == null)
            {
                deserializeResult.Use = false;
            }
            deserializeResult.Result = result;
        }
        public void Serialize(object graph, IGenericSerialierToken serializerToken, SerializationToken resultSerializationToken)
        {
            PropertyInfo[]            info = graph.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
            List <SerializationToken> serializationTokens = new List <SerializationToken>();

            foreach (
                PropertyInfo source in
                info.Where(
                    propertyInfo =>
                    propertyInfo.CanRead && propertyInfo.CanWrite &&
                    propertyInfo.GetIndexParameters().Length == 0))
            {
                serializationTokens.Add(serializerToken.Serialize(source.Name));
                serializationTokens.Add(serializerToken.SerializeProperty(source.GetValue(graph, new object[0]), source));
            }
            resultSerializationToken.Type                = graph.GetType();
            resultSerializationToken.TokenType           = SerializationTokenType.Concrete;
            resultSerializationToken.SerializationTokens = serializationTokens.ToArray();
        }
        public void Deserialize(SerializationToken serializationToken, IGenericSerialierToken serializerToken, DeserializeResult deserializeResult)
        {
            object graph;

            try
            {
                graph = Activator.CreateInstance(serializationToken.Type);
            }
            catch (Exception e)
            {
                deserializeResult.Result = null;
                deserializeResult.Use    = false;
                return;
            }
            serializerToken.RegisterReverseToken(serializationToken, graph);
            for (int i = 0; i < serializationToken.SerializationTokens.Length / 2; i++)
            {
                string propertyName = (string)serializerToken.Deserialize(serializationToken.SerializationTokens[2 * i]).Result;

                PropertyInfo propertyInfo = serializationToken.Type.GetProperty(propertyName,
                                                                                BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
                if (propertyInfo != null && propertyInfo.CanRead && propertyInfo.CanWrite &&
                    propertyInfo.GetIndexParameters().Length == 0)
                {
                    DeserializeResult dResult = serializerToken.DeserializeProperty(serializationToken.SerializationTokens[2 * i + 1], propertyInfo);
                    if (dResult.Use)
                    {
                        try
                        {
                            propertyInfo.SetValue(graph, dResult.Result, new object[0]);
                        }
                        catch (Exception)
                        {
                            // ignored
                        }
                    }
                }
            }
            deserializeResult.Result = graph;
        }
Exemple #8
0
        public void Deserialize(SerializationToken serializationToken, IGenericSerialierToken serializerToken, DeserializeResult deserializeResult)
        {
            Array array = Array.CreateInstance(serializationToken.Type.GetElementType(),
                                               serializationToken.SerializationTokens.Length);

            serializerToken.RegisterReverseToken(serializationToken, array);
            for (int i = 0; i < serializationToken.SerializationTokens.Length; i++)
            {
                DeserializeResult dResult = serializerToken.Deserialize(serializationToken.SerializationTokens[i]);
                if (dResult.Use)
                {
                    try
                    {
                        array.SetValue(dResult.Result, i);
                    }
                    catch (Exception)
                    {
                        // ignored
                    }
                }
            }
            deserializeResult.Result = array;
        }
        public void Deserialize(SerializationToken serializationToken, IGenericSerialierToken serializerToken, DeserializeResult deserializeResult)
        {
            IDictionary dictionary = (IDictionary)Activator.CreateInstance(serializationToken.Type);

            serializerToken.RegisterReverseToken(serializationToken, dictionary);
            for (int i = 0; i < serializationToken.SerializationTokens.Length / 2; i++)
            {
                DeserializeResult key   = serializerToken.Deserialize(serializationToken.SerializationTokens[2 * i]);
                DeserializeResult value = serializerToken.Deserialize(serializationToken.SerializationTokens[2 * i + 1]);
                if (key.Use && key.Result != null && !dictionary.Contains(key.Result) && value.Use)
                {
                    try
                    {
                        dictionary.Add(key.Result, value.Result);
                    }
                    catch (Exception)
                    {
                        // ignored
                    }
                }
            }
            deserializeResult.Result = dictionary;
        }
        public void Deserialize(SerializationToken serializationToken, IGenericSerialierToken serializerToken, DeserializeResult deserializeResult)
        {
            Type parentIListType =
                GetParentTypes(serializationToken.Type)
                .First(type => type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IList <>));
            MethodInfo insertMethod = parentIListType.GetMethod("Insert");
            object     list         = Activator.CreateInstance(serializationToken.Type);

            serializerToken.RegisterReverseToken(serializationToken, list);
            for (int i = 0; i < serializationToken.SerializationTokens.Length; i++)
            {
                DeserializeResult dResult = serializerToken.Deserialize(serializationToken.SerializationTokens[i]);
                if (dResult.Use)
                {
                    insertMethod.Invoke(list, new[] { i, dResult.Result });
                }
                else
                {
                    Type elementType = parentIListType.GetGenericArguments().First();
                    insertMethod.Invoke(list, new[] { i, elementType.GetDefault() });
                }
            }
            deserializeResult.Result = list;
        }
 public void  Serialize(object graph, IGenericSerialierToken serializerToken, SerializationToken resultSerializationToken)
 {
     resultSerializationToken.Data      = ((Type)graph).AssemblyQualifiedName;
     resultSerializationToken.TokenType = SerializationTokenType.Type;
     resultSerializationToken.Type      = typeof(Type);
 }
 public void Deserialize(SerializationToken serializationToken, IGenericSerialierToken serializerToken, DeserializeResult deserializeResult)
 {
     deserializeResult.Result = serializationToken.Data;
 }
 public void Serialize(object graph, IGenericSerialierToken serializerToken, SerializationToken resultSerializationToken)
 {
     resultSerializationToken.TokenType = SerializationTokenType.Data;
     resultSerializationToken.Type      = graph.GetType();
     resultSerializationToken.Data      = graph;
 }
Exemple #14
0
 public void RegisterReverseToken(SerializationToken serializationToken, object graph)
 {
     _reverseTokens[serializationToken] = graph;
 }
Exemple #15
0
 public DeserializeResult Deserialize(SerializationToken serializationToken)
 {
     return(DeserializeProperty(serializationToken, null));
 }