ESHttpJsonUnmarshaller implements ESHttpUnmarshaller { @Override public final Object unmarshal(final DeserializerRegistry registry, final SerializedDataType dataType, final EnhancedMimeType mimeType, final Object data) { if (data == null) { return null; } if (!(data instanceof JsonObject)) { throw new IllegalArgumentException("Can only unmarshal JsonObject, but was: " + data + " [" + data.getClass().getName() + "]"); } final JsonObject jsonObj = (JsonObject) data; final String transferEncodingData = mimeType.getParameter("transfer-encoding"); if (transferEncodingData == null) { final Deserializer deSer = registry.getDeserializer(dataType, mimeType); return deSer.unmarshal(jsonObj, dataType, mimeType); } final String base64str = jsonObj.getString(Base64Data.EL_ROOT_NAME); final byte[] bytes = Base64.decodeBase64(base64str); final Deserializer deSer = registry.getDeserializer(dataType, mimeType); return deSer.unmarshal(bytes, dataType, mimeType); }  @Override final Object unmarshal(final DeserializerRegistry registry, final SerializedDataType dataType,
            final EnhancedMimeType mimeType, final Object data);  }
@Test public void testNull() throws MimeTypeParseException { final SimpleSerializerDeserializerRegistry registry = new SimpleSerializerDeserializerRegistry(); assertThat( new ESHttpJsonUnmarshaller().unmarshal(registry, new SerializedDataType("whatever"), new EnhancedMimeType("application/json"), null)).isNull(); }
@Test public void testJsonJson() throws IOException, MimeTypeParseException { final SerializedDataType dataType = new SerializedDataType(MyEvent.TYPE.asBaseType()); final EnhancedMimeType mimeType = new EnhancedMimeType("application/json"); final DeserializerRegistry registry = createRegistry(); final JsonObject jsonObj = parse("/event-json-json-json.json", "$.Data"); final Object obj = new ESHttpJsonUnmarshaller().unmarshal(registry, dataType, mimeType, jsonObj); assertThat(obj).isInstanceOf(JsonObject.class); final JsonObject event = (JsonObject) obj; assertThat(event.getString("id")).isEqualTo("b2a936ce-d479-414f-b67f-3df4da383d47"); assertThat(event.getString("description")).isEqualTo("Hello, JSON!"); }
@Test public void testJsonOther() throws IOException, MimeTypeParseException { final SerializedDataType dataType = new SerializedDataType(MyEvent.TYPE.asBaseType()); final EnhancedMimeType mimeType = new EnhancedMimeType( "application/xml; version=1; encoding=utf-8; transfer-encoding=base64"); final DeserializerRegistry registry = createRegistry(); final JsonObject jsonObj = parse("/event-json-json-other.json", "$.Data"); final Object obj = new ESHttpJsonUnmarshaller().unmarshal(registry, dataType, mimeType, jsonObj); assertThat(obj).isInstanceOf(MyEvent.class); final MyEvent event = (MyEvent) obj; assertThat(event.getId()).isEqualTo("bd58da40-9249-4b42-a077-10455b483c80"); assertThat(event.getDescription()).isEqualTo("Hello, XML!"); }