Skip to content

Instantly share code, notes, and snippets.

@aaronhoffman
Created September 13, 2018 21:47
Show Gist options
  • Save aaronhoffman/de40ac508de95101fa41859195728e39 to your computer and use it in GitHub Desktop.
Save aaronhoffman/de40ac508de95101fa41859195728e39 to your computer and use it in GitHub Desktop.
Newtonsoft.Json BaseJsonConverter example
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public class TrivialJsonConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return true;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var jt = JToken.Load(reader);
return jt.ToObject(objectType);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var jt = JToken.FromObject(value);
jt.WriteTo(writer);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment