lisak
lisak

Reputation: 21981

Converting only the first level of object tree using Jackson ObjectMapper

Is it possible to make ObjectMapper convert only the actual object without converting the rest of the object tree recursively ?

So that :

Map<String,Object> props = new ObjectMapper().convertValue(obj, Map.class);

results in a map of [field, value] where values are the actual references to instances of the fields of obj instead of Maps ?

Upvotes: 8

Views: 2851

Answers (2)

StaxMan
StaxMan

Reputation: 116522

I am not sure I understand what you are really trying to do here.

But one thing that may help is to keep in mind that java.lang.Object type (as well as JsonNode) can be freely included in the structure, to get sort of "untyped" binding deeper in the structure. With these types, you can avoid rigid data-binding for some subsets of the object model; and possibly convert to POJOs using ObjectMapper.convertValue() more dynamically.

Upvotes: 0

Pascal G&#233;linas
Pascal G&#233;linas

Reputation: 2824

There is no such feature right now with Jackson. You can probably achieve this with a custom Serializer/Deserializer pair that could share some data and "protocol". But, why bother doing this when the easier (and a LOT faster) way would be to have a generic way to go from POJO to Map, probably using reflection.

Upvotes: 1

Related Questions