// [Issue#227]
 public void testGenericEnumSerializer() throws Exception {
   // By default, serialize using name
   ObjectMapper m = new ObjectMapper();
   SimpleModule module = new SimpleModule("foobar");
   module.addSerializer(Enum.class, new LowerCasingEnumSerializer());
   m.registerModule(module);
   assertEquals(quote("b"), m.writeValueAsString(TestEnum.B));
 }
 public void testSimpleKeyDeser() throws Exception {
   ObjectMapper mapper = new ObjectMapper();
   SimpleModule module = new SimpleModule("test", Version.unknownVersion());
   module.addKeyDeserializer(String.class, new ContextualDeser("???"));
   mapper.registerModule(module);
   MapBean result = mapper.readValue("{\"map\":{\"a\":3}}", MapBean.class);
   Map<String, Integer> map = result.map;
   assertNotNull(map);
   assertEquals(1, map.size());
   Map.Entry<String, Integer> entry = map.entrySet().iterator().next();
   assertEquals(Integer.valueOf(3), entry.getValue());
   assertEquals("map:a", entry.getKey());
 }
 public void testSimpleKeySer() throws Exception {
   ObjectMapper mapper = new ObjectMapper();
   SimpleModule module = new SimpleModule("test", Version.unknownVersion());
   module.addKeySerializer(String.class, new ContextualKeySerializer("prefix"));
   mapper.registerModule(module);
   Map<String, Object> input = new HashMap<String, Object>();
   input.put("a", Integer.valueOf(3));
   String json =
       mapper
           .writerWithType(
               TypeFactory.defaultInstance()
                   .constructMapType(HashMap.class, String.class, Object.class))
           .writeValueAsString(input);
   assertEquals("{\"prefix:a\":3}", json);
 }
Ejemplo n.º 4
0
  @Override
  public Record deserialize(InputStream in) throws RecordSerializationException {

    ObjectMapper mapper = new ObjectMapper();
    SimpleModule module = new SimpleModule();
    module.addDeserializer(Record.class, new EventDeserializer());
    mapper.registerModule(module);

    Record record = null;
    try {
      record = mapper.readValue(in, Record.class);
    } catch (IOException e) {
      e.printStackTrace();
    }

    return record;
  }
Ejemplo n.º 5
0
  @Override
  public void serialize(OutputStream out, Record record) throws RecordSerializationException {

    ObjectMapper mapper = new ObjectMapper();
    SimpleModule module = new SimpleModule();
    module.addSerializer(StandardRecord.class, new EventSerializer());
    mapper.registerModule(module);

    // map json to student

    try {
      mapper.enable(SerializationFeature.INDENT_OUTPUT);
      mapper.setPropertyNamingStrategy(
          PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
      String jsonString = mapper.writeValueAsString(record);

      out.write(jsonString.getBytes());
      out.flush();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }