I have hundreds of existing POCOs that I'm currently using with Entity Framework. A new requirement is that I need to be able to serialize these objects and cache them in Redis. I decided to serialize to JSON using Json.NET but it won't serialize all of my objects. See the example below.
public class Envelope<T>
{
public string Metadata{ get; set; }
public T Value { get; set; }
}
public class MyClass {
public string MyProperty { get; set; }
}
public void Serialize()
{
Envelope<List<MyClass>> envelope = new Envelope<List<MyClass>>();
envelope.Metadata = "Some Metadata";
envelope.Value.Add(new MyClass { MyProperty = "Some Property Data" });
string json = JsonConvert.Serialize(envelope);
}
When this data structure is serialized, everything looks good except MyClass.MyProperty is ignored. The List<MyClass> becomes a JSON array with 1 element but that element has no properties. If I add [DataContract] and [DataMember] to MyClass, then they are serialized correctly. I shouldn't have to do this, right? I didn't have to put those attributes on the Envelope<T> class. I'd rather not have to go through my hundred of POCOs and add these attributes.