title: NSwag customizing JSON output
body: <p>I have a class:</p>
  
  <pre><code>class Test {
    public IPAddress Ip {get; set; }
  }
  </code></pre>
  
  <p>By default, Nswag (<code>Nswag.AspNetCore</code> package) at <a href="http://localhost:1234/swagger" rel="nofollow noreferrer">http://localhost:1234/swagger</a> serializes it as so, in the preview "Example" output:</p>
  
  <pre><code>[    
    "Ip": {
       "AddressFamily": 0,
       "ScopeId": 0,
       "IsIPv6Multicast": true,
       "IsIPv6LinkLocal": true,
       "IsIPv6SiteLocal": true,
       "IsIPv6Teredo": true,
       "IsIPv4MappedToIPv6": true,
       "Address": 0
      }
  ]
  </code></pre>
  
  <p>I want to custimize it, so that it serializes just the Ip field as a <code>string</code> without all the other type properties of <code>IPAddress</code>.</p>
  
  <p>To do that, I created my own <code>JsonConverter</code>:</p>
  
  <pre><code>public class IPAddressConverter : JsonConverter
  { 
      public override bool CanConvert(Type objectType)
      {
          return (objectType == typeof(IPAddress));
      }
  
      public override void WriteJson(JsonWriter writer, object value,   JsonSerializer serializer)
      {
          writer.WriteValue(value.ToString());
      }
  
      public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
      { 
          return IPAddress.Parse((string)reader.Value);
      }
  }
  </code></pre>
  
  <p>And verified that it works with JSON.Net.</p>
  
  <pre><code>var settings = new JsonSerializerSettings() { Converters =  { new IPAddressConverter() } };
  JsonConvert.SerializeObject(IPAddress.Parse("123.123.123.123"), settings);
  JsonConvert.DeserializeObject&lt;IPAddress&gt;("123.123.123.123", settings);
  </code></pre>
  
  <p>Now I wanted to plug this into NSwag. I modified the NSwag settings like so inside ASP.Net's Startup.cs:</p>
  
  <pre><code>    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
      public void Configure(IApplicationBuilder app, IHostingEnvironment env)
      {
          if (env.IsDevelopment())
          {
              app.UseDeveloperExceptionPage();
          }
  
          app.UseMvc();
  
          var swaggerSettings = new SwaggerUi3Settings()
          { 
              ContractResolver = new CustomSwaggerContractResolver(),
              DefaultPropertyNameHandling = NJsonSchema.PropertyNameHandling.Default /* If this is not set to default it raises an exception when a ContractResolver is set and then swagger page does not load at all */
          };
  
          app.UseSwaggerUi3(typeof(Startup).Assembly, swaggerSettings);
   }
  </code></pre>
  
  <p>#</p>
  
  <pre><code>public class CustomSwaggerContractResolver : DefaultContractResolver
  {   
      protected override JsonContract CreateContract(Type objectType)
      {  
          JsonContract contract = base.CreateContract(objectType);
  
          if (objectType == typeof(IPAddress))
          {
              contract.Converter = new IPAddressConverter();
          }  
  
          return contract;
      }
  } 
  </code></pre>
  
  <p>Unfortunately when I browse to <a href="http://localhost:1234/swagger" rel="nofollow noreferrer">http://localhost:1234/swagger</a> to see the Json "Example" output of my <code>Test</code> class, it is still including all the properties of <code>IPAddress</code> instead of <code>Ip</code> field being serialized as a simple <code>string</code>.</p>
  
  <p>When I debug I can see that code inside <code>CustomSwaggerContractResolver</code> is called, and the converter is assigned, but the <code>IPAddressConverter</code> itself is never being called.</p>
  
  <p>Is there anything that I am missing?</p>
  
  <p>Thank you.</p>