MATSIM
StringStringMapConverter.java
Go to the documentation of this file.
1 package org.matsim.utils.objectattributes.attributeconverters;
2 
3 import com.fasterxml.jackson.core.JsonProcessingException;
4 import com.fasterxml.jackson.databind.ObjectMapper;
5 import com.fasterxml.jackson.databind.type.MapType;
6 import com.fasterxml.jackson.databind.type.TypeFactory;
8 
9 import java.util.Collections;
10 import java.util.Map;
11 
12 public class StringStringMapConverter implements AttributeConverter<Map<String, String>> {
13 
14  private static final ObjectMapper mapper = new ObjectMapper();
15  private static final MapType mapType = TypeFactory.defaultInstance().constructMapType(Map.class, String.class, String.class);
16 
17  @Override
18  public Map<String, String> convert(String value) {
19  try {
20  return Collections.unmodifiableMap(mapper.readValue(value, mapType));
21  } catch (JsonProcessingException e) {
22  throw new RuntimeException(e);
23  }
24  }
25 
26  @Override
27  public String convertToString(Object o) {
28  try {
29  return mapper.writeValueAsString(o);
30  } catch (JsonProcessingException e) {
31  throw new RuntimeException(e);
32  }
33  }
34 }