This is an automated email from the ASF dual-hosted git repository.

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit c90cc7f4bd20499f1504ab09abc633c499992789
Author: Rémi Kowalski <rkowal...@linagora.com>
AuthorDate: Fri Aug 30 16:55:48 2019 +0200

    JAMES-2813 Add Json deserializer selection
---
 .../java/org/apache/james/json/JsonGenericSerializer.java | 12 ++++++++++--
 .../test/java/org/apache/JsonGenericSerializerTest.java   | 15 +++++++++++++++
 2 files changed, 25 insertions(+), 2 deletions(-)

diff --git 
a/json/src/main/java/org/apache/james/json/JsonGenericSerializer.java 
b/json/src/main/java/org/apache/james/json/JsonGenericSerializer.java
index 3075beb..c03a4a1 100644
--- a/json/src/main/java/org/apache/james/json/JsonGenericSerializer.java
+++ b/json/src/main/java/org/apache/james/json/JsonGenericSerializer.java
@@ -107,10 +107,18 @@ public class JsonGenericSerializer<T, U extends DTO> {
         }
     }
 
+    public T deserialize(String type, String value) throws IOException {
+        try {
+            DTOModule<T, U> dtoModule = retrieveModuleForType(type);
+            U dto = objectMapper.readValue(value, dtoModule.getDTOClass());
+            return dtoModule.getToDomainObjectConverter().convert(dto);
+        } catch (MismatchedInputException e) {
+            throw new JsonGenericSerializer.InvalidTypeException("Unable to 
deserialize the json document", e);
+        }
+    }
+
     private DTOModule<T, U> retrieveModuleForType(String type) {
         return Optional.ofNullable(typeToModule.get(type))
             .orElseThrow(() -> new UnknownTypeException("unknown type " + 
type));
     }
-
-
 }
diff --git a/json/src/test/java/org/apache/JsonGenericSerializerTest.java 
b/json/src/test/java/org/apache/JsonGenericSerializerTest.java
index 8205553..ca6a58b 100644
--- a/json/src/test/java/org/apache/JsonGenericSerializerTest.java
+++ b/json/src/test/java/org/apache/JsonGenericSerializerTest.java
@@ -66,6 +66,14 @@ class JsonGenericSerializerTest {
 
     @SuppressWarnings("unchecked")
     @Test
+    void shouldDeserializeWithMissingWhenSpecifiedType() throws Exception {
+        assertThat(JsonGenericSerializer.of(TestModules.FIRST_TYPE)
+            .deserialize("first", MISSING_TYPE_JSON))
+            .isEqualTo(FIRST);
+    }
+
+    @SuppressWarnings("unchecked")
+    @Test
     void shouldThrowWhenDeserializeEventWithDuplicatedTypes() {
         assertThatThrownBy(() -> JsonGenericSerializer.of(
                 TestModules.FIRST_TYPE,
@@ -81,6 +89,13 @@ class JsonGenericSerializerTest {
             .isInstanceOf(JsonGenericSerializer.UnknownTypeException.class);
     }
 
+    @Test
+    void shouldThrowWhenDeserializeUnknownTypeWhenSpecifiedType() {
+        assertThatThrownBy(() -> JsonGenericSerializer.of()
+            .deserialize("first",FIRST_JSON))
+            .isInstanceOf(JsonGenericSerializer.UnknownTypeException.class);
+    }
+
     @ParameterizedTest
     @MethodSource
     void serializeShouldHandleAllKnownTypes(BaseType domainObject, String 
serializedJson) throws Exception {


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org
For additional commands, e-mail: server-dev-h...@james.apache.org

Reply via email to