From 54eacf438e826514307668cfe26e6c6a63a21570 Mon Sep 17 00:00:00 2001 From: Antti Villberg Date: Thu, 27 Jul 2017 14:57:17 +0300 Subject: [PATCH] JsonNode support with Data/Json refs #7388 Change-Id: I602fd4a3ca3dfa8aac2eb873d4dbce79698b612f --- .../META-INF/MANIFEST.MF | 3 +- .../scl/Data/JsonNode.scl | 23 +++++++++++++++ .../scl/data/xml/JsonNodeHelper.java | 28 +++++++++++++++++++ .../org.simantics.scl.feature/feature.xml | 14 ++++++++++ 4 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 bundles/org.simantics.scl.data/scl/Data/JsonNode.scl create mode 100644 bundles/org.simantics.scl.data/src/org/simantics/scl/data/xml/JsonNodeHelper.java diff --git a/bundles/org.simantics.scl.data/META-INF/MANIFEST.MF b/bundles/org.simantics.scl.data/META-INF/MANIFEST.MF index 52095a101..61be3240e 100644 --- a/bundles/org.simantics.scl.data/META-INF/MANIFEST.MF +++ b/bundles/org.simantics.scl.data/META-INF/MANIFEST.MF @@ -8,5 +8,6 @@ Require-Bundle: org.simantics.scl.runtime;bundle-version="0.4.0", org.simantics.scl.osgi;bundle-version="1.0.4", org.jdom2;bundle-version="2.0.6", org.junit;bundle-version="4.12.0";resolution:=optional, - com.fasterxml.jackson.core.jackson-core;bundle-version="2.8.2" + com.fasterxml.jackson.core.jackson-core;bundle-version="2.8.2", + com.fasterxml.jackson.core.jackson-databind Bundle-ClassPath: . diff --git a/bundles/org.simantics.scl.data/scl/Data/JsonNode.scl b/bundles/org.simantics.scl.data/scl/Data/JsonNode.scl new file mode 100644 index 000000000..f0e49f438 --- /dev/null +++ b/bundles/org.simantics.scl.data/scl/Data/JsonNode.scl @@ -0,0 +1,23 @@ +import "StandardLibrary" +import "Data/Writer" +import "JavaBuiltin" as Java +import "Data/Json" + +importJava "com.fasterxml.jackson.databind.JsonNode" where + data JsonNode + +importJava "org.simantics.scl.data.xml.JsonNodeHelper" where + @private + @JavaName toJsonString + jsonNodeToString :: JsonNode -> String + @private + @JavaName fromJsonString + stringToJsonNode :: String -> JsonNode + +jsonNodeToJson :: JsonNode -> Json +jsonNodeToJson node = fromJsonString (jsonNodeToString node) + +jsonToJsonNode :: Json -> JsonNode +jsonToJsonNode json = stringToJsonNode (toJsonString json) + + \ No newline at end of file diff --git a/bundles/org.simantics.scl.data/src/org/simantics/scl/data/xml/JsonNodeHelper.java b/bundles/org.simantics.scl.data/src/org/simantics/scl/data/xml/JsonNodeHelper.java new file mode 100644 index 000000000..9eda11429 --- /dev/null +++ b/bundles/org.simantics.scl.data/src/org/simantics/scl/data/xml/JsonNodeHelper.java @@ -0,0 +1,28 @@ +package org.simantics.scl.data.xml; + +import java.io.IOException; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; + +public class JsonNodeHelper { + + private static final ObjectMapper SORTED_MAPPER = new ObjectMapper(); + + static { + SORTED_MAPPER.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true); + } + + public static String toJsonString(JsonNode node) throws JsonProcessingException { + final Object obj = SORTED_MAPPER.treeToValue(node, Object.class); + final String json = SORTED_MAPPER.writeValueAsString(obj); + return json; + } + + public static JsonNode fromJsonString(String s) throws JsonProcessingException, IOException { + return SORTED_MAPPER.readTree(s); + } + +} diff --git a/features/org.simantics.scl.feature/feature.xml b/features/org.simantics.scl.feature/feature.xml index 5c8f6fd1d..c0ee33b4e 100644 --- a/features/org.simantics.scl.feature/feature.xml +++ b/features/org.simantics.scl.feature/feature.xml @@ -227,4 +227,18 @@ This Agreement is governed by the laws of the State of New York and the intellec version="0.0.0" unpack="false"/> + + + + -- 2.47.1