簡體   English   中英

JavaScriptSerializer反序列化為嵌套對象

[英]JavaScriptSerializer deserializing to a nested object

我有一個包含三個參數的對象(稱為表達式):term1操作term2其中term1和term2是對象,而操作是字符串。 處理對象的邏輯可以處理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]”。

任何想法?

我的方法調用很簡單:

expression expr = deserialize<expression>(data);

您需要序列化有關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
        };
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM