Currently I'm trying to parse a json object in Java using gson. But I am not familiar with Java that much but I had done this same project on C# some months ago. I need to serialize a field using a custom JosnConverter
. The code I used in my c# project.
public class ValueConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteValue(value.ToString());
writer.Flush();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override bool CanConvert(Type objectType)
{
return true;
}
}
And I define the field like
[JsonProperty("id"), JsonConverter(typeof(ValueStringConverter))]
public long Id { get; set; }
How do I achieve the same thing using Gson? I have tried to look at TypeAdapter
but I am not sure that is what I need or not.