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(/*throwException:*/ false); for (int i = 0; i < metaType.Properties.Length; ++i) { fsMetaProperty property = metaType.Properties[i]; // We only save properties marked with RuntimeSaveAttribute if (property.RuntimeSave == 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); }
/// <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); } } }
private static string GetConverterString(fsMetaProperty member) { if (member.OverrideConverterType == null) return "null"; return string.Format("typeof({0})", member.OverrideConverterType.CSharpName(/*includeNamespace:*/ true)); }
/// <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) { throw new InvalidOperationException("Unable to find property " + propertyNames[i] + " on " + ReflectedType.Name); } 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); } } }
// Token: 0x060004E7 RID: 1255 RVA: 0x0001E594 File Offset: 0x0001C794 private static string GetConverterString(fsMetaProperty member) { if (member.OverrideConverterType == null) { return("null"); } return(string.Format("typeof({0})", member.OverrideConverterType.CSharpName(true))); }
/// <summary> /// AOT compiles the object (in C#). /// </summary> private static string GenerateDirectConverterForTypeInCSharp(Type type, fsMetaProperty[] members, bool isConstructorPublic) { StringBuilder 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 (fsMetaProperty 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) { fsMetaProperty 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(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); }
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(); } }
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())); }
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(); var defaultInstance = metaType.CreateInstance(); for (int i = 0; i < metaType.Properties.Length; ++i) { fsMetaProperty property = metaType.Properties[i]; if (property.CanRead == false) { continue; } if (fsGlobalConfig.SerializeDefaultValues == false) { if (Equals(property.Read(instance), property.Read(defaultInstance))) { 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); }
// Token: 0x060004E8 RID: 1256 RVA: 0x0001E5C8 File Offset: 0x0001C7C8 private static string GenerateDirectConverterForTypeInCSharp(Type type, fsMetaProperty[] members, bool isConstructorPublic) { StringBuilder stringBuilder = new StringBuilder(); string text = type.CSharpName(true); string text2 = type.CSharpName(true, true); stringBuilder.AppendLine("using System;"); stringBuilder.AppendLine("using System.Collections.Generic;"); stringBuilder.AppendLine(); stringBuilder.AppendLine("namespace FullSerializer {"); stringBuilder.AppendLine(" partial class fsConverterRegistrar {"); stringBuilder.AppendLine(string.Concat(new string[] { " public static Speedup.", text2, "_DirectConverter Register_", text2, ";" })); stringBuilder.AppendLine(" }"); stringBuilder.AppendLine("}"); stringBuilder.AppendLine(); stringBuilder.AppendLine("namespace FullSerializer.Speedup {"); stringBuilder.AppendLine(string.Concat(new string[] { " public class ", text2, "_DirectConverter : fsDirectConverter<", text, "> {" })); stringBuilder.AppendLine(" protected override fsResult DoSerialize(" + text + " model, Dictionary<string, fsData> serialized) {"); stringBuilder.AppendLine(" var result = fsResult.Success;"); stringBuilder.AppendLine(); for (int i = 0; i < members.Length; i++) { fsMetaProperty fsMetaProperty = members[i]; stringBuilder.AppendLine(string.Concat(new string[] { " result += SerializeMember(serialized, ", fsAotCompilationManager.GetConverterString(fsMetaProperty), ", \"", fsMetaProperty.JsonName, "\", model.", fsMetaProperty.MemberName, ");" })); } stringBuilder.AppendLine(); stringBuilder.AppendLine(" return result;"); stringBuilder.AppendLine(" }"); stringBuilder.AppendLine(); stringBuilder.AppendLine(" protected override fsResult DoDeserialize(Dictionary<string, fsData> data, ref " + text + " model) {"); stringBuilder.AppendLine(" var result = fsResult.Success;"); stringBuilder.AppendLine(); for (int j = 0; j < members.Length; j++) { fsMetaProperty fsMetaProperty2 = members[j]; stringBuilder.AppendLine(string.Concat(new object[] { " var t", j, " = model.", fsMetaProperty2.MemberName, ";" })); stringBuilder.AppendLine(string.Concat(new object[] { " result += DeserializeMember(data, ", fsAotCompilationManager.GetConverterString(fsMetaProperty2), ", \"", fsMetaProperty2.JsonName, "\", out t", j, ");" })); stringBuilder.AppendLine(string.Concat(new object[] { " model.", fsMetaProperty2.MemberName, " = t", j, ";" })); stringBuilder.AppendLine(); } stringBuilder.AppendLine(" return result;"); stringBuilder.AppendLine(" }"); stringBuilder.AppendLine(); stringBuilder.AppendLine(" public override object CreateInstance(fsData data, Type storageType) {"); if (isConstructorPublic) { stringBuilder.AppendLine(" return new " + text + "();"); } else { stringBuilder.AppendLine(" return Activator.CreateInstance(typeof(" + text + "), /*nonPublic:*/true);"); } stringBuilder.AppendLine(" }"); stringBuilder.AppendLine(" }"); stringBuilder.AppendLine("}"); return(stringBuilder.ToString()); }
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()); }