JsonTranscoder implements CacheTranscoder { @Override public CachedObject encode(final Object o) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { mapper.writeValue(baos, new Holder(o)); return new CachedObjectImpl(JSON_SERIALIZED, baos.toByteArray()); } catch (IOException e) { LOGGER.warn(String.format("Error serializing object %s", o), e); throw new RuntimeException(e); } finally { try { baos.close(); } catch (IOException e) { LOGGER.warn("Error while closing stream", e); } } } JsonTranscoder(final ObjectMapper mapper); boolean asyncDecode(final CachedObject data); @Override Object decode(final CachedObject data); @Override CachedObject encode(final Object o); int getMaxSize();  }
@Test public void testEncodeAndDecodeWithCustomSerializer() { JsonObjectMapper mapper = new JsonObjectMapper(); Map<Class<?>, JsonSerializer<?>> serializers = new HashMap<Class<?>, JsonSerializer<?>>(); serializers.put(Point.class, new PointSerializer()); mapper.setSerializers(serializers); transcoder = new JsonTranscoder(mapper); Point p = new Point(40, 50); CachedObject co = transcoder.encode(p); assertNotNull(co); assertNotNull(co.getData()); assertEquals("{\"v\":\"40x50\"}", new String(co.getData())); }
@Test public void testEncodeAndDecodeRegisterSerializerDirectlyToModule() { JsonObjectMapper mapper = new JsonObjectMapper(); SimpleModule module = new SimpleModule("cemo", Version.unknownVersion()); module.addSerializer(Point.class, new PointSerializer()); mapper.registerModule(module); transcoder = new JsonTranscoder(mapper); Point p = new Point(40, 50); CachedObject co = transcoder.encode(p); assertNotNull(co); assertNotNull(co.getData()); assertEquals("{\"v\":\"40x50\"}", new String(co.getData())); }