title: Custom JsonConverter self referencing loop
body: <p>Is there a way I can create my own Custom JsonConverter, which modifies the data before writing out the Json, that works with a nested parent-child structure? Whenever I try at the moment, I end up with a self referencing loop error. I've tried searching for solutions, but I can't quite find anything that matches.</p>
  
  <p>I've created a simple solution to demonstrate the problem.</p>
  
  <p>I have the following model and converter</p>
  
  <pre><code>public class NestedModel
  {
      public int Id { get; set; }
  
      public string Forename { get; set; }
  
      public string Surname { get; set; }
  
      public string Custom { get; set; }
  
  
      public List&lt;SimpleModel&gt; Children { get; set; }
  }
  
  public class NestedModelJsonConverter : JsonConverter
  {
  
      public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
      {
          var nestedModel = value as NestedModel;
          nestedModel.Custom = "Modified by Json Converter";
  
          //This causes a self referencing loop error
          serializer.Serialize(writer, value);
  
          //This resolves the self referencing loop error, but it does not call my custom Json Converter for any of the Children, and instead uses the default serialization
          //var jo = JObject.FromObject(value);
          //jo.WriteTo(writer);
      }
  
      public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
      {
          if (reader.TokenType == JsonToken.Null)
              return null;
  
          // Load JObject from stream
          JObject jObject = JObject.Load(reader);
  
          // Create target object based on JObject
          NestedModel target = new NestedModel();
  
          // Populate the object properties
          StringWriter writer = new StringWriter();
          serializer.Serialize(writer, jObject);
          using (JsonTextReader newReader = new JsonTextReader(new StringReader(writer.ToString())))
          {
              newReader.Culture = reader.Culture;
              newReader.DateParseHandling = reader.DateParseHandling;
              newReader.DateTimeZoneHandling = reader.DateTimeZoneHandling;
              newReader.FloatParseHandling = reader.FloatParseHandling;
              serializer.Populate(newReader, target);
          }
  
          return target;
      }
  
      public override bool CanRead { get { return true; } }
  
      public override bool CanWrite { get { return true; } }
  
      public override bool CanConvert(Type objectType) { return typeof(NestedModel).IsAssignableFrom(objectType); }
  }
  </code></pre>
  
  <p>and the test code to use it</p>
  
  <pre><code>string sourceJson = @"{
      ""Id"": 1,
      ""Forename"": ""John"",
      ""Surname"": ""Smith"",
      ""Children"":
      [
          {
              ""Id"": 2,
              ""Forename"": ""Joe"",
              ""Surname"": ""Bloggs"",
              ""Children"": null
          }
      ]
  }";
  
  var settings = new JsonSerializerSettings()
  {
      Converters = new List&lt;JsonConverter&gt;()
      {
          new NestedModelJsonConverter()
      },
      Formatting = Formatting.Indented
  };
  var nestedModel = JsonConvert.DeserializeObject&lt;NestedModel&gt;(sourceJson, settings);
  
  string outputJson = JsonConvert.SerializeObject(nestedModel, settings);
  </code></pre>
  
  <p>However when it tries to Write the Json, it gives a self referencing loop error, presumably when it tries to process the List of Children.
  I can prevent that error by using a JObject for the conversion, but then that prevents my custom converter from being used for the child elements. I want to have my custom WriteJson method fire for each level of that structure so that I can modify some data before writing it.</p>
  
  <p>Is there a way to do this and get around the self referencing loop error?</p>