You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Allow classes to be marked as serializable without needing another class as the serializer.
Is this similar to #594?
Feature description
It would work similarly to Java's Comparable interface, so you wouldn't need a serializer in another class.
Currently, you need to either add a TypeAdapter as an external class through @JsonAdapter or GsonBuilder, but it would be much more convenient if the instance (of the object we're serializing) could serialize itself.
The implementation could look a little something like this (not the best code, triple nested anonymous classes my beloved) (I code in 4 spaces so the formatting may be a bit wonk):
To me it sounds as if this is the same feature request. The only difference might be that the other feature request also requests this for deserialization.
you need to either add a TypeAdapter as an external class through @JsonAdapter
Note that the adapter class can be a nested static class (so that it can access private fields of the enclosing class), but when referencing it in @JsonAdapter you might have to qualify its name with the name of the enclosing class. For example something like:
The following are just some personal thoughts on this:
Such a serialize method should probably use a JsonWriter parameter though instead of JsonElement as return type for better performance. And maybe it would also need a Gson parameter to allow looking up other adapters, e.g. to serialize field values.
If you also want to support deserialization, then you might also want to put the implementation in the same class so that both serialization and deserialization are close together.
However then the deserialization would probably happen through a static method invoked using reflection. So there is no compile-time verification that the method name and signature is correct.
Or if a non-static method is used, Gson would first have to create an instance with default values and then call the deserialize method on it. But then it cannot set any final fields.
Unlike TypeAdapterFactory whose created type adapters are cached by Gson, it seems here there is no way to cache anything for serialization. So the serialize method would have to look up delegate adapters every time it is called (assuming the serialize method has a Gson parameter, as mentioned above).
This might not cause a big overhead though.
So personally I am not completely sure if that would really provide such a big advantage over using @JsonAdapter on a class.
Problem solved by the feature
Allow classes to be marked as serializable without needing another class as the serializer.
Is this similar to #594?
Feature description
It would work similarly to Java's
Comparable
interface, so you wouldn't need a serializer in another class.Currently, you need to either add a TypeAdapter as an external class through
@JsonAdapter
orGsonBuilder
, but it would be much more convenient if the instance (of the object we're serializing) could serialize itself.The implementation could look a little something like this (not the best code, triple nested anonymous classes my beloved) (I code in 4 spaces so the formatting may be a bit wonk):
Alternatives / workarounds
Implement the serializer or TypeAdapter in another class.
The text was updated successfully, but these errors were encountered: