public override fsResult TrySerialize(object instance, out fsData serialized, Type storageType)
        {
            serialized = fsData.CreateDictionary();
            var result = fsResult.Success;

            fsMetaType metaType = fsMetaType.Get(instance.GetType());

            metaType.EmitAotData();

            for (int i = 0; i < metaType.Properties.Length; ++i)
            {
                fsMetaProperty property = metaType.Properties[i];
                if (property.CanRead == false)
                {
                    continue;
                }

                fsData serializedData;

                var itemResult = Serializer.TrySerialize(property.StorageType, property.Read(instance), out serializedData);
                result.AddMessages(itemResult);
                if (itemResult.Failed)
                {
                    continue;
                }

                serialized.AsDictionary[property.JsonName] = serializedData;
            }

            return(result);
        }
        public override fsResult TrySerialize(object instance, out fsData serialized, Type storageType)
        {
            serialized = fsData.CreateDictionary();
            var result = fsResult.Success;

            fsMetaType metaType = fsMetaType.Get(instance.GetType());

            //Dont do this for UnityObject. While there is fsUnityObjectConverter, this converter is also used as override,
            //when serializing a UnityObject directly.
            object defaultInstance = null;

            if (fsGlobalConfig.SerializeDefaultValues == false && !(instance is UnityEngine.Object))
            {
                defaultInstance = metaType.GetDefaultInstance();
            }

            for (int i = 0; i < metaType.Properties.Length; ++i)
            {
                fsMetaProperty property = metaType.Properties[i];

                if (property.WriteOnly)
                {
                    continue;
                }

                if (property.AsReference && Serializer.IgnoreSerializeCycleReferences)
                {
                    continue;
                }

                var propertyValue = property.Read(instance);

                // auto instance?
                if (propertyValue == null && property.AutoInstance)
                {
                    propertyValue = fsMetaType.Get(property.StorageType).CreateInstance();
                    property.Write(instance, propertyValue);
                }
                else if (fsGlobalConfig.SerializeDefaultValues == false && defaultInstance != null)
                {
                    if (Equals(propertyValue, property.Read(defaultInstance)))
                    {
                        continue;
                    }
                }

                fsData serializedData;
                var    itemResult = Serializer.TrySerialize(property.StorageType, propertyValue, out serializedData);
                result.AddMessages(itemResult);
                if (itemResult.Failed)
                {
                    continue;
                }

                serialized.AsDictionary[property.JsonName] = serializedData;
            }

            return(result);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Adds a new AOT compilation unit.
 /// </summary>
 /// <param name="type">The type of object we are AOT compiling.</param>
 /// <param name="members">The members on the object which will be serialized/deserialized.</param>
 public static void AddAotCompilation(Type type, fsMetaProperty[] members, bool isConstructorPublic)
 {
     _uncomputedAotCompilations.Add(new AotCompilation {
         Type = type,
         Members = members,
         IsConstructorPublic = isConstructorPublic
     });
 }
Ejemplo n.º 4
0
        public override fsResult TrySerialize(object instance, out fsData serialized, Type storageType)
        {
            // 传进来的serialized 直接赋值一个Dict
            serialized = fsData.CreateDictionary();
            var result = fsResult.Success;

            // Serializer.Config 就是一些配置
            fsMetaType metaType = fsMetaType.Get(Serializer.Config, instance.GetType());

            metaType.EmitAotData();

            //PARADOXNOTION ADDITION
            object defaultInstance = null;

            //Dont do this for UnityObject. While there is fsUnityObjectConverter, this converter is also used as override,
            //when serializing a UnityObject directly.
            if (fsGlobalConfig.SerializeDefaultValues == false && !(instance is UnityEngine.Object))
            {
                defaultInstance = metaType.CreateInstance();
            }
            //

            for (int i = 0; i < metaType.Properties.Length; ++i)
            {
                fsMetaProperty property = metaType.Properties[i];
                // 不可读的跳过
                if (property.CanRead == false)
                {
                    continue;
                }

                // 如果是不存储默认值的情况
                //PARADOXNOTION ADDITION
                if (fsGlobalConfig.SerializeDefaultValues == false && defaultInstance != null)
                {
                    if (Equals(property.Read(instance), property.Read(defaultInstance)))
                    {
                        continue;
                    }
                }
                //

                fsData serializedData;

                // property.Read(instance) 理解为从某一个实例读取某一个属性,字段值
                var itemResult = Serializer.TrySerialize(property.StorageType, property.OverrideConverterType,
                                                         property.Read(instance), out serializedData);
                result.AddMessages(itemResult);
                if (itemResult.Failed)
                {
                    continue;
                }

                serialized.AsDictionary[property.JsonName] = serializedData;
            }

            return(result);
        }
Ejemplo n.º 5
0
        public override fsResult TryDeserialize(fsData data, ref object instance, Type storageType)
        {
            var result = fsResult.Success;

            // Verify that we actually have an Object
            if ((result += CheckType(data, fsDataType.Object)).Failed)
            {
                return(result);
            }

            if (data.AsDictionary.Count == 0)
            {
                return(fsResult.Success);
            }

            fsMetaType metaType = fsMetaType.Get(Serializer.Config, storageType);

            metaType.EmitAotData();

            for (int i = 0; i < metaType.Properties.Length; ++i)
            {
                fsMetaProperty property = metaType.Properties[i];
                if (property.CanWrite == false)
                {
                    continue;
                }

                fsData propertyData;
                if (data.AsDictionary.TryGetValue(property.JsonName, out propertyData))
                {
                    object deserializedValue = null;

/*
 *                  // We have to read in the existing value, since we need to support partial
 *                  // deserialization. However, this is bad for perf.
 *                  // TODO: Find a way to avoid this call when we are not doing a partial deserialization
 *                  //       Maybe through a new property, ie, Serializer.IsPartialSerialization, which just
 *                  //       gets set when starting a new serialization? We cannot pipe the information
 *                  //       through CreateInstance unfortunately.
 *                  if (property.CanRead) {
 *                      deserializedValue = property.Read(instance);
 *                  }
 */
                    var itemResult = Serializer.TryDeserialize(propertyData, property.StorageType,
                                                               property.OverrideConverterType, ref deserializedValue);
                    result.AddMessages(itemResult);
                    if (itemResult.Failed)
                    {
                        continue;
                    }

                    property.Write(instance, deserializedValue);
                }
            }

            return(result);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// AOT compiles the object (in C#).
        /// </summary>
        private static string GenerateDirectConverterForTypeInCSharp(Type type, fsMetaProperty[] members, bool isConstructorPublic)
        {
            var sb = new StringBuilder();
            string typeName = type.CSharpName(/*includeNamespace:*/ true);
            string typeNameSafeDecl = type.CSharpName(true, true);

            sb.AppendLine("using System;");
            sb.AppendLine("using System.Collections.Generic;");
            sb.AppendLine();
            sb.AppendLine("namespace FullSerializer {");
            sb.AppendLine("    partial class fsConverterRegistrar {");
            sb.AppendLine("        public static Speedup." + typeNameSafeDecl + "_DirectConverter " + "Register_" + typeNameSafeDecl + ";");
            sb.AppendLine("    }");
            sb.AppendLine("}");
            sb.AppendLine();
            sb.AppendLine("namespace FullSerializer.Speedup {");
            sb.AppendLine("    public class " + typeNameSafeDecl + "_DirectConverter : fsDirectConverter<" + typeName + "> {");
            sb.AppendLine("        protected override fsResult DoSerialize(" + typeName + " model, Dictionary<string, fsData> serialized) {");
            sb.AppendLine("            var result = fsResult.Success;");
            sb.AppendLine();
            foreach (var member in members) {
                sb.AppendLine("            result += SerializeMember(serialized, " + GetConverterString(member) + ", \"" + member.JsonName + "\", model." + member.MemberName + ");");
            }
            sb.AppendLine();
            sb.AppendLine("            return result;");
            sb.AppendLine("        }");
            sb.AppendLine();
            sb.AppendLine("        protected override fsResult DoDeserialize(Dictionary<string, fsData> data, ref " + typeName + " model) {");
            sb.AppendLine("            var result = fsResult.Success;");
            sb.AppendLine();
            for (int i = 0; i < members.Length; ++i) {
                var member = members[i];
                sb.AppendLine("            var t" + i + " = model." + member.MemberName + ";");
                sb.AppendLine("            result += DeserializeMember(data, " + GetConverterString(member) + ", \"" + member.JsonName + "\", out t" + i + ");");
                sb.AppendLine("            model." + member.MemberName + " = t" + i + ";");
                sb.AppendLine();
            }
            sb.AppendLine("            return result;");
            sb.AppendLine("        }");
            sb.AppendLine();
            sb.AppendLine("        public override object CreateInstance(fsData data, Type storageType) {");
            if (isConstructorPublic) {
                sb.AppendLine("            return new " + typeName + "();");
            }
            else {
                sb.AppendLine("            return Activator.CreateInstance(typeof(" + typeName + "), /*nonPublic:*/true);");
            }
            sb.AppendLine("        }");
            sb.AppendLine("    }");
            sb.AppendLine("}");

            return sb.ToString();
        }
        public override fsResult TryDeserialize(fsData data, ref object instance, Type storageType)
        {
            var result = fsResult.Success;

            // Verify that we actually have an Object
            if ((result += CheckType(data, fsDataType.Object)).Failed)
            {
                return(result);
            }

            if (data.AsDictionary.Count == 0)
            {
                return(fsResult.Success);
            }

            fsMetaType metaType = fsMetaType.Get(storageType);

            for (int i = 0; i < metaType.Properties.Length; ++i)
            {
                fsMetaProperty property = metaType.Properties[i];

                if (property.ReadOnly)
                {
                    continue;
                }

                fsData propertyData;
                if (data.AsDictionary.TryGetValue(property.JsonName, out propertyData))
                {
                    object deserializedValue = null;

                    //This does not work well with no serializing default values -> Find a workaround.
                    if (fsGlobalConfig.SerializeDefaultValues)
                    {
                        if (metaType.DeserializeOverwriteRequest || typeof(ICollection).IsAssignableFrom(storageType))
                        {
                            deserializedValue = property.Read(instance);
                        }
                    }

                    var itemResult = Serializer.TryDeserialize(propertyData, property.StorageType, ref deserializedValue, null);
                    result.AddMessages(itemResult);
                    if (itemResult.Failed)
                    {
                        continue;
                    }

                    property.Write(instance, deserializedValue);
                }
            }

            return(result);
        }
        private fsResult GetProperty(object instance, out fsMetaProperty property)
        {
            var properties = fsMetaType.Get(instance.GetType()).Properties;

            for (int i = 0; i < properties.Length; ++i)
            {
                if (properties[i].MemberName == _memberName)
                {
                    property = properties[i];
                    return(fsResult.Success);
                }
            }

            property = default(fsMetaProperty);
            return(fsResult.Fail("No property named \"" + _memberName + "\" on " + instance.GetType().FriendlyName()));
        }
        public override fsResult TryDeserialize(fsData data, ref object instance, Type storageType)
        {
            var result = fsResult.Success;

            // Verify that we actually have an Object
            if ((result += CheckType(data, fsDataType.Object)).Failed)
            {
                return(result);
            }

            if (data.AsDictionary.Count == 0)
            {
                return(fsResult.Success);
            }

            fsMetaType metaType = fsMetaType.Get(Serializer.Config, storageType);

            for (int i = 0; i < metaType.Properties.Length; ++i)
            {
                fsMetaProperty property = metaType.Properties[i];

                fsData propertyData;
                if (data.AsDictionary.TryGetValue(property.JsonName, out propertyData))
                {
                    object deserializedValue = null;

                    var itemResult = Serializer.TryDeserialize(propertyData, property.StorageType, null, ref deserializedValue);
                    result.AddMessages(itemResult);
                    if (itemResult.Failed)
                    {
                        continue;
                    }

                    property.Write(instance, deserializedValue);
                }
            }

            return(result);
        }
Ejemplo n.º 10
0
        private static string GetConverterString(fsMetaProperty member)
        {
            if (member.OverrideConverterType == null)
                return "null";

            return string.Format("typeof({0})",
                                 member.OverrideConverterType.CSharpName(/*includeNamespace:*/ true));
        }