+/*
+
+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
readJson = getDoubleValue
toJson = JsonDouble
fromJson (JsonDouble value) = value
+ fromJson (JsonLong value) = Java.l2d value
instance Json Float where
writeJson = writeNumberFloat
| 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