New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
De-/Serialization for sub-types without "type" property #13
Comments
Hi @sir-boformer , Thanks for giving your improvement idea ! It seems to me that having to specify Note that this type field also helps on the deserializing side when no subtype match have been found, then it's possible to know which was the type. What do you think ? |
I don't get why it helps on the deserializing side. There would still be the JSON type property which can be logged when something goes wrong. The Actually I created this issue because the GSON Java library supports sub-classes without extra "type" fields. Here is how the converter registration works in GSON: // adding all different container classes with their flag
final RuntimeTypeAdapterFactory<AbstractContainer> typeFactory = RuntimeTypeAdapterFactory
.of(Animal.class, "type") // Here you specify which is the parent class and what field particularizes the child class.
.registerSubtype(Dog.class, "dog") // if the flag equals the class name, you can skip the second parameter. This is only necessary, when the "type" field does not equal the class name.
.registerSubtype(Cat.class, "cat");
// add the polymorphic specialization
final Gson gson = new GsonBuilder().registerTypeAdapterFactory(typeFactory).create(); Basically all that is needed is a way to configure JsonSubTypes without attributes, and the serialization logic that adds the property to JSON (serialization would only be enabled when configured without attributes). After all I can live with the extra property. It was just a suggestion ;) |
Base on the proposal of @boformer I might have a solution draft... Could you tell me if it might meet your needs ? P.S.: More in depth testing needs to be done and naming does not sound really good. |
That's actually brilliant, nice work! I think it just needs a shorter name 😄 |
Maybe just |
I've added the failing test in the branch that is related to #16 |
Released in v1.3.0 with #28 |
Awesome! Thank you! |
I would like to avoid having an extra "type" property in my sub-types.
Motivation: It is cleaner to avoid the extra property (avoid duplicate information)
Deserializing is working perfectly:
Serialization not so much:
It seems like it would be necessary to annotate all sub-types with
[JsonConverter]
, and it would require reflection to get the parent type that contains the type mapping.The text was updated successfully, but these errors were encountered: