Given the following class:
public sealed class GorpCollection : IEnumerable<Gorp> {
private readonly IReadOnlyCollection<Gorp> _gorps;
public GorpCollection(string name, IEnumerable<Gorp> gorps) {
_gorps = gorps.ToList().AsReadOnly();
GorpType1 = _gorps.Where(gorp => gorp.GorpType == 1).ToList().AsReadOnly();
GorpType2 = _gorps.Where(gorp => gorp.GorpType == 2).ToList().AsReadOnly();
Name = name;
}
public string Name { get; }
public IReadOnlyCollection GorpType1 { get; }
public IReadOnlyCOllection GorpType2 { get; }
public IEnumerator<Gorp> GetEnumerator => _gorps.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable) _gorps).GetEnumerator();
}
I would like to serialize it as its concrete class to a JSON object, and not as an IEnumerable
which comes out as an array. I am content for the properties Name
, GorpType1
, and GorpType2
to be serialized.
What is the easiest way to do this with the least messing about with custom converters, binders, and settings? I would have thought a simple attribute was available such as [SerializeAs(typeof(GorpCollection))]
. But a fair amount of searching later, and I still don't know a simple way to get this done.