0

We have a relatively big json document but just need to the special fields of it. So, I defined the needed properties in my model and can serialize/deserialize, well. Recently we are forced to hand over the json document as we received, but since there are so much fields and these fields vary frequently, I'm looking for a method to do this without defining extra fields in the model. I couldn't do it by using an extra Map<String, Object> in the model classes but I guess there should be an solution for it.
Let's suppose my model is like this:

public class Model {
    public A a;
}
public class A {
    public int i;
}

and the json is like this:

{
  "a": {
    "i": "1",
    "j": {
      "q": 2.11,
      "e": "94300000",
      "r": "book",
      "t": null
    },
    "b": 1
  },
  "ff": [
    {
      "gg": "ttttttttt",
      "dd": "oooooooo",
      "jj": null,
      "kk": [
        "pppppppp"
      ]
    },
    {
      "mm": null
    }
  ]
}

How can I do it?

user2041057
  • 69
  • 2
  • 10
  • 1
    Try to use `JsonAnySetter` annotation. You can accept and add to map only important fields on level of class `Model` and/or `A`. Take a look at [How to serialize ANY Object into a URI?](https://stackoverflow.com/questions/18408599/how-to-serialize-any-object-into-a-uri), [Map JSON key-value to class in java](https://stackoverflow.com/questions/63994359/map-json-key-value-to-class-in-java), [Deserialize duplicate keys to list using Jackson](https://stackoverflow.com/questions/61528937/deserialize-duplicate-keys-to-list-using-jackson) – Michał Ziober Nov 28 '20 at 22:39
  • @MichałZiober Thank you, I found also [this](https://stackoverflow.com/questions/39156293/jackson-deserialize-extra-fields-as-map/40926692#40926692). – user2041057 Nov 29 '20 at 14:13

0 Answers0