Mapping a dynamic json object field in Jackson?

后端 未结 2 1288
野的像风
野的像风 2020-12-03 07:58

I have json objects in the following schema:

{
  name: \"foo\",
  timestamp: 1475840608763,
  payload:
  {
     foo: \"bar\"
  }
}

Here, th

2条回答
  •  [愿得一人]
    2020-12-03 08:18

    Does this help?

    import java.util.HashMap;
    import java.util.Map;
    
    import com.fasterxml.jackson.annotation.JsonAnyGetter;
    import com.fasterxml.jackson.annotation.JsonAnySetter;
    
    public class Payload {
    
        private final Map other = new HashMap<>();
    
        @JsonAnyGetter
        public Map any() {
            return other;
        }
    
        @JsonAnySetter
        public void set(final String name, final Object value) {
            other.put(name, value);
        }
    
        public Map getOther() {
            return other;
        }
    
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = (prime * result) + ((other == null) ? 0 : other.hashCode());
            return result;
        }
    
        @Override
        public boolean equals(final Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null) {
                return false;
            }
            if (!(obj instanceof Payload)) {
                return false;
            }
            Payload other = (Payload) obj;
            if (this.other == null) {
                if (other.other != null) {
                    return false;
                }
            } else if (!this.other.equals(other.other)) {
                return false;
            }
            return true;
        }
    
        @Override
        public String toString() {
            return "Payload [other=" + other + "]";
        }
    
    }
    

    Then this owning class

    import com.fasterxml.jackson.annotation.JsonCreator;
    import com.fasterxml.jackson.annotation.JsonProperty;
    
    public class Outer {
    
        private final String name;
        private final long timestamp;
    
        private final Payload payload;
    
        @JsonCreator
        public Outer(@JsonProperty("name") final String name, @JsonProperty("timestamp") final long timestamp, @JsonProperty("payload") final Payload payload) {
            super();
            this.name = name;
            this.timestamp = timestamp;
            this.payload = payload;
        }
    
        public String getName() {
            return name;
        }
    
        public long getTimestamp() {
            return timestamp;
        }
    
        public Payload getPayload() {
            return payload;
        }
    
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = (prime * result) + ((name == null) ? 0 : name.hashCode());
            result = (prime * result) + ((payload == null) ? 0 : payload.hashCode());
            result = (prime * result) + (int) (timestamp ^ (timestamp >>> 32));
            return result;
        }
    
        @Override
        public boolean equals(final Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null) {
                return false;
            }
            if (!(obj instanceof Outer)) {
                return false;
            }
            Outer other = (Outer) obj;
            if (name == null) {
                if (other.name != null) {
                    return false;
                }
            } else if (!name.equals(other.name)) {
                return false;
            }
            if (payload == null) {
                if (other.payload != null) {
                    return false;
                }
            } else if (!payload.equals(other.payload)) {
                return false;
            }
            if (timestamp != other.timestamp) {
                return false;
            }
            return true;
        }
    
        @Override
        public String toString() {
            return "Outer [name=" + name + ", timestamp=" + timestamp + ", payload=" + payload + "]";
        }
    
    }
    

    Then to test

    public class Main {
    
        private static final ObjectMapper mapper = new ObjectMapper();
    
        public static void main(final String... args) throws JsonParseException, JsonMappingException, IOException {
    
            final Outer outer = mapper.readValue(new File("test.json"), Outer.class);
    
            System.out.println(outer);
    
    
        }
    
    }
    

    Gives console output of

    Outer [name=foo, timestamp=1475840608763, payload=Payload [other={foo=bar}]]
    

提交回复
热议问题