Skip to content

Instantly share code, notes, and snippets.

@osfameron
Last active September 1, 2020 07:31
Show Gist options
  • Save osfameron/73513bb78e78ed439f3f8a38f1b8be51 to your computer and use it in GitHub Desktop.
Save osfameron/73513bb78e78ed439f3f8a38f1b8be51 to your computer and use it in GitHub Desktop.
Test of updating a partial object from a record. (Rather clumsy)
module JsonTest where
import Prelude (bind, pure, (#), ($), Unit)
import Data.Argonaut.Core (Json, caseJsonObject, fromNumber, fromObject, fromString)
import Data.Argonaut.Decode (JsonDecodeError, decodeJson, parseJson)
import Debug.Trace (spy)
import Effect
import Effect.Console (log)
import Data.Either (Either)
import Foreign.Object as O
type Foo = { foo :: String, bar :: Number }
foo :: Foo
foo = { foo: "Hello", bar: 1.0 }
decodeFoo :: Json -> Either JsonDecodeError Foo
decodeFoo = decodeJson
j :: String
j = """{"foo":"Hello","bar":1.0,"baz":"extra"}"""
main :: Effect Unit
main = do
let result =
do
parsed <- parseJson j
decoded <- decodeFoo parsed
let updated = decoded { foo = "Updated", bar = 2.0 }
let encoded = spy "encode" $ encodeJson updated
let new =
spy "new" $
caseJsonObject
parsed
(\obj -> obj
# O.union (unsafePartial fromJust $ toObject encoded)
# fromObject)
parsed
pure new
log "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment