JavaScriptSerializer反序列化为嵌套对象

时间:2014-05-12 00:12:03

标签: c# javascript serialization deserialization

我有一个包含三个参数的对象(称为表达式):term1 operation term2其中term1和term2是对象,operation是一个字符串。处理对象的逻辑可以处理term1或term2是字符串或递归场景,其中它们包含嵌入式表达式。

[DataContract] public class expression
{
    [DataMember] public object term1 { get; set; }
    [DataMember] public string operation { get; set; }
    [DataMember] public object term2 { get; set; }
}

当我尝试对term1 / term2为简单字符串的对象进行反序列化时,它的效果很好,即

{ term1: "foo", operation: "=", term2: "bar" }

将使用C#JavaScriptSerializer和以下代码正确反序列化:

    public T deserialize_json<T>(string json_string)
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        return (T)serializer.Deserialize<T>(json_string);
    }

但是,当我尝试提交嵌套对象时,即:

{ 
  term1: 
  { 
    term1: "foo", operation: "=", term2: "bar" 
  }, 
  operation: "or", 
  term2: 
  { 
    term1: "foo", operation: "equals", term2: "baz" 
  } 
}

JavaScriptSerializer会将term1和term2反序列化为'System.Collections.Generic.Dictionary 2 [System.String,System.Object]&#39;`

有什么想法吗?

我的方法调用很简单:

expression expr = deserialize<expression>(data);

1 个答案:

答案 0 :(得分:1)

您需要序列化有关term1和term2类型的信息。为此,您可以使用Newtonsoft.Json库。

试试这段代码:

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
        };
    }
}