jhlee8804
2/7/2018 - 7:40 AM

JsonConverter for BigInt to string

/// <summary>
/// long으로 선언된 필드를 json으로 직렬화할 때 string 타입으로 강제 치환한다.
/// </summary>
public class BigIntToStringConverter : JsonConverter
{
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        JToken jt = JValue.ReadFrom(reader);
        return jt.Value<long>();
    }

    public override bool CanConvert(Type objectType)
    {
        return typeof(Int64).Equals(objectType);
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        serializer.Serialize(writer, value.ToString());
    }
}