Skip to content

Instantly share code, notes, and snippets.

@codedmart
Created December 4, 2015 16:30
Show Gist options
  • Select an option

  • Save codedmart/08d9c416ffa8a9dac1cd to your computer and use it in GitHub Desktop.

Select an option

Save codedmart/08d9c416ffa8a9dac1cd to your computer and use it in GitHub Desktop.
module Types.Foo where
import Prelude
import Data.Argonaut ((~>), (:=), (.?), jsonEmptyObject)
import Data.Argonaut.Encode (EncodeJson, encodeJson)
import Data.Argonaut.Decode (DecodeJson, decodeJson)
import Data.Maybe (Maybe(..))
type Foo =
{ foo :: Maybe Number
, bar :: Maybe String
}
newtype FooNT = FooNT
{ foo :: Maybe Number
, bar :: Maybe String
}
instance decodeJsonFooNT :: DecodeJson FooNT where
decodeJson json = do
obj <- decodeJson json
foo <- obj .? "foo"
bar <- obj .? "bar"
pure $ FooNT {foo: foo, bar: bar}
instance encodeJsonFooNT :: EncodeJson FooNT where
encodeJson (FooNT f)
= "bar" := f.bar
~> "foo" := f.foo
~> jsonEmptyObject
instance showFooNT :: Show FooNT where
show (FooNT f) = "Foo {foo: " ++ show f.foo ++ ", bar:" ++ show f.bar ++ "}"
runFoo :: FooNT -> Foo
runFoo (FooNT f) = f
initialFoo :: Foo
initialFoo =
{ foo: Just 42.0
, bar: Nothing
}
foo :: FooNT
foo = FooNT initialFoo
encodedFoo = encodeJson foo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment