How do I get json.net to serialize members of a class deriving from List<T>?
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).
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.
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)
{
JObject jo = JObject.Load(reader);
PagedResult<T> result = new PagedResult<T>();
result.PageSize = (int)jo["PageSize"];
result.PageIndex = (int)jo["PageIndex"];
result.TotalItems = (int)jo["TotalItems"];
result.TotalPages = (int)jo["TotalPages"];
result.AddRange(jo["Items"].ToObject<T[]>(serializer));
return result;
}
}
(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);
}
}
Output:
{
"PageSize": 10,
"PageIndex": 0,
"TotalItems": 3,
"TotalPages": 1,
"Items": [
"foo",
"bar",
"baz"
]
}
The simplest workaround that comes to my mind is to expose internal elements as another property of derived class:
[JsonProperty]
public IEnumerable<T> Elements {
get
{
return this;
}
}