下面列出了com.fasterxml.jackson.databind.type.TypeFactory#constructMapType ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Test the property 'type'
*/
@Test
public void typeTest() {
String zebraJson = "{\"type\":\"mountain\",\"className\":\"zebra\",\"key1\":\"value1\",\"key2\":12321}";
JSON j = new JSON();
TypeFactory typeFactory = j.getMapper().getTypeFactory();
MapType mapType = typeFactory.constructMapType(HashMap.class, String.class, Object.class);
try {
HashMap<String, Object> map = j.getMapper().readValue(zebraJson, mapType);
Assert.assertEquals(map.get("type"), "mountain");
Map<String,Object> result =
j.getMapper().readValue(zebraJson, new TypeReference<Map<String,Object>>() {});
Assert.assertEquals(result.get("type"), "mountain");
} catch (Exception ex) {
Assert.assertEquals(true, false); // exception shouldn't be thrown
}
}
public static JsonValueMap fromBundle(BundleName bundle, Locale locale) {
JsonValueMap jvm = new JsonValueMap();
jvm.setMapId(bundle.getValueMapId());
URL resource = findResource(bundle.toString(), CONTROL, locale);
if (resource == null) {
return jvm;
}
ObjectMapper om = JsonUtils.defaultObjectMapper();
try {
TypeFactory typeFactory = om.getTypeFactory();
MapType jsonType = typeFactory.constructMapType(Map.class, String.class, Object.class);
Map<String, Object> jsonMap = om.readValue(resource, jsonType);
if (LOG.isLoggable(Level.FINE)) {
LOG.fine(String.valueOf(jsonMap));
}
Object data = jsonMap.get("data");
jvm.setValues((List<Map<String, Object>>) data);
} catch (Exception ex) {
LOG.log(Level.WARNING, "Cannot read resource " + resource, ex);
}
return jvm;
}
private List<Message> mapGetMessagesResponse(SearchResponse response) throws IOException {
SearchHit[] hits = response.getHits().getHits();
TypeFactory factory = TypeFactory.defaultInstance();
MapType type = factory.constructMapType(HashMap.class, String.class, String.class);
List<Message> messages = new ArrayList<>(hits.length);
for (SearchHit hit : hits) {
String source = hit.getSourceAsString();
Map<String, String> mapSource = objectMapper.readValue(source, type);
Message msg = new Message(mapSource.get("messageId"), mapSource.get("payload"), null);
messages.add(msg);
}
return messages;
}
private List<Message> mapGetMessagesResponse(SearchResponse response) throws IOException {
SearchHit[] hits = response.getHits().getHits();
TypeFactory factory = TypeFactory.defaultInstance();
MapType type = factory.constructMapType(HashMap.class, String.class, String.class);
List<Message> messages = new ArrayList<>(hits.length);
for (SearchHit hit : hits) {
String source = hit.getSourceAsString();
Map<String, String> mapSource = objectMapper.readValue(source, type);
Message msg = new Message(mapSource.get("messageId"), mapSource.get("payload"), null);
messages.add(msg);
}
return messages;
}
private List<Message> mapGetMessagesResponse(SearchResponse response) throws IOException {
SearchHit[] hits = response.getHits().getHits();
TypeFactory factory = TypeFactory.defaultInstance();
MapType type = factory.constructMapType(HashMap.class, String.class, String.class);
List<Message> messages = new ArrayList<>(hits.length);
for (SearchHit hit : hits) {
String source = hit.getSourceAsString();
Map<String, String> mapSource = objectMapper.readValue(source, type);
Message msg = new Message(mapSource.get("messageId"), mapSource.get("payload"), null);
messages.add(msg);
}
return messages;
}
private List<Message> mapGetMessagesResponse(SearchResponse response) throws IOException {
SearchHit[] hits = response.getHits().getHits();
TypeFactory factory = TypeFactory.defaultInstance();
MapType type = factory.constructMapType(HashMap.class, String.class, String.class);
List<Message> messages = new ArrayList<>(hits.length);
for (SearchHit hit : hits) {
String source = hit.getSourceAsString();
Map<String, String> mapSource = objectMapper.readValue(source, type);
Message msg = new Message(mapSource.get("messageId"), mapSource.get("payload"), null);
messages.add(msg);
}
return messages;
}
@SuppressWarnings( "unchecked" )
protected TableSerializer(final TableSerializer src,
final BeanProperty property,
final TypeFactory typeFactory,
final JsonSerializer<?> rowKeySerializer,
final JsonSerializer<?> columnKeySerializer,
final TypeSerializer valueTypeSerializer,
final JsonSerializer<?> valueSerializer)
{
super(src, property);
_type = src._type;
_rowSerializer = (JsonSerializer<Object>) rowKeySerializer;
_columnSerializer = (JsonSerializer<Object>) columnKeySerializer;
_valueTypeSerializer = valueTypeSerializer;
_valueSerializer = (JsonSerializer<Object>) valueSerializer;
final MapType columnAndValueType = typeFactory.constructMapType(Map.class,
_type.containedTypeOrUnknown(1), _type.containedTypeOrUnknown(2));
JsonSerializer<?> columnAndValueSerializer =
MapSerializer.construct((Set<String>) null,
columnAndValueType,
false,
_valueTypeSerializer,
_columnSerializer,
_valueSerializer,
null);
final MapType rowMapType = typeFactory.constructMapType(Map.class,
_type.containedTypeOrUnknown(0), columnAndValueType);
_rowMapSerializer =
MapSerializer.construct((Set<String>) null,
rowMapType,
false,
null,
_rowSerializer,
(JsonSerializer<Object>) columnAndValueSerializer,
null);
}
public void enterComponent_terminologies_section(AdlParser.Component_terminologies_sectionContext ctx) {
if(archetype instanceof OperationalTemplate) {
OperationalTemplate template = (OperationalTemplate) archetype;
TypeFactory typeFactory = OdinToJsonConverter.getObjectMapper().getTypeFactory();
MapType mapType = typeFactory.constructMapType(ConcurrentHashMap.class, String.class, ArchetypeTerminology.class);
template.setComponentTerminologies(OdinObjectParser.convert(ctx.odin_text(), mapType));
} else {
throw new IllegalArgumentException("cannot add component terminologies to anything but an operational template");
}
}
@Override
public JavaType getInputType(TypeFactory typeFactory) {
return typeFactory.constructMapType(SingleKeyHashMap.class, String.class, JsonAttribute.class);
}
@Override
public JavaType getInputType(TypeFactory typeFactory) {
return typeFactory.constructMapType(SingleKeyHashMap.class, String.class, JsonField.class);
}
@Override
public JavaType getInputType(TypeFactory typeFactory) {
return typeFactory.constructMapType(SingleKeyHashMap.class, String.class, JsonMessage.class);
}
@Override
public JavaType getOutputType(TypeFactory typeFactory) {
return typeFactory.constructMapType(HashMap.class, String.class, JsonAttribute.class);
}
@Override
public JavaType getOutputType(TypeFactory typeFactory) {
return typeFactory.constructMapType(HashMap.class, String.class, JsonField.class);
}
@Override
public JavaType getOutputType(TypeFactory typeFactory) {
return typeFactory.constructMapType(HashMap.class, String.class, JsonMessage.class);
}
private MapType constructMapType(Ctx pc, TypeFactory typeFactory) {
return typeFactory.constructMapType(LinkedHashMap.class, typeFactory.constructType(String.class), pc.getType().getContentType());
}
public Map<String, String> fromJsonToMap(byte[] bytes) throws IOException {
TypeFactory factory = TypeFactory.defaultInstance();
MapType mapType = factory.constructMapType(HashMap.class, String.class, String.class);
return objectMapper.readValue(bytes, mapType);
}
@Override
JavaType emulatedJavaType(TypeFactory typeFactory) {
return typeFactory.constructMapType(LinkedHashMap.class, mapType.getKeyType(), mapType.getContentType());
}
@Override
JavaType emulatedJavaType(TypeFactory typeFactory) {
JavaType containerType = typeFactory.constructCollectionType(ArrayList.class, mapType.getContentType());
return typeFactory.constructMapType(LinkedHashMap.class, mapType.getKeyType(), containerType);
}