public object Deserialize(string serializedObject) { if (serializedObject == null) { throw new ArgumentNullException("serializedObject"); } Dictionary <string, string> dic = StringConversionServices.ParseKeyValueCollection(serializedObject); if ((dic.ContainsKey("SerializerHandlerType") == false) || (dic.ContainsKey("SerializedObject") == false)) { throw new InvalidOperationException("serializedObject is of wrong format"); } string serilizerHandlerTypeString = StringConversionServices.DeserializeValueString(dic["SerializerHandlerType"]); Type serilizerHandlerType = TypeManager.GetType(serilizerHandlerTypeString); ISerializerHandler serializerHandler; lock (_lock) { if (_serializeHandlers.TryGetValue(serilizerHandlerType, out serializerHandler) == false) { serializerHandler = (ISerializerHandler)Activator.CreateInstance(serilizerHandlerType); _serializeHandlers.Add(serilizerHandlerType, serializerHandler); } } string serializedObjectString = StringConversionServices.DeserializeValueString(dic["SerializedObject"]); object resultObject = serializerHandler.Deserialize(serializedObjectString); return(resultObject); }
public string Serialize(object objectToSerialize) { if (objectToSerialize == null) { throw new ArgumentNullException("objectToSerialize"); } StringBuilder sb = new StringBuilder(); StringConversionServices.SerializeKeyValuePair(sb, "_Type_", TypeManager.SerializeType(objectToSerialize.GetType())); IEnumerable <PropertyInfo> propertyInfos = from prop in objectToSerialize.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance) where prop.CanRead && prop.CanWrite select prop; foreach (PropertyInfo propertyInfo in propertyInfos) { MethodInfo methodInfo = (from mi in typeof(StringConversionServices).GetMethods(BindingFlags.Public | BindingFlags.Static) where mi.Name == "SerializeKeyValuePair" && mi.IsGenericMethodDefinition && mi.GetParameters().Length == 3 && mi.GetParameters()[2].ParameterType.IsGenericParameter select mi).SingleOrDefault(); methodInfo = methodInfo.MakeGenericMethod(new Type[] { propertyInfo.PropertyType }); object propertyValue = propertyInfo.GetValue(objectToSerialize, null); methodInfo.Invoke(null, new object[] { sb, propertyInfo.Name, propertyValue }); } return(sb.ToString()); }
public static T Deserialize <T>(Type propertyClassType, string serializedProperties) { ISerializer serializer = GetSerializer(propertyClassType); Dictionary <string, string> objectState = StringConversionServices.ParseKeyValueCollection(serializedProperties); return((T)serializer.Deserialize(objectState)); }
public bool TrySerialize(object objectToSerialize, out string serializedObject, out string errorMessage) { if (objectToSerialize == null) { throw new ArgumentNullException("objectToSerialize"); } serializedObject = null; IEnumerable <SerializerHandlerAttribute> serializerHandlerAttributes = objectToSerialize.GetType().GetCustomAttributesRecursively <SerializerHandlerAttribute>(); if (serializerHandlerAttributes.Count() == 0) { errorMessage = string.Format("The type '{0}' has no '{1}' defined in its inherit tree", objectToSerialize.GetType(), typeof(SerializerHandlerAttribute)); return(false); } SerializerHandlerAttribute serializerHandlerAttribute = serializerHandlerAttributes.First(); if (serializerHandlerAttribute.SerializerHandlerType == null) { errorMessage = string.Format("The type '{0}' has specified a null argument to the '{1}'", objectToSerialize.GetType(), typeof(SerializerHandlerAttribute)); return(false); } if (typeof(ISerializerHandler).IsAssignableFrom(serializerHandlerAttribute.SerializerHandlerType) == false) { errorMessage = string.Format("The type '{0}' has specified a type that does not implement the '{1}' argument to the '{2}'", objectToSerialize.GetType(), typeof(ISerializerHandler), typeof(SerializerHandlerAttribute)); return(false); } Type serializeHandlerType = serializerHandlerAttribute.SerializerHandlerType; ISerializerHandler serializerHandler; lock (_lock) { if (_serializeHandlers.TryGetValue(serializeHandlerType, out serializerHandler) == false) { serializerHandler = (ISerializerHandler)Activator.CreateInstance(serializeHandlerType); _serializeHandlers.Add(serializeHandlerType, serializerHandler); } } string serializedObj = serializerHandler.Serialize(objectToSerialize); StringBuilder sb = new StringBuilder(); StringConversionServices.SerializeKeyValuePair(sb, "SerializerHandlerType", TypeManager.SerializeType(serializerHandler.GetType())); StringConversionServices.SerializeKeyValuePair(sb, "SerializedObject", serializedObj); errorMessage = null; serializedObject = sb.ToString(); return(true); }
public object Deserialize(string serializedObject) { Dictionary <string, string> dic = StringConversionServices.ParseKeyValueCollection(serializedObject); if (dic.ContainsKey("_Type_") == false) { throw new ArgumentException("serializedObject is of wrong format"); } string typeString = StringConversionServices.DeserializeValueString(dic["_Type_"]); Type type = TypeManager.GetType(typeString); object obj = Activator.CreateInstance(type); IEnumerable <PropertyInfo> propertyInfos = from prop in type.GetProperties(BindingFlags.Public | BindingFlags.Instance) where prop.CanRead && prop.CanWrite select prop; foreach (PropertyInfo propertyInfo in propertyInfos) { if (dic.ContainsKey(propertyInfo.Name) == false) { throw new ArgumentException("serializedObject is of wrong format"); } MethodInfo methodInfo = (from mi in typeof(StringConversionServices).GetMethods(BindingFlags.Public | BindingFlags.Static) where mi.Name == "DeserializeValue" && mi.IsGenericMethodDefinition && mi.GetParameters().Length == 2 && mi.GetParameters()[1].ParameterType.IsGenericParameter select mi).SingleOrDefault(); object defaultValue; if (propertyInfo.PropertyType == typeof(Guid)) { defaultValue = default(Guid); } else if (propertyInfo.PropertyType == typeof(string)) { defaultValue = default(string); } else if (propertyInfo.PropertyType == typeof(int)) { defaultValue = default(int); } else if (propertyInfo.PropertyType == typeof(DateTime)) { defaultValue = default(DateTime); } else if (propertyInfo.PropertyType == typeof(bool)) { defaultValue = default(bool); } else if (propertyInfo.PropertyType == typeof(decimal)) { defaultValue = default(decimal); } else if (propertyInfo.PropertyType == typeof(long)) { defaultValue = default(long); } else { defaultValue = null; } methodInfo = methodInfo.MakeGenericMethod(new Type[] { propertyInfo.PropertyType }); object propertyValue = methodInfo.Invoke(null, new object[] { dic[propertyInfo.Name], defaultValue }); propertyInfo.SetValue(obj, propertyValue, null); } return(obj); }