]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
JsonNode support with Data/Json 71/771/1
authorAntti Villberg <antti.villberg@semantum.fi>
Thu, 27 Jul 2017 11:57:17 +0000 (14:57 +0300)
committerAntti Villberg <antti.villberg@semantum.fi>
Thu, 27 Jul 2017 11:57:17 +0000 (14:57 +0300)
refs #7388

Change-Id: I602fd4a3ca3dfa8aac2eb873d4dbce79698b612f

bundles/org.simantics.scl.data/META-INF/MANIFEST.MF
bundles/org.simantics.scl.data/scl/Data/JsonNode.scl [new file with mode: 0644]
bundles/org.simantics.scl.data/src/org/simantics/scl/data/xml/JsonNodeHelper.java [new file with mode: 0644]
features/org.simantics.scl.feature/feature.xml

index 52095a10169c88160aac11f4b9d3392cc79806e4..61be3240eb72230529a65c273d113410c311f9e8 100644 (file)
@@ -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 (file)
index 0000000..f0e49f4
--- /dev/null
@@ -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 (file)
index 0000000..9eda114
--- /dev/null
@@ -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);       
+       }
+       
+}
index 5c8f6fd1d9d236980f96d055fb245ae923a56d7c..c0ee33b4ef2f2ad106edabb6d6a0152686bfe504 100644 (file)
@@ -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"/>
 
+   <plugin
+         id="com.fasterxml.jackson.core.jackson-annotations"
+         download-size="0"
+         install-size="0"
+         version="0.0.0"
+         unpack="false"/>
+
+   <plugin
+         id="com.fasterxml.jackson.core.jackson-databind"
+         download-size="0"
+         install-size="0"
+         version="0.0.0"
+         unpack="false"/>
+
 </feature>