java - Jackson: Serializing enums as interfaces -


(disclaimer: extreme oversimplification. actual scenario considerably more complex.)

say have 2 systems, producer , consumer. code independent, aside single shared interface:

public interface thing {     string getname();     string getdescription();     int getprice(); } 

the idea producer creates bunch of data , sends json on http. producer has bunch of implementations of thing, each additional pieces of metadata , stuff required in data generation process.

as it's undesirable producer have kind of knowledge of jackson/serialization aside thin layer @ top, serialization attributes should kept out of thing implementations. due amount of implementation being grow in future, having mixins of them becomes unsustainable. believed sufficient apply annotations thing interface itself.

the first simple approach @jsonserialize annotation on interface. @ first, seemed work, resulted in problem. of implementations of thing enums, resulting in jackson serializing them name instead of fields defined in interface.

some googling revealed following annotation:

@jsonformat(shape= jsonformat.shape.object) 

while did indeed solve problem serializing fields instead of name, did too began serializing implementation-specific public fields not defined in thing interface, resulting not in information leak, failed deserialization in consumer due data containing unknown entries.

as further googling didn't yield results, solution can think of marking fields ignorable, extremely undesirable due mentioned reasons.

is there way, altering interface , annotations, enforce fields, no more, no less, should serialized both when comes classes , enums?

i have issue when working jackson. deserialization fails because, during deserialization, jackson unable find polymorphic reference type.

you should annotating interface @jsontypeinfo.

something like:

@jsontypeinfo(use = jsontypeinfo.id.class, include = jsontypeinfo.as.property, property = "class") 

there isn't of code in question , hence answer.


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -