如何获得json.net序列化类从列表&LT派生的成员; T&GT ;? [英] How do I get json.net to serialize members of a class deriving from List<T>?
问题描述
我创建了一个类 PagedResult&LT; T&GT; :列表&LT; T&GT;
包含了一些添加的成员,以便与我们的组件中的一个工作。然而,当我运行JSON解串器,只序列化列表。如果我用标记 [JSONObject的]
和 [JsonProperty]
那么它只会序列化的成员在派生类派生类和不列表。我如何获得这两个?
I created a class PagedResult<T> : List<T>
that contains a few added members in order to work with one of our components. However, when I run json deserializer, it only serializes the list. If I markup the derived class with [JsonObject]
and [JsonProperty]
then it'll only serialize the members of the derived class and not the list. How do I get both?
推荐答案
在默认情况下,Json.Net将把任何类,它实现的IEnumerable
作为数组。您可以通过装饰用类[的JSONObject]
属性覆盖此行为,但随后只有对象属性将被序列化,正如你所看到的。因为它不是通过公共财产(更确切地说,它是通过的GetEnumerator()
方法暴露)暴露列表本身不会得到序列化。
By default, Json.Net will treat any class that implements IEnumerable
as an array. You can override this behavior by decorating the class with a [JsonObject]
attribute, but then only the object properties will get serialized, as you have seen. The list itself will not get serialized because it is not exposed via a public property (rather, it is exposed via the GetEnumerator()
method).
如果你想同时,您可以做的@Konrad曾建议并在你的派生类提供的公共属性揭露列表,或者你可以写一个自定义的 JsonConverter
连载整个事情,你认为合适。后一种方法的一个例子如下:
If you want both, you can either do as @Konrad has suggested and provide a public property on your derived class to expose the list, or you can write a custom JsonConverter
to serialize the whole thing as you see fit. An example of the latter approach follows.
假设你的 PagedResult&LT; T&GT;
类看起来是这样的:
Assuming that your PagedResult<T>
class looks something like this:
class PagedResult<T> : List<T>
{
public int PageSize { get; set; }
public int PageIndex { get; set; }
public int TotalItems { get; set; }
public int TotalPages { get; set; }
}
您可以做一个转换器,它是这样的:
You can make a converter for it like this:
class PagedResultConverter<T> : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(PagedResult<T>));
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
PagedResult<T> result = (PagedResult<T>)value;
JObject jo = new JObject();
jo.Add("PageSize", result.PageSize);
jo.Add("PageIndex", result.PageIndex);
jo.Add("TotalItems", result.TotalItems);
jo.Add("TotalPages", result.TotalPages);
jo.Add("Items", JArray.FromObject(result.ToArray(), serializer));
jo.WriteTo(writer);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
(另请注意, [JSONObject的]
和 [JsonProperty]
属性不是必需使用这种方法,因为什么序列化的知识被封装到转换器类。)
(Notice also that the [JsonObject]
and [JsonProperty]
attributes are not required with this approach, because the knowledge of what to serialize is encapsulated into the converter class.)
下面是显示在行动转换器演示:
Here is a demo showing the converter in action:
class Program
{
static void Main(string[] args)
{
PagedResult<string> result = new PagedResult<string> { "foo", "bar", "baz" };
result.PageIndex = 0;
result.PageSize = 10;
result.TotalItems = 3;
result.TotalPages = 1;
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.Converters.Add(new PagedResultConverter<string>());
settings.Formatting = Formatting.Indented;
string json = JsonConvert.SerializeObject(result, settings);
Console.WriteLine(json);
}
}
输出:
{
"PageSize": 10,
"PageIndex": 0,
"TotalItems": 3,
"TotalPages": 1,
"Items": [
"foo",
"bar",
"baz"
]
}
这篇关于如何获得json.net序列化类从列表&LT派生的成员; T&GT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!