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

            fsMetaType metaType = fsMetaType.Get(Serializer.Config, 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.OverrideConverterType,
                                                         property.Read(instance), out serializedData);
                result.AddMessages(itemResult);
                if (itemResult.Failed)
                {
                    continue;
                }

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

            return(result);
        }
        private static string GetConverterString(fsMetaProperty member) {
            if (member.OverrideConverterType == null)
                return "null";

            return string.Format("typeof({0})",
                                 member.OverrideConverterType.CSharpName(/*includeNamespace:*/ true));
        }
Пример #3
0
        public override fsFailure TryDeserialize(fsData data, ref object instance, Type storageType)
        {
            if (data.IsDictionary == false)
            {
                return(fsFailure.Fail("Reflected converter requires a dictionary for data"));
            }

            fsMetaType metaType = fsMetaType.Get(storageType);

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

                fsData propertyData;
                if (data.AsDictionary.TryGetValue(property.Name, out propertyData))
                {
                    object deserializedValue = null;
                    var    failed            = Serializer.TryDeserialize(propertyData, property.StorageType, ref deserializedValue);
                    if (failed.Failed)
                    {
                        return(failed);
                    }

                    property.Write(instance, deserializedValue);
                }
            }

            return(fsFailure.Success);
        }
 /// <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
     });
 }
 public Member(fsMetaProperty property)
 {
     MemberName = property.MemberName;
     JsonName = property.JsonName;
     StorageType = property.StorageType.CSharpName(true);
     OverrideConverterType = null;
     if (property.OverrideConverterType != null)
         OverrideConverterType = property.OverrideConverterType.CSharpName();
 }
        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);
            }

            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);
        }
Пример #7
0
        private fsResult GetProperty(object instance, out fsMetaProperty property) {
            var properties = fsMetaType.Get(Serializer.Config, 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().CSharpName());
        }
        /// <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();
        }
 // Token: 0x06000438 RID: 1080 RVA: 0x0001B798 File Offset: 0x00019998
 private fsResult GetProperty(object instance, out fsMetaProperty property)
 {
     fsMetaProperty[] properties = fsMetaType.Get(this.Serializer.Config, instance.GetType()).Properties;
     for (int i = 0; i < properties.Length; i++)
     {
         if (properties[i].MemberName == this._memberName)
         {
             property = properties[i];
             return(fsResult.Success);
         }
     }
     property = null;
     return(fsResult.Fail("No property named \"" + this._memberName + "\" on " + instance.GetType().CSharpName()));
 }
Пример #10
0
        // Token: 0x06000466 RID: 1126 RVA: 0x0001C52C File Offset: 0x0001A72C
        public override fsResult TryDeserialize(fsData data, ref object instance, Type storageType)
        {
            fsResult fsResult = fsResult.Success;
            fsResult fsResult2;

            fsResult = (fsResult2 = fsResult + base.CheckType(data, fsDataType.Object));
            if (fsResult2.Failed)
            {
                return(fsResult);
            }
            fsMetaType fsMetaType = fsMetaType.Get(this.Serializer.Config, storageType);

            fsMetaType.EmitAotData();
            for (int i = 0; i < fsMetaType.Properties.Length; i++)
            {
                fsMetaProperty property = fsMetaType.Properties[i];
                if (property.CanWrite)
                {
                    fsData data2;
                    if (data.AsDictionary.TryGetValue(property.JsonName, out data2))
                    {
                        int referenceId = fsBaseConverter.GetReferenceId(data2);
                        if (referenceId != -1 && this.Serializer.IsObjectVersionedAndUnderConstruction(referenceId))
                        {
                            object temp = instance;
                            this.Serializer.WhenInstanceCreated(referenceId, delegate(object refObject)
                            {
                                property.Write(temp, refObject);
                            });
                        }
                        else
                        {
                            object value = null;
                            if (property.CanRead)
                            {
                                value = property.Read(instance);
                            }
                            fsResult result = this.Serializer.TryDeserialize(data2, property.StorageType, property.OverrideConverterType, ref value);
                            fsResult.AddMessages(result);
                            if (!result.Failed)
                            {
                                property.Write(instance, value);
                            }
                        }
                    }
                }
            }
            return(fsResult);
        }
Пример #11
0
        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().CSharpName()));
        }
Пример #12
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);
            }

            fsMetaType metaType = fsMetaType.Get(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;

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

                    property.Write(instance, deserializedValue);
                }
            }

            return(result);
        }
Пример #13
0
        // Token: 0x06000465 RID: 1125 RVA: 0x0001C470 File Offset: 0x0001A670
        public override fsResult TrySerialize(object instance, out fsData serialized, Type storageType)
        {
            serialized = fsData.CreateDictionary();
            fsResult   success    = fsResult.Success;
            fsMetaType fsMetaType = fsMetaType.Get(this.Serializer.Config, instance.GetType());

            fsMetaType.EmitAotData();
            for (int i = 0; i < fsMetaType.Properties.Length; i++)
            {
                fsMetaProperty fsMetaProperty = fsMetaType.Properties[i];
                if (fsMetaProperty.CanRead)
                {
                    fsData   value;
                    fsResult result = this.Serializer.TrySerialize(fsMetaProperty.StorageType, fsMetaProperty.OverrideConverterType, fsMetaProperty.Read(instance), out value);
                    success.AddMessages(result);
                    if (!result.Failed)
                    {
                        serialized.AsDictionary[fsMetaProperty.JsonName] = value;
                    }
                }
            }
            return(success);
        }
Пример #14
0
        public override fsFailure TrySerialize(object instance, out fsData serialized, Type storageType)
        {
            serialized = fsData.CreateDictionary();

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

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

                fsData serializedData;

                var failed = Serializer.TrySerialize(property.StorageType, property.Read(instance), out serializedData);
                if (failed.Failed)
                {
                    return(failed);
                }

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

            return(fsFailure.Success);
        }
Пример #15
0
        /// <summary>
        /// Override the default property names and use the given ones instead of serialization.
        /// </summary>
        public void SetProperties(params string[] propertyNames) {
            Properties = new fsMetaProperty[propertyNames.Length];

            for (int i = 0; i < propertyNames.Length; ++i) {
                MemberInfo[] members = ReflectedType.GetFlattenedMember(propertyNames[i]);

                if (members.Length == 0) {
                    // We silently fail here b/c there could be stripping applied
                    // on the platform that removed the member
                    continue;
                }
                if (members.Length > 1) {
                    throw new InvalidOperationException("More than one property matches " +
                        propertyNames[i] + " on " + ReflectedType.Name);
                }

                MemberInfo member = members[0];
                if (member is FieldInfo) {
                    Properties[i] = new fsMetaProperty((FieldInfo)member);
                }
                else {
                    Properties[i] = new fsMetaProperty((PropertyInfo)member);
                }
            }
        }