1

Say I have some video POCO with a description property which contains some text followed by tags in the form of [foo,bar]. I would like to extract the tags from the description, remove it from the description and add the tags to the Tags property.

I just want to custom deserialize the Description property in a custom JsonConverter and let Json.Net deserialize the other properties as usual. I tried calling serializer.Deserialize<MyVideo>(reader) but this will trigger my converter again, creating a recursive loop.

So is it possible to just handle the Description and Tags properties myself and let Json.Net deserialize everything else somehow?

Sample of the MyVideo POCO:

[JsonConverter(typeof(VideoDescriptionConverter))]
public class MyVideo
{
   [JsonProperty("description")]
   public string Description { get; set; }

   public IList<string> Tags { get; set; }

   // <more properties>
}

My VideoDescriptionConverter.ReadJson method:

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
   // This will recursively loop so doesn't work:
   var video = serializer.Deserialize<MyVideo>(reader);
   //video.Description = <extract description and tags>
   //video.Tags = ...
   return video;
}
Kapé
  • 4,411
  • 3
  • 37
  • 54
  • Is this a duplicate of [Custom deserializer only for some fields with json.NET](http://stackoverflow.com/q/41510242/3744182)? – dbc Mar 03 '17 at 14:09
  • @dbc Thanks, didn't see that one, looks like I just needed to call `serializer.Populate` instead. – Kapé Mar 07 '17 at 09:41

0 Answers0