public override object Deserialize(IDictionaryConverterLocator locator, Dictionary <string, string> serialized, Type type) { return(type.GetConstructors()[0].Invoke( TypeDescriptor.GetProperties(type) .OfType <PropertyDescriptor>() .Select(prop => DeserializeProperty(locator, serialized, prop)) .ToArray())); }
protected object DeserializeProperty(IDictionaryConverterLocator locator, Dictionary <string, string> serialized, PropertyDescriptor property) { var converter = locator.GetConverter(property.PropertyType); var separator = converter.GetSeparator(); var propSerialized = serialized.FilterWithKeyPrefix(property.Name) .ToDictionary(x => x.Key.StartsWith(separator) ? x.Key.Substring(separator.Length) : x.Key, x => x.Value); var propValue = converter.Deserialize(locator, propSerialized, property.PropertyType); return(propValue); }
public virtual object Deserialize(IDictionaryConverterLocator locator, Dictionary <string, string> serialized, Type type) { if (serialized.ContainsKey(string.Empty) && serialized[string.Empty] != null) { return(FromString(type, serialized[string.Empty])); } else { return(type.GetDefaultValue()); } }
public string GetPath(IDictionaryConverterLocator locator, object container, object value) { var property = TypeDescriptor.GetProperties(container).Cast <PropertyDescriptor>().FirstOrDefault(x => x.GetValue(container) == value); if (property != null) { return(NewPrefix(property.Name, string.Empty, locator.GetConverter(value))); } else { return(string.Empty); } }
public string GetPath(IDictionaryConverterLocator locator, object container, object value) { var index = 0; foreach (var item in (IEnumerable)container) { if (item == value) { return(NewPrefix(string.Empty, index, locator.GetConverter(value).GetSeparator())); } index++; } return(string.Empty); }
public virtual Dictionary <string, string> Serialize(IDictionaryConverterLocator locator, object value) { var type = value.GetType(); var typeConverter = TypeDescriptor.GetConverter(type); if (typeConverter.CanConvertTo(typeof(string))) { return(new Dictionary <string, string> { { string.Empty, ToString(value) } }); } else { throw new ArgumentException(string.Format("It's not possible to convert type '{0}' to 'string'.", type.FullName)); } }
public Dictionary <string, string> Serialize(IDictionaryConverterLocator locator, object value) { var result = new Dictionary <string, string>(); var index = 0; foreach (var item in (IEnumerable)value) { var converter = locator.GetConverter(item); var serialized = converter.Serialize(locator, item); result.AddRange(serialized.ToDictionary(x => NewPrefix(x.Key, index, converter.GetSeparator()), x => x.Value)); index++; } return(result); }
public virtual object Deserialize(IDictionaryConverterLocator locator, Dictionary <string, string> serialized, Type type) { if (serialized.Any() && serialized.All(x => !string.IsNullOrEmpty(x.Key) || x.Value != null)) { var result = Activator.CreateInstance(type); foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(result)) { property.SetValue(result, DeserializeProperty(locator, serialized, property)); } return(result); } else { return(type.GetDefaultValue()); } }
public Dictionary <string, string> Serialize(IDictionaryConverterLocator locator, object value) { var objHasValue = (bool)value.GetType().GetProperty("HasValue").GetValue(value, null); if (objHasValue) { //If it has a value then it serializes into the same object var objValue = value.GetType().GetProperty("Value").GetValue(value, null); var converter = locator.GetConverter(objValue); return(converter.Serialize(locator, objValue)); } else { //If it doesn't have a value it returns an empty dictionary return(new Dictionary <string, string> { }); } }
public virtual Dictionary <string, string> Serialize(IDictionaryConverterLocator locator, object value) { var result = new Dictionary <string, string>(); var properties = TypeDescriptor.GetProperties(value); if (properties.Count > 0) { foreach (PropertyDescriptor property in properties) { var propertyValue = property.GetValue(value); var converter = locator.GetConverter(propertyValue); var serialized = converter.Serialize(locator, propertyValue); var separator = locator.GetConverter(propertyValue); result.AddRange(serialized.ToDictionary(x => NewPrefix(property.Name, x.Key, converter), x => x.Value)); } } else { result.Add(string.Empty, value != null ? EmptyObject : null); } return(result); }
public object Deserialize(IDictionaryConverterLocator locator, Dictionary <string, string> serialized, Type type) { //Obtains the inner type of the optional var elementType = type.GetGenericArguments().First(); if (serialized.ContainsKey("Nothing")) { //Generates Nothing dynamically var resNothingType = typeof(NothingStrict <>).MakeGenericType(elementType); return(Activator.CreateInstance(resNothingType)); } else { var elementConverter = locator.GetConverter(elementType); var justVal = elementConverter.Deserialize(locator, serialized, elementType); //Generates Just dynamically Type justType = typeof(JustStrict <>); Type resJustType = justType.MakeGenericType(elementType); System.Reflection.ConstructorInfo ctor = resJustType.GetConstructor(new Type[] { elementType }); return(ctor.Invoke(new object[] { justVal })); } }
public virtual object Deserialize(IDictionaryConverterLocator locator, Dictionary <string, string> serialized, Type type) { var elementType = ElementType(type); var elementConverter = locator.GetConverter(elementType); var list = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(elementType)); if (serialized.Any()) { int deserialized = 0; for (int index = 0; true; index++) { var indexSerialized = serialized.FilterWithKeyPrefix(NewPrefix(string.Empty, index, string.Empty) + elementConverter.GetSeparator()); var listElement = elementConverter.Deserialize(locator, indexSerialized, elementType); list.Add(listElement); deserialized += indexSerialized.Count; if (deserialized == serialized.Count) { break; } } } return(TryCastFromList(list, type, elementType)); }
public static IDictionaryConverter GetConverter(this IDictionaryConverterLocator locator, object value) { var type = value != null?value.GetType() : typeof(object); return(locator.GetConverter(type)); }
public string GetPath(IDictionaryConverterLocator locator, object container, object value) { return(string.Empty); }
public override object Deserialize(IDictionaryConverterLocator locator, Dictionary <string, string> serialized, Type type) { var flatExpression = (FlatExpression)base.Deserialize(locator, serialized, typeof(FlatExpression)); return(flatExpression.Unflatten()); }
public override Dictionary <string, string> Serialize(IDictionaryConverterLocator locator, object value) { return(base.Serialize(locator, ((LambdaExpression)value).Flatten())); }
public override object Deserialize(IDictionaryConverterLocator locator, Dictionary <string, string> serialized, Type type) { return(Type.GetType(serialized[string.Empty])); }
public override Dictionary <string, string> Serialize(IDictionaryConverterLocator locator, object value) { return(new Dictionary <string, string> { { string.Empty, ((Type)value).AssemblyQualifiedName } }); }