JsonDiagnosticCodec extends DiagnosticCodecSkeleton<String> { @Override public <T> T deserialize(String json, Class<T> target) throws DiagnosticCodecException { requireNonNull(json); requireNonNull(target); try { return JsonNode.class.isAssignableFrom(target) ? target.cast(objectMapper.readTree(json)) : objectMapper.readValue(json, target); } catch (IOException | RuntimeException e) { throw new DiagnosticCodecException(e); } } JsonDiagnosticCodec(ObjectMapperFactory objectMapperFactory); @Override String serialize(Object o); @Override T deserialize(String json, Class<T> target); @Override String toString();  }
@Test public void test_deserialize_simple_types() { Stream.of( tuple2(Integer.MAX_VALUE, "" + 0x7fffffff), tuple2(Long.MAX_VALUE, "" + 0x7fffffffffffffffL), tuple2(Float.MAX_VALUE, "" + Float.MAX_VALUE), tuple2(Double.MAX_VALUE, "" + Double.MAX_VALUE), tuple2((byte) 1, "1"), tuple2(true, "true"), tuple2(TimeUnit.HOURS, "\"HOURS\"") ).forEach(tuple -> { Class<?> target = tuple.t1 instanceof Enum<?> ? ((Enum<?>) tuple.t1).getDeclaringClass() : tuple.t1.getClass(); assertThat("De-serializing: " + tuple.t2, codec.deserialize(tuple.t2, target), is(equalTo(tuple.t1))); }); }
@Test public void test_deserialize_request() { DiagnosticRequest request = new DiagnosticRequest(Closeable.class, "prepareDiner()"); assertThat(codec.deserialize(jsonFile("/output1.json"), DiagnosticRequest.class), is(equalTo(request))); }
@Test public void test_deserialize_fails() { exception.expect(DiagnosticCodecException.class); codec.deserialize("foo", getClass()); }
@Test public void test_deserialize_request_with_complex_arguments() { Tomato tomato = new Tomato(new TomatoCooking(), "red"); Pepper pepper = new Pepper(new TomatoCooking(), "spain"); DiagnosticRequest request = new DiagnosticRequest(Closeable.class, "prepareDiner()", tomato, pepper); DiagnosticRequest deserialized = codec.deserialize(jsonFile("/output2.json"), DiagnosticRequest.class); assertThat(deserialized, is(equalTo(request))); }
@Test public void test_deserialize_response_with_complex_arguments() { Tomato tomato = new Tomato(new TomatoCooking(), "red"); DiagnosticResponse<Tomato> response = new DiagnosticResponse<>(tomato); DiagnosticResponse<?> deserialized = codec.deserialize(jsonFile("/output3.json"), DiagnosticResponse.class); assertThat(deserialized, is(equalTo(response))); }