The project is an Asp.Net Web API web service.
I have a type hierarchy that I need to be able to serialize to and from Json, so I have taken the code from this SO: H
I just came across this myself and I was pulling my hair out in frustration!
To solve the issue, the following worked for me, but because I missed the CanWrite solution, it's a more complex workaround.
JsonConverter attribute on the copy.WriteJson method, convert the value into your dummy type, then serialise that type instead.For instance, this is similar to my original class:
[JsonConverter(typeof(MyResponseConverter))]
public class MyResponse
{
public ResponseBlog blog { get; set; }
public Post[] posts { get; set; }
}
The copy looks like this:
public class FakeMyResponse
{
public ResponseBlog blog { get; set; }
public Post[] posts { get; set; }
public FakeMyResponse(MyResponse response)
{
blog = response.blog;
posts = response.posts;
}
}
The WriteJson is:
public override void WriteJson(JsonWriter writer, object value,
JsonSerializer serializer)
{
if (CanConvert(value.GetType()))
{
FakeMyResponse response = new FakeMyResponse((MyResponse)value);
serializer.Serialize(writer, response);
}
}
Edit:
The OP pointed out that using an Expando could be another possible solution. This works well, saving the bother of creating the new class, although DLR support requires Framework 4.0 or later. The approach is to create a new dynamic ExpandoObject and then initialise its properties in the WriteJson method directly to create the copy, e.g.:
public override void WriteJson(JsonWriter writer, object value,
JsonSerializer serializer)
{
if (CanConvert(value.GetType()))
{
var response = (MyResponse)value;
dynamic fake = new System.Dynamic.ExpandoObject();
fake.blog = response.blog;
fake.posts = response.posts;
serializer.Serialize(writer, fake);
}
}