pt2121
pt2121

Reputation: 11870

purescript-argonaut-generic gDecodeJson: 'tag' property is missing"

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?

Upvotes: 0

Views: 159

Answers (1)

gb.
gb.

Reputation: 4649

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.

Upvotes: 1

Related Questions