JsonCodec implements Codec<ServiceInstance> { @Override public ServiceInstance deserialize(InputStream source) throws IOException { InputStreamReader reader = new InputStreamReader(source, ENCODING); try { @Nullable ServiceInstanceSchema schema = gson.fromJson(reader, ServiceInstanceSchema.class); if (schema == null) { throw new IOException("JSON did not include a ServiceInstance object"); } return schema.asServiceInstance(); } catch (JsonParseException e) { throw new IOException("Problem parsing JSON ServiceInstance.", e); } } JsonCodec();   JsonCodec(Gson gson); @Override void serialize(ServiceInstance instance, OutputStream sink); @Override ServiceInstance deserialize(InputStream source);  }
@Test public void testDeserializeMinimal() throws IOException { String minimal = "{\"serviceEndpoint\":{\"host\":\"foo\",\"port\":1000},\"status\":\"ALIVE\"}"; ByteArrayInputStream source = new ByteArrayInputStream(minimal.getBytes(Charsets.UTF_8)); ServiceInstance actual = STANDARD_JSON_CODEC.deserialize(source); ServiceInstance expected = new ServiceInstance(new Endpoint("foo", 1000), ImmutableMap.of(), Status.ALIVE); assertEquals(expected, actual); }