Created
September 13, 2018 21:47
-
-
Save aaronhoffman/de40ac508de95101fa41859195728e39 to your computer and use it in GitHub Desktop.
Newtonsoft.Json BaseJsonConverter example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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