Skip to content

Instantly share code, notes, and snippets.

@hansonkd
Created August 22, 2012 06:06
Show Gist options
  • Save hansonkd/3422759 to your computer and use it in GitHub Desktop.
Save hansonkd/3422759 to your computer and use it in GitHub Desktop.
Fay JSON
{-# LANGUAGE NoImplicitPrelude #-}
module Console where
import Language.Fay.FFI
import Language.Fay.Prelude
data MyData = MyData { xVar :: Int, yVar :: Int }
myData :: MyData
myData = MyData { yVar = 3, xVar = 9 }
main = do
jsonSerialized <- toJSON myData
jsonDeserialized <- toMyData jsonSerialized
printBool $ (xVar myData == xVar jsonDeserialized)
print "Now Testing Data Generated by Aeson Library"
aesonData <- toMyData "{\"xVar\":3,\"yVar\":-1}"
printInt $ yVar aesonData
-- | Print using console.log.
print :: String -> Fay ()
print = ffi "console.log(%1)"
printBool :: Bool -> Fay ()
printBool = ffi "console.log(%1)"
printInt :: Int -> Fay ()
printInt = ffi "console.log(%1)"
toMyData :: String -> Fay MyData
toMyData = ffi "JSON.parse(%1)"
toJSON :: MyData -> Fay String
toJSON = ffi "JSON.stringify(%1)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment