Skip to content

Commit

Permalink
[Enhancement #66] Ensure deserialization of typed numeric values work…
Browse files Browse the repository at this point in the history
…s correctly.
  • Loading branch information
ledsoft committed Dec 17, 2024
1 parent f0fa640 commit b92859a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
41 changes: 41 additions & 0 deletions src/test/resources/objectWithNumericValues.json
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit b92859a

Please sign in to comment.