Beispiel #1
0
  public void testJackson428() throws Exception {
    ObjectMapper serMapper = new ObjectMapper();

    TypeResolverBuilder<?> serializerTyper =
        new ObjectMapper.DefaultTypeResolverBuilder(ObjectMapper.DefaultTyping.NON_FINAL);
    serializerTyper = serializerTyper.init(JsonTypeInfo.Id.NAME, createTypeNameIdResolver(true));
    serializerTyper = serializerTyper.inclusion(JsonTypeInfo.As.PROPERTY);
    serMapper.setDefaultTyping(serializerTyper);

    // Let's start by constructing something to serialize first
    MapHolder holder = new MapHolder();
    holder.map = new HashMap<MapKey, List<Object>>();
    List<Object> ints = new ArrayList<Object>();
    ints.add(Integer.valueOf(3));
    holder.map.put(new MapKey("key"), ints);
    String json = serMapper.writeValueAsString(holder);

    // Then deserialize: need separate mapper to initialize type id resolver appropriately
    ObjectMapper deserMapper = new ObjectMapper();
    TypeResolverBuilder<?> deserializerTyper =
        new ObjectMapper.DefaultTypeResolverBuilder(ObjectMapper.DefaultTyping.NON_FINAL);
    deserializerTyper =
        deserializerTyper.init(JsonTypeInfo.Id.NAME, createTypeNameIdResolver(false));
    deserializerTyper = deserializerTyper.inclusion(JsonTypeInfo.As.PROPERTY);
    deserMapper.setDefaultTyping(deserializerTyper);

    MapHolder result = deserMapper.readValue(json, MapHolder.class);
    assertNotNull(result);
    Map<?, ?> map = result.map;
    assertEquals(1, map.size());
    Map.Entry<?, ?> entry = map.entrySet().iterator().next();
    Object key = entry.getKey();
    assertEquals(MapKey.class, key.getClass());
    Object value = entry.getValue();
    assertTrue(value instanceof List<?>);
    List<?> list = (List<?>) value;
    assertEquals(1, list.size());
    assertEquals(Integer.class, list.get(0).getClass());
    assertEquals(Integer.valueOf(3), list.get(0));
  }
Beispiel #2
0
 private void appendQueryParams(URI query) {
   for (Map.Entry<String, String> param : queryParams.entrySet()) {
     query.param(param.getKey(), param.getValue());
   }
 }