A simple Foreign/JSON library based on the Purescript's RowToList feature.
Requires compiler version 0.11.6 or greater.
See the API Docs or the tests for usage.
I use this in my simple-rpc-telegram-bot project to read configs quickly and easily:
newtype FilePath = FilePath String
derive instance ntFP :: Newtype FilePath _
derive newtype instance rfFP :: ReadForeign FilePath
newtype Token = Token String
derive instance ntT :: Newtype Token _
derive newtype instance rfT :: ReadForeign Token
newtype Id = Id Int
derive instance ntI :: Newtype Id _
derive newtype instance rfI :: ReadForeign Id
type Config =
{ token :: Token
, torscraperPath :: FilePath
, master :: Id
}
getConfig :: IO (F Config)
getConfig = liftAff $ readJSON <$> readTextFile UTF8 "./config.json"You might look at some of these examples for ideas:
- Parsing to a different type and modifying the field https://gist.github.com/justinwoo/9d0bb67a84c227f327da7171bb7105c2
- Untagged sum type parsing https://github.com/justinwoo/untagged-sum-decode-simple-json-example/blob/master/src/Main.purs
- Date parsing to JS Date in Eff https://github.com/justinwoo/date-parsing-simple-json-example/blob/master/src/Main.purs
- Enum-style sum type parsing https://github.com/justinwoo/enum-sum-generics-example-simple-json/blob/master/src/Main.purs
- These and more here https://www.reddit.com/r/purescript/comments/7b5y7q/some_extra_examples_of_simplejson_usage/
This library will decode undefined and null as Nothing and write Nothing as null. Please use the NullOrUndefined type if you'd like to write undefined instead. Please take caution when using Maybe as this default may not be what you want.
See more here: https://github.com/justinwoo/purescript-simple-json/releases/tag/v0.10.0