Go to file
archaeron 72bfaf2046 Merge pull request #1 from mcgordonite/master
Updates for psc 0.10.5.
2017-02-06 21:11:26 +00:00
docs/Data/YAML/Foreign Updates for psc 0.10.5. 2017-02-04 18:56:10 +00:00
src/Data/YAML/Foreign Updates for psc 0.10.5. 2017-02-04 18:56:10 +00:00
test Updates for psc 0.10.5. 2017-02-04 18:56:10 +00:00
.gitignore en- and decode YAML 2015-12-20 15:35:04 +01:00
bower.json Updates for psc 0.10.5. 2017-02-04 18:56:10 +00:00
LICENSE Initial commit 2015-12-19 14:01:33 +01:00
package.json en- and decode YAML 2015-12-20 15:35:04 +01:00
README.md en- and decode YAML 2015-12-20 15:35:04 +01:00

purescript-yaml

data Point = Point Int Int

data Mobility
    = Fix
    | Flex

data GeoObject = GeoObject
    { name :: String
    , scale :: Number
    , points :: Array Point
    , mobility :: Mobility
    , coverage :: Number
    }

Decode YAML

Write IsForeign instances for your data structures.

instance pointIsForeign :: IsForeign Point where
    read value = do
        x <- readProp "X" value
        y <- readProp "Y" value
        return $ Point x y

instance mobilityIsForeign :: IsForeign Mobility where
    read value = do
        mob <- readString value
        case mob of
            "Fix" -> return Fix
            "Flex" -> return Flex
            _ -> Left $ JSONError "Mobility must be either Flex or Fix"

instance archiObjectIsForeign :: IsForeign GeoObject where
    read value = do
        name <- readProp "Name"  value
        scale <- readProp "Scale"  value
        points <- readProp "Points"  value
        mobility <- readProp "Mobility"  value
        coverage <- readProp "Coverage"  value
        return $ GeoObject { name, scale, points, mobility, coverage }

Read the YAML into your data structures.

yamlInput :: String
yamlInput = """
- Name: House
  Scale: 9.5
  Points:
  - X: 10
    Y: 10
  - X: 20
    Y: 10
  - X: 5
    Y: 5
  Mobility: Fix
  Coverage: 10
- Name: Tree
  Scale: 1
  Points:
  - X: 1
    Y: 1
  - X: 2
    Y: 2
  - X: 0
    Y: 0
  Mobility: Fix
  Coverage: 10
"""

decoded = (readYAML yamlInput) :: F (Array GeoObject)

Encode YAML

instance pointToYAML :: ToYAML Point where
    toYAML (Point x y) =
        object
            [ "X" := x
            , "Y" := y
            ]

instance mobilityToYAML :: ToYAML Mobility where
    toYAML Fix = toYAML "Fix"
    toYAML Flex = toYAML "Flex"

instance archiObjectToYAML :: ToYAML GeoObject where
    toYAML (GeoObject o) =
        object
            [ "Name" := o.name
            , "Scale" := o.scale
            , "Points" := o.points
            , "Mobility" := o.mobility
            , "Coverage" := o.coverage
            ]
data :: Array GeoObject
data =
    [ GeoObject
        { coverage: 10.0
        , mobility: Fix
        , name: "House"
        , points: [ Point 10 10, Point 20 10, Point 5 5 ]
        , scale: 9.5
        }
    , GeoObject
        { coverage: 10.0
        , mobility: Fix
        , name: "Tree"
        , points: [ Point 1 1, Point 2 2, Point 0 0 ]
        , scale: 1.0
        }
    ]

encoded = printYAML data