From b92859a6667b7beae2cbea16e02fcda2db438cd5 Mon Sep 17 00:00:00 2001 From: Martin Ledvinka Date: Tue, 17 Dec 2024 10:54:09 +0100 Subject: [PATCH] [Enhancement #66] Ensure deserialization of typed numeric values works correctly. --- .../ExpandedJsonLdDeserializerTest.java | 17 ++++++++ .../JsonLdSerializerTestBase.java | 1 - .../resources/objectWithNumericValues.json | 41 +++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/test/resources/objectWithNumericValues.json diff --git a/src/test/java/cz/cvut/kbss/jsonld/deserialization/expanded/ExpandedJsonLdDeserializerTest.java b/src/test/java/cz/cvut/kbss/jsonld/deserialization/expanded/ExpandedJsonLdDeserializerTest.java index 6db0a31..304995c 100644 --- a/src/test/java/cz/cvut/kbss/jsonld/deserialization/expanded/ExpandedJsonLdDeserializerTest.java +++ b/src/test/java/cz/cvut/kbss/jsonld/deserialization/expanded/ExpandedJsonLdDeserializerTest.java @@ -36,6 +36,7 @@ import cz.cvut.kbss.jsonld.environment.model.GenericMember; import cz.cvut.kbss.jsonld.environment.model.ObjectWithAnnotationProperties; import cz.cvut.kbss.jsonld.environment.model.ObjectWithMultilingualString; +import cz.cvut.kbss.jsonld.environment.model.ObjectWithNumericAttributes; import cz.cvut.kbss.jsonld.environment.model.ObjectWithPluralMultilingualString; import cz.cvut.kbss.jsonld.environment.model.Organization; import cz.cvut.kbss.jsonld.environment.model.OwlPropertyType; @@ -56,6 +57,8 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import java.math.BigDecimal; +import java.math.BigInteger; import java.net.URI; import java.util.Date; import java.util.HashMap; @@ -863,4 +866,18 @@ void deserializationUsesProvidedTargetTypeWhenNoTypeIsSpecifiedTypeAssumingIsEna assertEquals(TestUtil.UNSC_URI, result.getEmployer().getUri()); assertNull(result.getEmployer().getName()); } + + @Test + void deserializationHandlesNumericValuesWithDatatypes() throws Exception { + final JsonArray input = readAndExpand("objectWithNumericValues.json"); + final ObjectWithNumericAttributes result = sut.deserialize(input, ObjectWithNumericAttributes.class); + assertNotNull(result); + assertEquals((short) 128, result.getShortValue()); + assertEquals(128, result.getIntValue()); + assertEquals(128L, result.getLongValue()); + assertEquals(128.3f, result.getFloatValue()); + assertEquals(128.3, result.getDoubleValue()); + assertEquals(BigInteger.valueOf(128000), result.getBigIntegerValue()); + assertEquals(BigDecimal.valueOf(128000.821), result.getBigDecimalValue()); + } } diff --git a/src/test/java/cz/cvut/kbss/jsonld/serialization/JsonLdSerializerTestBase.java b/src/test/java/cz/cvut/kbss/jsonld/serialization/JsonLdSerializerTestBase.java index 0344a46..f2a2aba 100644 --- a/src/test/java/cz/cvut/kbss/jsonld/serialization/JsonLdSerializerTestBase.java +++ b/src/test/java/cz/cvut/kbss/jsonld/serialization/JsonLdSerializerTestBase.java @@ -83,7 +83,6 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.hasItems; -import static org.hamcrest.Matchers.in; import static org.hamcrest.Matchers.startsWith; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; diff --git a/src/test/resources/objectWithNumericValues.json b/src/test/resources/objectWithNumericValues.json new file mode 100644 index 0000000..4ad69ce --- /dev/null +++ b/src/test/resources/objectWithNumericValues.json @@ -0,0 +1,41 @@ +{ + "@context": { + "intValue": { + "@id": "http://krizik.felk.cvut.cz/ontologies/jb4jsonld/intValue", + "@type": "http://www.w3.org/2001/XMLSchema#int" + }, + "shortValue": { + "@id": "http://krizik.felk.cvut.cz/ontologies/jb4jsonld/shortValue", + "@type": "http://www.w3.org/2001/XMLSchema#short" + }, + "longValue": { + "@id": "http://krizik.felk.cvut.cz/ontologies/jb4jsonld/longValue", + "@type": "http://www.w3.org/2001/XMLSchema#long" + }, + "floatValue": { + "@id": "http://krizik.felk.cvut.cz/ontologies/jb4jsonld/floatValue", + "@type": "http://www.w3.org/2001/XMLSchema#float" + }, + "doubleValue": { + "@id": "http://krizik.felk.cvut.cz/ontologies/jb4jsonld/doubleValue", + "@type": "http://www.w3.org/2001/XMLSchema#double" + }, + "bigIntegerValue": { + "@id": "http://krizik.felk.cvut.cz/ontologies/jb4jsonld/bigIntegerValue", + "@type": "http://www.w3.org/2001/XMLSchema#integer" + }, + "bigDecimalValue": { + "@id": "http://krizik.felk.cvut.cz/ontologies/jb4jsonld/bigDecimalValue", + "@type": "http://www.w3.org/2001/XMLSchema#decimal" + } + }, + "@id": "http://example.com/objectWithNumericValues", + "@type": "http://krizik.felk.cvut.cz/ontologies/jb4jsonld/ObjectWithNumericAttributes", + "shortValue": 128, + "intValue": 128, + "longValue": 128, + "floatValue": 128.3, + "doubleValue": 128.3, + "bigIntegerValue": 128000, + "bigDecimalValue": 128000.821 +} \ No newline at end of file