Allow YAML to be converted into Json

This commit is contained in:
Dominick Gendill 2017-05-09 14:42:47 -06:00
parent fab2d38e08
commit 849484bf3f
2 changed files with 11 additions and 4 deletions

View File

@ -15,7 +15,8 @@
"purescript-functions": "^3.0.0",
"purescript-foreign": "^4.0.0",
"purescript-foreign-generic": "^4.1.0",
"purescript-unsafe-coerce": "^3.0.0"
"purescript-unsafe-coerce": "^3.0.0",
"purescript-argonaut-core": "^3.1.0"
},
"devDependencies": {
"purescript-console": "^3.0.0",

View File

@ -1,12 +1,14 @@
module Data.YAML.Foreign.Decode (parseYAML, readYAMLGeneric) where
module Data.YAML.Foreign.Decode (parseYAML, readYAMLGeneric, parseYAMLToJson) where
import Data.Foreign (F, Foreign, ForeignError(..), fail)
import Data.Foreign.Generic.Class (class GenericDecode)
import Data.Foreign.Generic (genericDecode)
import Data.Foreign.Generic.Class (class GenericDecode)
import Data.Foreign.Generic.Types (Options)
import Data.Function.Uncurried (Fn3, runFn3)
import Data.Generic.Rep (class Generic)
import Prelude ((>=>), (<<<), pure)
import Prelude ((>=>), (<<<), pure, (>>=))
import Unsafe.Coerce (unsafeCoerce)
import Data.Argonaut.Core (Json)
foreign import parseYAMLImpl :: forall r. Fn3 (String -> r) (Foreign -> r) String r
@ -14,6 +16,10 @@ foreign import parseYAMLImpl :: forall r. Fn3 (String -> r) (Foreign -> r) Strin
parseYAML :: String -> F Foreign
parseYAML yaml = runFn3 parseYAMLImpl (fail <<< JSONError) pure yaml
-- | Attempt to parse a YAML string, returning the result as Json
parseYAMLToJson :: String -> F Json
parseYAMLToJson yaml = parseYAML yaml >>= pure <<< unsafeCoerce
-- | Automatically generate a YAML parser for your data from a generic instance.
readYAMLGeneric :: forall a rep. (Generic a rep) => (GenericDecode rep) => Options -> String -> F a
readYAMLGeneric opts = parseYAML >=> genericDecode opts