Reputation: 2761
I have an object (called an expression) that contains three parameters: term1 operation term2 where term1 and term2 are objects and operation is a string. The logic handling the object can handle term1 or term2 being a string or recursive scenarios where they contain an embedded expression.
i.e.
[DataContract] public class expression
{
[DataMember] public object term1 { get; set; }
[DataMember] public string operation { get; set; }
[DataMember] public object term2 { get; set; }
}
When I attempt to deserialize an object where term1/term2 are simple strings, it works great, i.e.
{ term1: "foo", operation: "=", term2: "bar" }
will deserialize correctly using the C# JavaScriptSerializer and the following code:
public T deserialize_json<T>(string json_string)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
return (T)serializer.Deserialize<T>(json_string);
}
However, when I attempt to submit a nested object, i.e.:
{
term1:
{
term1: "foo", operation: "=", term2: "bar"
},
operation: "or",
term2:
{
term1: "foo", operation: "equals", term2: "baz"
}
}
The JavaScriptSerializer will deserialize term1 and term2 into 'System.Collections.Generic.Dictionary
2[System.String,System.Object]'`
Any idea?
My method call is simple:
expression expr = deserialize<expression>(data);
Upvotes: 0
Views: 1218
Reputation: 300
You need to serialize information about type of term1 and term2. For that you can use Newtonsoft.Json library.
Try this code:
expression model = new expression() { term1 = new expression() { operation = " + " } };
string content = ModelManager<expression>.SaveToString(model);
/* content =
{
"$type": "Test.expression, Test",
"term1": {
"$type": "Test.expression, Test",
"operation": " + "
}
}
*/
var model2 = ModelManager<expression>.LoadFromString(content);
string content2 = ModelManager<expression>.SaveToString(model2);
// content == content2
public static class ModelManager<T>
{
public static T LoadFromString(string content)
{
using (var sr = new StringReader(content))
using (var jr = new JsonTextReader(sr))
return GetSerializer().Deserialize<T>(jr);
}
public static string SaveToString(T model)
{
StringBuilder sb = new StringBuilder(512);
using (var sw = new StringWriter(sb, System.Globalization.CultureInfo.InvariantCulture))
using (var jtw = new JsonTextWriter(sw))
GetSerializer().Serialize(jtw, model);
return sb.ToString();
}
private static Newtonsoft.Json.JsonSerializer GetSerializer()
{
return new Newtonsoft.Json.JsonSerializer()
{
NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore,
DefaultValueHandling = Newtonsoft.Json.DefaultValueHandling.Include,
TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Objects,
TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple,
Formatting = Formatting.Indented,
MaxDepth = null,
ReferenceLoopHandling = ReferenceLoopHandling.Error,
PreserveReferencesHandling = PreserveReferencesHandling.None
};
}
}
Upvotes: 1