Serialization a class which inherited from Collection

Jan 12, 2010 at 3:32 PM

I'd like to serialization a class inherited from Collection and save both the items and the ActivePageIndex, if I use JsonArrayAttribute, I only got the items saved, if I use JsonObjectAttribute, I only got string "(Collection)".

Is there anyway to do it?

[JsonObject(MemberSerialization.OptIn)]
public class Pages : ObservableCollection<String>
{
//other properties...
[JsonProperty]
public int ActivePageIndex;
//other properties...
}

Coordinator
Jan 12, 2010 at 10:05 PM

You can't have both. A collection is serialized as an array so there would be nowhere to put the property values because there are no properties on an array.

Jan 12, 2010 at 10:28 PM

but it's strange that serialization got "(Collection)" when using [JsonObject(MemberSerialization.OptIn)].

why not serialize it as a normal class when [JsonObject(MemberSerialization.OptIn)] ?

Coordinator
Jan 12, 2010 at 11:39 PM

Hmmm I suspect it might be caused by a TypeConverter associated with ObservableCollection<T>