Jackson JSON:将属性的 Map<String,Object> 解编回其原始 class
Jackson JSON: Unmarshalling a Map<String,Object> of attributes back into its original class
我正在使用第三方库(准确地说是 Elasticsearch 2.3.3),它已经为我将给定的 JSON 结构解析为 Map<String,Object>
实例,我想要创建对象的 class 实例,该对象最初编组为 JSON:
- 编组:
MarshallableObject
→ marshalled_object.json
- 解组:
marshalled_object.json
→ Map<String,Object>
→ MarshallableObject
)
此外,这里是一些说明逻辑的示例代码:
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Stream;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.QueryBuilders;
import com.fasterxml.jackson.databind.ObjectMapper;
public final class AttrMapUnmarshallingTest {
/**
* This class was directly used in the process for creating JSON documents indexed by Elasticsearch
*/
public static final class MarshallableObject {
public String bar;
public String foo;
}
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
public static void main(final String[] args) throws UnknownHostException {
try (final TransportClient client = TransportClient.builder().build().addTransportAddress(new InetSocketTransportAddress(InetAddress.getLoopbackAddress(), 9300))) {
final SearchResponse response = client.prepareSearch("news").setTypes("article")
.setQuery(QueryBuilders.matchQuery("text", "advertisement")).execute().actionGet();
final Stream<Map<String, Object>> attrMaps = Arrays.stream(response.getHits().getHits())
.map(hit -> hit.sourceAsMap());
final Stream<MarshallableObject> objs = attrMaps.map(attrMap -> {
// TODO: Find way of converting attr map into original class
// which was marshalled into JSON
// final MarshallableObject obj = OBJECT_MAPPER.read(attrMap);
});
}
}
}
我在 SearchHit
class 上做了一些阅读,这是我认为可能有效的方法:
Stream<MarshallableObject> objs = Arrays.stream(response.getHits().getHits())
.filter(hit->hit.hasSource()) // For some reason source could be null.
.map(hit->
OBJECT_MAPPER.readValue(hit.sourceAsString(),
MarshallableObject.class));
注意:您需要声明 MarshallableObject
class static
才能进行读取。显然,无法从 json 字符串中重建隐藏的 ParentClass$this
引用。
祝你好运。
我正在使用第三方库(准确地说是 Elasticsearch 2.3.3),它已经为我将给定的 JSON 结构解析为 Map<String,Object>
实例,我想要创建对象的 class 实例,该对象最初编组为 JSON:
- 编组:
MarshallableObject
→marshalled_object.json
- 解组:
marshalled_object.json
→Map<String,Object>
→MarshallableObject
)
此外,这里是一些说明逻辑的示例代码:
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Stream;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.QueryBuilders;
import com.fasterxml.jackson.databind.ObjectMapper;
public final class AttrMapUnmarshallingTest {
/**
* This class was directly used in the process for creating JSON documents indexed by Elasticsearch
*/
public static final class MarshallableObject {
public String bar;
public String foo;
}
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
public static void main(final String[] args) throws UnknownHostException {
try (final TransportClient client = TransportClient.builder().build().addTransportAddress(new InetSocketTransportAddress(InetAddress.getLoopbackAddress(), 9300))) {
final SearchResponse response = client.prepareSearch("news").setTypes("article")
.setQuery(QueryBuilders.matchQuery("text", "advertisement")).execute().actionGet();
final Stream<Map<String, Object>> attrMaps = Arrays.stream(response.getHits().getHits())
.map(hit -> hit.sourceAsMap());
final Stream<MarshallableObject> objs = attrMaps.map(attrMap -> {
// TODO: Find way of converting attr map into original class
// which was marshalled into JSON
// final MarshallableObject obj = OBJECT_MAPPER.read(attrMap);
});
}
}
}
我在 SearchHit
class 上做了一些阅读,这是我认为可能有效的方法:
Stream<MarshallableObject> objs = Arrays.stream(response.getHits().getHits())
.filter(hit->hit.hasSource()) // For some reason source could be null.
.map(hit->
OBJECT_MAPPER.readValue(hit.sourceAsString(),
MarshallableObject.class));
注意:您需要声明 MarshallableObject
class static
才能进行读取。显然,无法从 json 字符串中重建隐藏的 ParentClass$this
引用。
祝你好运。