2

Is there any alternative to @JsonProperty to define json field name for deserialization at runtime (application startup)?

I considered using custom deserializer, but I don't want to read entire json to JsonNode, because some fields are complex Java objects. I want jackson to parse it in default way using all configured modules.

9
  • The @JsonProperty doesn't have to be annotated to the field itself. You could use it in a mix-in class. You could have multiple mix-in classes to switch between multiple versions of even generate a mix-in at runtime. Commented Jul 15, 2021 at 18:52
  • There is a @JsonIgnoreProperties that you can define to ignore unknown or specific values. Commented Jul 15, 2021 at 18:53
  • What would be the use case you have in mind? You usually know your POJO(s), if one of them mutates you define multiple sub-classes where the field varies, but what would be the case in which a field is determined at runtime? Maybe stating the need in the question would help to find a (different?) solution Commented Jul 15, 2021 at 18:53
  • You also have the option to deserialize the json payload into a Map<String, Object> which would give you a little more flexibility if you do not want a predefined POJO model Commented Jul 15, 2021 at 18:55
  • 1
    Take a look at AnnotationIntrospector class and findNameForDeserialization method. You can implement your own version where you read field name from a property file and return for required field. See examples: 1, 2 Commented Jul 15, 2021 at 22:49

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.