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.

gdomo
  • 1,650
  • 1
  • 9
  • 18
  • 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. – Robert Jul 15 '21 at 18:52
  • There is a `@JsonIgnoreProperties` that you can define to ignore unknown or specific values. – shinjw Jul 15 '21 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 – Matteo NNZ Jul 15 '21 at 18:53
  • You also have the option to deserialize the json payload into a Map which would give you a little more flexibility if you do not want a predefined POJO model – shinjw Jul 15 '21 at 18:55
  • @MatteoNNZ I want field name to be a application configuration parameter. Its value depends on environment (from test to prod) and should be available for change by support team without software update. Also, I have multiple sources of very similar jsons, that differs only in that field name. I don't want to write a separate class for each of them. – gdomo Jul 15 '21 at 19:07
  • @Robert There is no problem to annotate field itself, so annotating mix-in class instead doesn't make sense. Generation mix-in at runtime could be a solution, but I didn't found how to implement it. Modifying annotations via reflection is an ugly option, and possibly not works in my case – gdomo Jul 15 '21 at 19:18
  • Did you try using `JsonAnySetter`? Or can you try JsonPath, which is not how Jackson works, but it just gives you can option to parse values using a runtime value. – aksappy Jul 15 '21 at 19:46
  • @G.Domozhirov The main idea was that you can have multiple mix-ins (of course not activated all at the same time, only one at a time) for the same property which define different property names so you don't have to use the dirty way and modify annotations via reflection. – Robert Jul 15 '21 at 19:50
  • 1
    Take a look at `AnnotationIntrospector` class and [findNameForDeserialization](https://fasterxml.github.io/jackson-databind/javadoc/2.11/com/fasterxml/jackson/databind/AnnotationIntrospector.html#findNameForDeserialization-com.fasterxml.jackson.databind.introspect.Annotated-) method. You can implement your own version where you read field name from a property file and return for required `field`. See examples: [1](https://stackoverflow.com/a/62771448/51591), [2](https://stackoverflow.com/a/61286494/51591) – Michał Ziober Jul 15 '21 at 22:49

0 Answers0