]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.data/scl/Data/Json.scl
Two rendering glitch fixes for time series charts
[simantics/platform.git] / bundles / org.simantics.scl.data / scl / Data / Json.scl
index 659329df7ed8ad12f1b5cc293f85851a0db0ec1e..cacffb01acc806a48d284f220afc8e874c8b8a65 100644 (file)
@@ -1,3 +1,23 @@
+/*
+
+An example how to implement 
+
+data GeographicalLocation = GeographicalLocation {
+    latitude :: Double,
+    longitude :: Double
+}
+
+instance Json GeographicalLocation where
+    toJson GeographicalLocation { latitude, longitude } = JsonObject [
+        JsonField "latitude" (toJson latitude),
+        JsonField "longitude" (toJson longitude)
+    ]
+    fromJson object = GeographicalLocation {
+        latitude = fromJson $ fromJust $ lookupJsonField "latitude" object,
+        longitude = fromJson $ fromJust $ lookupJsonField "longitude" object
+    }
+*/
+
 import "StandardLibrary"
 import "Data/Writer"
 import "JavaBuiltin" as Java
@@ -154,6 +174,7 @@ instance Json Double where
     readJson = getDoubleValue
     toJson = JsonDouble
     fromJson (JsonDouble value) = value
+    fromJson (JsonLong value) = Java.l2d value
 
 instance Json Float where
     writeJson = writeNumberFloat
@@ -298,6 +319,12 @@ data Json =
   | JsonObject [JsonField]
 data JsonField = JsonField String Json
   
+lookupJsonField :: String -> Json -> Maybe Json  
+lookupJsonField fieldName (JsonObject fields) = mapFirst selector fields
+  where
+    selector (JsonField name value) | name == fieldName = Just value
+    selector _ = Nothing 
+  
 deriving instance Show Json
 deriving instance Show JsonField