Skip to content

Commit

Permalink
Surrogate can't deal with switching a complex type with a value, repl…
Browse files Browse the repository at this point in the history
…ace it with a dummy class
  • Loading branch information
anaisbetts committed Dec 21, 2010
1 parent 93613d1 commit fb9d30f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace ReactiveXaml.Serialization
{
public class DataContractSerializationProvider
public class DataContractSerializationProvider : IEnableLogger
{
IDataContractSurrogate _dataSurrogate;
IEnumerable<Type> _knownTypes;
Expand All @@ -33,6 +33,10 @@ public byte[] Serialize(object obj)
public object Deserialize(byte[] data, Type type)
{
var serializer = createSerializer(type);
#if DEBUG
string json = Encoding.Default.GetString(data);
this.Log().Info(json);
#endif
using (var ms = new MemoryStream(data)) {
return serializer.ReadObject(ms);
}
Expand Down
2 changes: 1 addition & 1 deletion ReactiveXaml.Serialization/ModelBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void setupModelBase()
ContentHash = CalculateHash();
RxStorage.Engine.Save(this);
});
ContentHash = CalculateHash();
ContentHash = CalculateHash();
}

public virtual Guid CalculateHash()
Expand Down
24 changes: 15 additions & 9 deletions ReactiveXaml.Serialization/SerializationDataSurrogates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

namespace ReactiveXaml.Serialization
{
public class SurrogateHashContainer {
public Guid Hash { get; set; }
}

public class SerializationItemDataSurrogate : IDataContractSurrogate
{
IStorageEngine _engine;
Expand All @@ -22,8 +26,13 @@ public SerializationItemDataSurrogate(IStorageEngine engine = null, object rootO

public Type GetDataContractType(Type type)
{
/*
if (typeof(SurrogateHashContainer) == type) {
return typeof(ISerializableItem);
}*/

if (typeof(ISerializableItem).IsAssignableFrom(type) && _toSkip == null) {
return typeof(Guid);
return typeof(SurrogateHashContainer);
}

return type;
Expand All @@ -36,8 +45,9 @@ public object GetDeserializedObject(object obj, Type targetType)
return obj;
}

if (obj is Guid && typeof(ISerializableItem).IsAssignableFrom(targetType)) {
return _engine.Load((Guid) obj);
if (obj is SurrogateHashContainer && typeof (ISerializableItem).IsAssignableFrom(targetType)) {
var hash = (SurrogateHashContainer) obj;
return _engine.Load(hash.Hash);
}

return obj;
Expand All @@ -53,16 +63,12 @@ public object GetObjectToSerialize(object obj, Type targetType)
var sib = obj as ISerializableItem;
if (sib != null) {
_engine.Save(sib);
return sib.ContentHash;
return new SurrogateHashContainer() {Hash = sib.ContentHash};
}
return obj;
}

public Type GetReferencedTypeOnImport(string typeName, string typeNamespace, object customData)
{
return null;
}

public Type GetReferencedTypeOnImport(string typeName, string typeNamespace, object customData) { return null; }
public object GetCustomDataToExport(Type clrType, Type dataContractType) { return null; }
public object GetCustomDataToExport(MemberInfo memberInfo, Type dataContractType) { return null; }
public void GetKnownCustomDataTypes(Collection<Type> customDataTypes) { }
Expand Down

0 comments on commit fb9d30f

Please sign in to comment.