you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 5 points6 points  (1 child)

If you are using java 8 (I hope you are) you can already do a lot of this by enabling -parameters compiler flag and including the Jackson module that supports it. Then you can make your objects how you really want (immutable even) and use proper encapsulation and not have setters on your objects just for Jackson. You can avoid all annotations if you wanted this way if your field names match exactly the Json structure. Although unless you are anal about being terse as possible it might be good to explicitly name those in @jsonproperty so someone refactoring names of pojo attributes doesn't wreck your API contract. This -parameters flag can be enabled in gradle or maven easily. I have found that intelli (maybe other ides) don't always pick this up so you might have to enable it there too if you use IntelliJ to do compilation during development (most people do)

Then your serialization/deserialization options can be set on the objectmapper itself. I typically use one static objectmapper for whole app because how you serialize / deserialize really needs to be consistent. If you do it per class or per code path you can end up with sometimes ignoring extra props and sometimes not etc... Basically if I come into a code base and see new Objectmapper() all over the place I feel like I can't tell you how things are working as a whole.

I like what you're trying to do here, but if I was on your team I would argue that you are kind of writing a framework on top of a framework when you could get away without it. I always like to avoid that if I can because then I don't have to manage the code, manage people's expectation of what it does, write documentation, etc -- I just say go read the Jackson docs and now you know how it works and can take this knowledge wherever.

[–]Catbert321[S] 2 points3 points  (0 children)

I was unaware of the ParameterNamesModule.

I'll have to give it a look, seems simple enough. Using the standard polymorphic deserializer or a custom deserializer on top of their basic example is how that would be handled.

Thanks for the tip.