[Haskell-beginners] How to parse JSON into existential type

Baa aquagnu at gmail.com
Tue Aug 29 12:36:05 UTC 2017


Hello List!

In some of my JSONs I have part "list of any values", for example:

  [1, "hi", "false", 2.71]

To parse it I create type like:

  data RespItem = forall a. FromJSON a => RespItem {
    riItem :: [a] }

,-------------------------------------------------------------------
| btw, I try `(FromJSON a, Show a, Generic a) => ...` also but don't
| see any difference, so I'm not sure what is right constraints list.
`-------------------------------------------------------------------

And GHC does not allow to derive automatically with "deriving" Show
and Generic instances for existential types, as I see. So I write:

  instance Generic RespItem
  instance Show RespItem

Also this type `RespItem` in part of another one:

  data Resp = Resp {
    rHeadings :: [T.Text]
    , rRow    :: [RespItem]
    } deriving (Generic, Show)

Interesting is that now auto-`deriving` works! OK, but I need to
to instantiate `FromJSON` for RespItem because next step must be
`FromJSON` for Resp. So I'm trying:

  instance FromJSON RespItem where
    parseJSON ??? = ???

and here I get different errors, and absolutely don't understand how to
parse it, I get "ambiguous type error..." and so on (which is right,
sure).


===
Best regards, Paul


More information about the Beginners mailing list