Note: The question is restricted to C# UWP.
The Dream:
public static class Serializer {
// return json string
public string Serialize(object obj) { ??? }
// return T from json string
public T Deserialize<T>(string jsonString) { ??? }
}
Closest I've Come:
public static class Serializer
{
public static string Serialize(object obj, DataContractJsonSerializerSettings settings=null)
{
if (obj == null) {
throw new NullReferenceException();
}
settings = settings ?? new DataContractJsonSerializerSettings();
DataContractJsonSerializer jsonizer = new DataContractJsonSerializer(obj.GetType(), settings);
string jsonString = null;
using ( MemoryStream stream = new MemoryStream() )
{
jsonizer.WriteObject(stream, obj);
stream.Position = 0;
StreamReader sr = new StreamReader(stream);
jsonString = sr.ReadToEnd();
}
return jsonString;
}
public static T Deserialize<T>(string jsonString)
{
DataContractJsonSerializer jsonizer = new DataContractJsonSerializer(typeof(T));
T obj;
using (Stream stream = GenerateStreamFromString(jsonString))
{
obj = (T)jsonizer.ReadObject(stream);
}
return obj;
}
private static Stream GenerateStreamFromString(string s)
{
MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
writer.Write(s);
writer.Flush();
stream.Position = 0;
return stream;
}
}
The Problem
The partial solution I posted works in simple cases. However, it fails when the subtype of the object being deserialized is difficult (or impossible) to determine from the json string. For instance,
IList<Animal> animals = new List<Animal>();
animals.add(new Dog("Woofy"));
animals.add(new Cat("Fluffy"));
string json = Serializer.Serialize(animals);
IList<Animal> result = Serializer.Deserialize<List<Animal>>(json);
// ^ fails because subtype information was lost during serialization
bool succeeded = result.get(0).Name.Equals("Woofy") && result.get(1).Name.Equals("Fluffy");
What I'm Looking For:
An implementation of the skeleton specified in "The Dream" which passes the driver specified in "The Problem". Comments welcome.