0

I'm learning Argonaut and I'm able to decode json manually:

foo = """{"f":"xxx"}"""
newtype Foo =
  Foo {
    f :: String
  }
derive instance genericFoo :: Generic Foo
instance decodeJsonFoo :: DecodeJson Foo where
  decodeJson json = do
    obj <- decodeJson json
    f <- obj .? "f"
    pure $ Foo { f }
main = either log (\x -> log $ show $ decodeJson x :: Either String Foo) (jsonParser foo)

I'm trying to use Generics instead and I thought

instance decodeJsonFoo :: DecodeJson Foo where
  decodeJson = gDecodeJson

would work.

however, I got (Left "When decoding a Main.Foo: 'tag' property is missing")

looked at Generic.purs but I'm new to Generics and can't figure out.

Do I misunderstand something or how can I fix this?

pt2121
  • 11,720
  • 8
  • 52
  • 69

1 Answers1

1

Try encoding Foo { f: "xxx" } with gEncodeJson - you'll see that the output format includes a tag property, which is what the decode is complaining about.

Generic encode/decode is mostly useful when you have it as a serialization format as your app, for cases like this, if you have the Foo value provided from somewhere else it's better to write the codecs yourself.

gb.
  • 4,629
  • 1
  • 20
  • 19