0

How can I map:

user: {
    #user1: "http:www.example.com/user1"
  }

To a class?

I tried:

public static final class User {

    public final String user1;

    @JsonCreator
    public User(@JsonProperty("#user1") String user1) {
          this.user1 = user1;
    }
}

The problem I ran into was that #user1 changes. It might be #user1 or #user2 or contain multiple #user1, #user2, etc simultaneously.

This will result in:

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "#user2"

  • 1
    If the fields can change you might be better off mapping that to maps and arrays e.g. like json-simple does it. – Thomas Jul 09 '15 at 13:29
  • Btw, what you you mean with "multiple `#user1`"? Would it be an array with that name that contains multiple strings, e.g. like `#user1: ["a", "b"]`? – Thomas Jul 09 '15 at 13:31
  • @Thomas: Thanks. Swapped out the class for a `HashMap` and it worked. It would be more like `user: { #user1: "abc", #user2: "xyz" }` –  Jul 09 '15 at 13:32
  • I see, that was a misunderstanding. In that case I'd settle for the list/array named `user` instead of optionally allowing for `#user1` etc. which seems like bad design anyways. – Thomas Jul 09 '15 at 13:36

0 Answers0