KeyWithType { @Override @JsonValue public final String toString() { return getType() + ":" + BaseEncoding.base64().encode(getKey().bytes()); }  abstract KeyType getType(); abstract Key getKey(); @Override @JsonValue final String toString(); @JsonCreator static KeyWithType fromString(String keyWithType);  }
@Test public void testSerialization() throws IOException { String serialized = mapper.writeValueAsString(kwt); String expectedSerialization = String.format("\"%s\"", kwtString); assertThat(serialized, is(expectedSerialization)); KeyWithType deserialized = mapper.readValue(serialized, KeyWithType.class); assertThat(deserialized.toString(), is(kwt.toString())); }