{-# LANGUAGE OverloadedStrings #-}
module Data.Geospatial.Internal.Geometry.Aeson (
readGeometryGeoAeson
, makeGeometryGeoAeson
, optValFromObj
, optAttributes
) where
import Control.Applicative ((<$>))
import Control.Monad (mzero)
import Data.Aeson (FromJSON (..), Object, ToJSON (..), Value,
object, (.:), (.:?), (.=))
import Data.Aeson.Types (Pair, Parser)
import Data.Maybe (Maybe (..))
import Data.Text (Text)
readGeometryGeoAeson :: (FromJSON a, FromJSON b) => String -> (a -> b) -> Object -> Parser b
readGeometryGeoAeson geomTypeString geomType geopointObj = do
geometryType <- geopointObj .: "type"
if geometryType == geomTypeString
then
geomType <$> geopointObj .: "coordinates"
else
mzero
makeGeometryGeoAeson :: (ToJSON a) => String -> a -> Value
makeGeometryGeoAeson typeString coordinates =
object ["type" .= typeString, "coordinates" .= coordinates]
optValFromObj :: (FromJSON a) => Text -> Object -> Parser (Maybe a)
optValFromObj = flip (.:?)
optAttributes :: (ToJSON a) => Text -> Maybe a -> [Pair]
optAttributes _ Nothing = []
optAttributes name (Just x) = [name .= x]