在 Jackson 中映射动态 json 对象字段?

2022-09-02 09:46:51

我在以下架构中有json对象:

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

此处,该字段包含一个嵌入式 json 对象,并且此对象的架构是动态的,并且每次都不同。payload

对象是从不同的 API 服务以及不同 API 服务的不同方法获得的原始输出。无法将其映射到所有可能的值。payload

是否可以拥有如下所示的 Java 类:

public class Event
{
    public String name;
    public long timestamp;
    public JsonObject payload;
}

或者沿着这些路线的东西,所以我可以接收基本模式并处理它,然后将其发送到相关类,该类将转换为其适当的预期类?payload


答案 1

JsonNode

你可以使用 com.fasterxml.jackson.databind package 中的 JsonNode

public class Event {

    public String name;
    public long timestamp;
    public JsonNode payload;

    // Getters and setters
}

然后使用以下方法解析它:

String json = "{\"name\":\"foo\",\"timestamp\":1475840608763,"
            + "\"payload\":{\"foo\":\"bar\"}}";

ObjectMapper mapper = new ObjectMapper();
Event event = mapper.readValue(json, Event.class);

映射到 POJOJsonNode

例如,假设您要将 JsonNode 实例映射到以下类:

public class Payload {

    private String foo;

    // Getters and setters
}

这可以通过以下代码段实现:

Payload payload = mapper.treeToValue(event.getPayload(), Payload.class);

考虑Map<String, Object>

根据您的要求,您可以使用一个代替JsonNodeMap<String, Object>

public class Event {

    public String name;
    public long timestamp;
    public Map<String, Object> payload;

    // Getters and setters
}

如果需要将 a 转换为 POJO,请使用:Map<String, Object>

Payload payload = mapper.convertValue(event.getPayload(), Payload.class);

根据 Jackson 文档,该方法在功能上类似于首先将给定值序列化为 JSON,然后将 JSON 数据绑定到给定类型的值,但应该更有效,因为不会(需要)发生完全序列化。但是,将使用与数据绑定相同的转换器(序列化程序和解串器),这意味着相同的对象映射器配置工作正常。convertValue()


答案 2

这有帮助吗?

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<String, Object> other = new HashMap<>();

    @JsonAnyGetter
    public Map<String, Object> any() {
        return other;
    }

    @JsonAnySetter
    public void set(final String name, final Object value) {
        other.put(name, value);
    }

    public Map<String, Object> 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 + "]";
    }

}

那么这个拥有类

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 + "]";
    }

}

然后进行测试

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);


    }

}

提供控制台输出

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