public object Deserialize(IStorage storage, Data.Node data, object result) { Kean.Collection.IList<Data.Node> nodes; Reflect.Type type; Reflect.Type elementType; type = data.Type; elementType = this.GetElementType(type); if (data is Data.Collection) nodes = (data as Data.Collection).Nodes; else { // only one element so it was impossible to know it was a collection nodes = new Kean.Collection.List<Data.Node>(data); data.Type = data.OriginalType ?? elementType; Uri.Locator locator = data.Locator.Copy(); locator.Fragment += "0"; data.Locator = locator; } if (result.IsNull()) result = this.Create(type, elementType, nodes.Count); int i = 0; foreach (Data.Node child in nodes) { int c = i++; // ensure c is unique in every closure of lambda function below storage.Deserialize(child, elementType, d => this.Set(result, d, c)); } return result; }
public object Deserialize(IStorage storage, Data.Node data, object result) { result = data.Type.Create(); Reflect.Field[] fields = result.GetFields(); foreach (Data.Node node in (data as Data.Branch).Nodes) { string name = node.Name.Convert(storage.Casing, Casing.Camel); Reflect.Field field = fields.Find(f => f.Name == name); if (field.NotNull()) storage.Deserialize(node, field.Type, d => field.Data = d); else new Exception.FieldMissing(data.Type, name, node.Region).Throw(); } return result; }
public object Deserialize(IStorage storage, Data.Node data, object result) { if (result.IsNull()) try { result = data.Type.Create(); } catch (System.MissingMethodException e) { if (data.Type == data.OriginalType) new Exception.CreateAbstract(e, data.Type, data.Region).Throw(); else new Exception.UnknownType(e, data.OriginalType, data.Region).Throw(); } if (result.NotNull()) { Reflect.Property[] properties = result.GetProperties(); if (data is Data.Branch) foreach (Data.Node node in (data as Data.Branch).Nodes) { string name = node.Name.Convert(storage.Casing, Casing.Pascal); Reflect.Property property = properties.Find(p => { Kean.Serialize.ParameterAttribute[] attributes; return (attributes = p.GetAttributes<Kean.Serialize.ParameterAttribute>()).Length > 0 && attributes[0].Name.NotEmpty() ? attributes[0].Name == node.Name : p.Name == name; }); if (property.IsNull()) new Exception.PropertyMissing(data.Type, name, node.Region).Throw(); else if (property.Writable) storage.Deserialize(node, property.Type, d => property.Data = d); else if (property.Readable && (property.Type.Category == Reflect.TypeCategory.Class || property.Type.Category == Reflect.TypeCategory.Array || property.Type.Category == Reflect.TypeCategory.Interface)) storage.DeserializeContent(node.DefaultType(property.Type), property.Data); else new Exception.PropertyNotWriteable(data.Type, name, node.Region).Throw(); } } return result; }