diff --git a/org.eclipse.scout.rt.jackson.test/src/test/java/org/eclipse/scout/rt/jackson/dataobject/JsonDataObjectsSerializationTest.java b/org.eclipse.scout.rt.jackson.test/src/test/java/org/eclipse/scout/rt/jackson/dataobject/JsonDataObjectsSerializationTest.java index 4dfc294288d..70871cdc758 100644 --- a/org.eclipse.scout.rt.jackson.test/src/test/java/org/eclipse/scout/rt/jackson/dataobject/JsonDataObjectsSerializationTest.java +++ b/org.eclipse.scout.rt.jackson.test/src/test/java/org/eclipse/scout/rt/jackson/dataobject/JsonDataObjectsSerializationTest.java @@ -167,7 +167,6 @@ import com.fasterxml.jackson.databind.exc.InvalidDefinitionException; import com.fasterxml.jackson.databind.exc.InvalidFormatException; import com.fasterxml.jackson.databind.exc.InvalidTypeIdException; -import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException; /** * Various test cases serializing and deserializing Scout data objects from/to JSON @@ -1803,6 +1802,24 @@ public void testSerializeDeserialize_EntityWithCollectionRaw() throws Exception assertEquals("TestItem2", attribute7.get(1).get(ScoutDataObjectModule.DEFAULT_TYPE_ATTRIBUTE_NAME)); } + @Test + public void testSerializeDeserialize_RawEntityWithDouble() throws Exception { + DoEntity entity = BEANS.get(DoEntity.class); + entity.put("attribute", 45.69); + String json = s_dataObjectMapper.writeValueAsString(entity); + DoEntity doMarshalled = s_dataObjectMapper.readValue(json, DoEntity.class); + assertEquals(new BigDecimal("45.69"), doMarshalled.get("attribute")); + } + + @Test + public void testSerializeDeserialize_RawEntityWithDoubleList() throws Exception { + DoEntity entity = BEANS.get(DoEntity.class); + entity.put("attribute", List.of(45.69)); + String json = s_dataObjectMapper.writeValueAsString(entity); + DoEntity doMarshalled = s_dataObjectMapper.readValue(json, DoEntity.class); + assertEquals(new BigDecimal("45.69"), doMarshalled.getList("attribute").get(0)); + } + @Test public void testSerializeDeserialize_TestMapDo() throws Exception { TestMapDo mapDo = new TestMapDo(); @@ -3225,27 +3242,28 @@ public void testSerializeDeserializeThrowable() throws Exception { assertArrayEquals(exception.getStackTrace(), marshalled.getException().getStackTrace()); } + /** + * {@link Optional} is currently not serializable/deserializable using Scout Jackson implementation. + */ @Test public void testSerializeDeserializeOptionalDo() throws Exception { @SuppressWarnings("unchecked") TestOptionalDo optional = BEANS.get(TestOptionalDo.class) .withOptString(Optional.empty()) .withOptStringList(Optional.empty(), Optional.of("foo")); - String json = s_dataObjectMapper.writeValueAsString(optional); - // currently serializable using Scout Jackson implementation, but without values, e.g. useless! - assertJsonEquals("TestOptionalDo.json", json); + // Expect: + // com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Java 8 optional type `java.util.Optional` + // not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jdk8" to enable handling + JsonMappingException writeException = assertThrows(JsonMappingException.class, () -> s_dataObjectMapper.writeValueAsString(optional)); + assertTrue("expected InvalidDefinitionException, got " + writeException, writeException instanceof InvalidDefinitionException); - // currently not deserializable using Scout Jackson implementation + // Expect: + // com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Java 8 optional type `java.util.Optional` + // not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jdk8" to enable handling + String json = readResourceAsString("TestOptionalDo.json"); JsonMappingException exception = assertThrows(JsonMappingException.class, () -> s_dataObjectMapper.readValue(json, TestOptionalDo.class)); - - // TODO [23.1] pbz remove when JDK 11 is no longer supported - if ("11".equals(System.getProperty("java.specification.version"))) { - assertTrue("expected cause UnrecognizedPropertyException, got " + exception.getCause(), exception.getCause() instanceof UnrecognizedPropertyException); - } - else { - assertTrue("expected cause InvalidDefinitionException, got " + exception.getCause(), exception.getCause() instanceof InvalidDefinitionException); - } + assertTrue("expected InvalidDefinitionException, got " + exception.getCause(), exception.getCause() instanceof InvalidDefinitionException); } @Test diff --git a/org.eclipse.scout.rt.jackson/src/main/java/org/eclipse/scout/rt/jackson/dataobject/DoCollectionDeserializer.java b/org.eclipse.scout.rt.jackson/src/main/java/org/eclipse/scout/rt/jackson/dataobject/DoCollectionDeserializer.java index b44e32f712e..154cd980ff2 100644 --- a/org.eclipse.scout.rt.jackson/src/main/java/org/eclipse/scout/rt/jackson/dataobject/DoCollectionDeserializer.java +++ b/org.eclipse.scout.rt.jackson/src/main/java/org/eclipse/scout/rt/jackson/dataobject/DoCollectionDeserializer.java @@ -10,6 +10,7 @@ package org.eclipse.scout.rt.jackson.dataobject; import java.io.IOException; +import java.math.BigDecimal; import java.util.Collection; import java.util.function.Supplier; @@ -74,10 +75,19 @@ protected ResolvedType resolveListElementType(JsonParser p) { } else { // all JSON scalar values are deserialized as bound type (if available) and as fallback as raw object using default jackson typing - return ObjectUtility.nvl(m_collectionType.getBindings().getBoundType(0), TypeFactory.unknownType()); + return ObjectUtility.nvl(m_collectionType.getBindings().getBoundType(0), resolveFallbackListElementType(p)); } } + protected ResolvedType resolveFallbackListElementType(JsonParser p) { + if (p.getCurrentToken() == JsonToken.VALUE_NUMBER_FLOAT) { + // deserialize floating point numbers as BigDecimal + return TypeFactory.defaultInstance().constructType(BigDecimal.class); + } + // JSON scalar values are deserialized as raw object using default jackson typing + return TypeFactory.unknownType(); + } + @Override public Object deserializeWithType(JsonParser p, DeserializationContext ctxt, TypeDeserializer typeDeserializer) throws IOException { return deserialize(p, ctxt); diff --git a/org.eclipse.scout.rt/pom.xml b/org.eclipse.scout.rt/pom.xml index 88ff3cc4b03..30000ff6c34 100644 --- a/org.eclipse.scout.rt/pom.xml +++ b/org.eclipse.scout.rt/pom.xml @@ -123,7 +123,7 @@ 10.0.18 2.0.7 1.3.14 - 2.14.0 + 2.16.1 4.1.104.Final 2.6.0 1.17