Yes, TypeAdapter is more performant and is typically the better
solution, but that will make delegation more difficult. That is, the
underlying Gson instance that you use would do
jsonWriter.beginObject(), [write some fields], jsonWriter.endObject().
This means that you won't be able to arbitrarily insert your own
field->Json mappings into the resulting object as you can with my
approach. As with many decisions in programming, it's a tradeoff
between programmer time and performance.
As for your deserialization question, whatever special logic you use
to serialize customField will need to be performed in reverse to set
the value on deserialization. If you don't mind the less performant
approach, you can use JsonDeserializer, remove the custom field from
the json that you receive, delegate to the internal Gson instance, and
then set customField manually.
If you want to use the TypeAdapter, I can't come up with a good way to
delegate to an internal Gson in this case. You'd just have to do the
whole de/serialization yourself, which seemed to be what you were
trying to avoid.
In regards to the TypeAdapterFactory question, you really shouldn't
have to use a factory unless you're doing really reflective,
high-level things. Most of the time, you can just create the
JsonDeserializer, JsonSerializer, and TypeAdapter instances that you
need and register each one mapped to its appropriate class.
> --
> You received this message because you are subscribed to the Google Groups
> "google-gson" group.
> To view this discussion on the web visit
>
https://groups.google.com/d/msg/google-gson/-/Q_gMVCU9ly0J.