-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Json (de)serialization configuration improvements #1573
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
Json (de)serialization configuration improvements #1573
Conversation
|
Thanks for the contribution, but this adds a lot of complexity as well as, perhaps, providing too many choices for how to configure the deserializer. Given the new mechanism to determine the type by calling a method I am not sure this really adds much, if any, value. Furthermore, when using the method mechanism, the polymorphism problem is solved with a single deserializer rather than making clones with a different JavaType (and it doesn't have to be a bean, Kafka can instantiate it via properties). @artembilan WDYT? |
|
Yep! I think method reference is much cleaner solution with less code to support. |
|
Thank you for looking at the code. Even with the method to determine target type when deserialization occurs, I still think there are things to consider here:
|
I don't follow - the type mapper is ignored when using a method call to determine the type.
Lines 468 to 473 in b2f8be6
You have a good point about generics when using the Serde with streams. I think a simpler solution for that is to add to the Serde. |
I was talking about
I accept your point about this being simpler, and I won't argue more - if you don't want that kind of copying, I can just replace last commit with one that does the cast. I do understand that maintaining this new code might fall on any of you, and nobody wants to maintain unnecessary code. |
|
OK; I get it now. However, I don't see how it will work when setting the "forced" type via properties since it's a simple class, not a If I am missing something, can you add a test for that use case? |
|
You are right, from config this might be useless. I thought that In this case I can either try to extend the type resolution for config to parse parameterized types (which might be tricky), or I can remove setting forced type by config property, what do you think would be better? |
|
I think just removing it is fine - that would be consistent with the deserializer only getting a For these "advanced" configurations, it's better to construct it as a bean and inject it into the producer factory. |
|
Very well, I removed the property configuration, will push with next change: In the light of above problem, what's your final decision on I might also suggest that the argument in public <T> JsonSerde<T> adjustType() {
return (JsonSerde<T>) this;
}In most cases, the generic parameter would be just inferred, so |
|
I am ok with keeping your |
7316ea2 to
a64ae01
Compare
|
Thanks for the contribution (and discussion), |
|
Thank you! |
This changes improve ability to configure
JsonSerdeas well asJsonSerializerandJsonDeserializer.First, allow
TypeReferenceandJavaTypewhen constructingJsonSerde. This can be helpful when using Serde for deserialization, but there are container types involved.Second, allow forcing type format when serializing. This is actually a problem that cannot be currently solved using
JsonSerializer: Lets assume theres a serialized type A, which is polymorphic and there is a list of objects of type A, possibly subtypes. Currently, the mechanism finds typed jackson serializer only for raw list, and serializes contents without configuration, so as simple beans, skipping properties that should be added. This can be seen in testJsonSerializationTests#testDeserializerTypeForcedTypewhich fails when not providing forced type toJsonSerializer.Lastly, this pull request adds ability to create copies of
JsonSerde,JsonSerializerandJsonDeserializerwith changed forced and default type. This change allows to create a single bean withJsonSerde<?>as actual type, configure its typeMapper, and other behaviors, and use this bean as a base for all the serdes used. Unfortunately, this couldn't be done with the fluent API, because generic parameter of type changes.There is a backward incompatibility in this changes - previously, when creating
JsonSerdewith specified class or type reference, this was only passed to deserialized as default type, and now is also passed to serializer as a forced type. This means that if user createdJsonSerdeinstance with specified target type, but used its serializer to serialize unrelated type, now this will fail, instead of serializing the data without type information. I don't think this is problematic: Either user didn't specify a type and serialized types based on header data or object mapper configuration, or they specified type and serialized objects of this type.In any case, I think specifing type for
JsonSerdeand using its serializer to serialize unrelated types could very well be a invalid configuration and probably should produce an error. If you don't agree, I can add backward compatiblity flag.