Skip to content

Commit 684b3f1

Browse files
committed
Merge branch 'jhg023-jsonld-java-275'
Signed-off-by: Peter Ansell <[email protected]>
2 parents 8bb1aca + 5e73b4f commit 684b3f1

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

‎core/src/main/java/com/github/jsonldjava/core/RDFDataset.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import static com.github.jsonldjava.core.JsonLdConsts.RDF_REST;
77
import static com.github.jsonldjava.core.JsonLdConsts.RDF_TYPE;
88
import static com.github.jsonldjava.core.JsonLdConsts.XSD_BOOLEAN;
9+
import static com.github.jsonldjava.core.JsonLdConsts.XSD_DECIMAL;
910
import static com.github.jsonldjava.core.JsonLdConsts.XSD_DOUBLE;
1011
import static com.github.jsonldjava.core.JsonLdConsts.XSD_INTEGER;
1112
import static com.github.jsonldjava.core.JsonLdConsts.XSD_STRING;
@@ -665,7 +666,10 @@ private Node objectToRDF(Object item) {
665666
return new Literal(Float.toString((float) value),
666667
datatype == null ? XSD_DOUBLE : (String) datatype, null);
667668
} else {
668-
// canonical double representation
669+
// Only canonicalize representation if datatype is not XSD_DECIMAL
670+
if (XSD_DECIMAL.equals(datatype)) {
671+
return new Literal(value.toString(), XSD_DECIMAL, null);
672+
}
669673
final DecimalFormat df = new DecimalFormat("0.0###############E0");
670674
df.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance(Locale.US));
671675
return new Literal(df.format(value),
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.github.jsonldjava.core;
2+
3+
import org.junit.Test;
4+
5+
import java.util.HashMap;
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
import static org.junit.Assert.assertEquals;
10+
11+
public class DecimalLiteralCanonicalTest {
12+
13+
@Test
14+
public void testDecimalIsNotCanonicalized() {
15+
double value = 6.5;
16+
17+
Map<String, Object> innerMap = new HashMap<>();
18+
innerMap.put("@value", value);
19+
innerMap.put("@type", "http://www.w3.org/2001/XMLSchema#decimal");
20+
21+
Map<String, Object> jsonMap = new HashMap<>();
22+
jsonMap.put("ex:id", innerMap);
23+
24+
JsonLdApi api = new JsonLdApi(jsonMap, new JsonLdOptions(""));
25+
RDFDataset dataset = api.toRDF();
26+
27+
List<Object> defaultList = (List<Object>) dataset.get("@default");
28+
Map<String, Object> tripleMap = (Map<String, Object>) defaultList.get(0);
29+
Map<String, String> objectMap = (Map<String, String>) tripleMap.get("object");
30+
31+
assertEquals("http://www.w3.org/2001/XMLSchema#decimal", objectMap.get("datatype"));
32+
assertEquals(Double.toString(value), objectMap.get("value"));
33+
}
34+
}

0 commit comments

Comments
 (0)