如何在此数据模式中使用ReadAsAsync <t>?</t>

时间:2012-05-02 16:34:07

标签: c# rest dotnet-httpclient

我正在使用System.Net.Http.HttpClient,the version currently available in NuGet, 以json格式从服务检索数据。数据大致如下:

{
  "schema": "Listing",
  "data": {
    "key": "28ba648c-de24-45d4-a7d9-70f810cf5438",
    "children": [{
      "kind": "type1",
      "data": {
        "body": "Four score and seven years ago...",
        "parent_id": "2qh3l",
        "report_count": 0,
        "name": "c4j6yeh"
      }
    }, {
      "kind": "type3",
      "data": {
        "domain": "abc.def.com",
        "flagged": true,
        "category": "news",
        "saved": false,
        "id": "t3dz0",
        "created": 1335998011.0
        }
    }]
  }
}

我使用HttpContentExtensions.ReadAsAsync<T>将该json字符串反序列化为对象图。类型定义大致如下:

public class Response
{
    public String schema { get;set; }
    public ListingData data { get;set; }
}

public class ListingData
{
    public string key { get;set; }
    public List<OneItem> children { get;set; }
}

问题在于:我希望children中的项目类型根据kind属性而有所不同。如果kind是“type1”,那么我想反序列化一个对象...让我们称之为Type1。如果kind是“type3”,那么我想要一个Type3类型的对象。

现在,我可以反序列化List<Type1>List<Type3>,但我不知道如何告诉反序列化逻辑区分这两者。

我可以将“type1”数据对象和“type3”数据对象的所有属性合并为单个.NET类型。但是,属性的​​数量足够大,这会变得混乱。

如果JSON中的属性名称(在这种情况下为data)不同,我可以区分使用它。例如,如果数据如下所示:

    "children": [{
      "kind": "type1",
      "t1data": { ... }
    }, {
      "kind": "type3",
      "t3data": { ... }
    }]

...然后我可以在.NET中做这样的事情:

public class OneItem
{
    public string kind { get;set; }
    public Type1 t1data { get;set; }
    public Type3 t3data { get;set; }
}

但是我的数据架构看起来并不像那样。

是否可以根据数据内容选择反序列化的类型?换一种说法, 查看一个属性的值(在本例中为kind),以确定如何反序列化另一个属性的内容(在本例中为data)。

或者,在ReadAsAsync尝试反序列化之前,是否可以注入一个作用于JSON的过滤器或转换器?

如果是这样,怎么样?

1 个答案:

答案 0 :(得分:0)

如果你对你的回复进行一些预处理并且可以使用Json.NET,那么你应该可以做你想做的事。

鉴于以下类别:

public class Response
{
    public string schema
    {
        get;
        set;
    }

    public ListingData data
    {
        get;
        set;
    }
}

public class ListingData
{
    public string key
    {
        get;
        set;
    }

    public List<object> children
    {
        get;
        set;
    }
}

public class Type1
{
    public string body
    {
        get;
        set;
    }

    public string parent_id
    {
        get;
        set;
    }

    public int report_count
    {
        get;
        set;
    }

    public string name
    {
        get;
        set;
    }
}

public class Type3
{
    public string domain
    {
        get;
        set;
    }

    public bool flagged
    {
        get;
        set;
    }

    public string category
    {
        get;
        set;
    }

    public bool saved
    {
        get;
        set;
    }

    public string id
    {
        get;
        set;
    }

    public double created
    {
        get;
        set;
    }
}

此测试通过:

[Test]
public void RoundTrip()
{
    var response = new Response
                        {
                            schema = "Listing",
                            data = new ListingData
                                        {
                                            key = "28ba648c-de24-45d4-a7d9-70f810cf5438",
                                            children = new List<object>
                                                            {
                                                                new Type1
                                                                    {
                                                                        body = "Four score and seven years ago...",
                                                                        parent_id = "2qh3l",
                                                                        report_count = 0,
                                                                        name = "c4j6yeh"
                                                                    },
                                                                new Type3
                                                                    {
                                                                        domain = "abc.def.com",
                                                                        flagged = true,
                                                                        category = "news",
                                                                        saved = false,
                                                                        id = "t3dz0",
                                                                        created = 1335998011.0
                                                                    }
                                                            }
                                        }
                        };

    var jsonSerializerSettings = new JsonSerializerSettings
                                        {
                                            Formatting = Formatting.Indented,
                                            TypeNameHandling = TypeNameHandling.Objects
                                        };

    string serializedResponse = JsonConvert.SerializeObject(response, jsonSerializerSettings);
    Console.WriteLine(serializedResponse);
    var roundTrippedResponse = JsonConvert.DeserializeObject<Response>(serializedResponse, jsonSerializerSettings);
    Assert.That(roundTrippedResponse.data.children.First().GetType(), Is.EqualTo(typeof(Type1)));
    Assert.That(roundTrippedResponse.data.children.Last().GetType(), Is.EqualTo(typeof(Type3)));
}

写入控制台的输出是:

{
  "$type": "Test.Response, Test",
  "schema": "Listing",
  "data": {
    "$type": "Test.ListingData, Test",
    "key": "28ba648c-de24-45d4-a7d9-70f810cf5438",
    "children": [
      {
        "$type": "Test.Type1, Test",
        "body": "Four score and seven years ago...",
        "parent_id": "2qh3l",
        "report_count": 0,
        "name": "c4j6yeh"
      },
      {
        "$type": "Test.Type3, Test",
        "domain": "abc.def.com",
        "flagged": true,
        "category": "news",
        "saved": false,
        "id": "t3dz0",
        "created": 1335998011.0
      }
    ]
  }
}

因此,如果您可以将收到的响应转换为与Json.NET的预期格式相匹配,那么这将有效。

要将所有这些组合在一起,您需要编写自定义MediaTypeFormatter并将其传递给ReadAsAsync&lt;&gt;()调用。