使用ServiceStack.Text序列化ExpandoObject

时间:2012-07-03 14:27:37

标签: c# .net json servicestack

我正在尝试使用库ServiceStack.Text序列化对象。这工作

using System.Dynamic;
using ServiceStack.Text;
var x = new {Value= 10, Product = "Apples"};                
Console.WriteLine(JsonSerializer.SerializeToString(x));

我得到了,正如我所期待的那样

{"Value":10,"Product":"Apples"}

然而

dynamic x = new ExpandoObject();
x.Value = 100;
x.Product = "Apples";
Console.WriteLine(JsonSerializer.SerializeToString(x));

我很惊讶

[{"Key":"Value","Value":100},{"Key":"Product","Value":"Apples"}]

为什么!发生了什么事?

其次我怎样才能得到我想要的东西?

1 个答案:

答案 0 :(得分:5)

ExpandoObject实施IConnection<KeyValuePair>IEnumerable<KeyValuePair>

public sealed class ExpandoObject :
    IDynamicMetaObjectProvider,
    IDictionary<string, object>,
    ICollection<KeyValuePair<string, object>>,
    IEnumerable<KeyValuePair<string, object>>,
    IEnumerable, INotifyPropertyChanged

我的猜测是,在内部,ServiceStack序列化程序将ExpandoObject视为IEnumerable<KeyValuePair>,因此它序列化为键/值对的JSON数组。


这与您的第一个(工作)代码段不同,因为.NET实际上为您的数据构建了一个真实的(匿名)类,基本上它是:

public class SomeNameTheCompilerMakesUp {
    internal int Value { get; set; }
    internal string Product { get; set; }
}

为你自动,所以当它被发送到序列化程序时,它正在使用具有真实属性的真实类,而ExpandoObject在内部确实由object[]支持。


另一方面,微软System.Web.Helpers.Json的行为方式相同。该测试通过:

    [TestMethod]
    public void ExpandoObjectSerializesToJsonArray()
    {
        dynamic anonType = new { Value = 10, Product = "Apples" };

        dynamic expando = new ExpandoObject();
        expando.Value = 10;
        expando.Product = "Apples";

        var anonResult = System.Web.Helpers.Json.Encode(anonType);
        var expandoResult = System.Web.Helpers.Json.Encode(expando);

        Assert.AreEqual("{\"Value\":10,\"Product\":\"Apples\"}", anonResult);
        Assert.AreEqual("[{\"Key\":\"Value\",\"Value\":10},{\"Key\":\"Product\",\"Value\":\"Apples\"}]", expandoResult);
    }

最后一次修改:

您可以将ExpandoObject变为Dictionary<string, object>,以此方式完成此工作。对此代码的警告是它将数据复制到字典中,因此您在内存中有2个副本(或者稍微少于,因为从技术上讲,字符串可能被实现)。

    [TestMethod]
    public void TestMethod1()
    {
        dynamic expando = new ExpandoObject();
        expando.Value = 10;
        expando.Product = "Apples";

        // copy expando properties to dictionary
        var dictionary = ((ExpandoObject)expando).ToDictionary(x => x.Key, x => x.Value);

        var expandoResult    = System.Web.Helpers.Json.Encode(expando);
        var dictionaryResult = System.Web.Helpers.Json.Encode(dictionary);

        Assert.AreEqual("[{\"Key\":\"Value\",\"Value\":10},{\"Key\":\"Product\",\"Value\":\"Apples\"}]", expandoResult);
        Assert.AreEqual("{\"Value\":10,\"Product\":\"Apples\"}", dictionaryResult);
    }

虽然对于之后遇到这种情况并且实际上正在使用System.Web.Helpers.Json的任何人来说,最好的办法就是将ExpandoObject包裹在这样的DynamicJsonObject中:

    [TestMethod]
    public void TestMethod1()
    {
        dynamic expando = new ExpandoObject();
        expando.Value = 10;
        expando.Product = "Apples";

        var dictionaryResult = System.Web.Helpers.Json.Encode(new DynamicJsonObject(expando));

        Assert.AreEqual("{\"Value\":10,\"Product\":\"Apples\"}", dictionaryResult);
    }

Ans一旦我完成了这个,我在这里找到了一个类似的问题:How to flatten an ExpandoObject returned via JsonResult in asp.net mvc?