[Haskell-beginners] lens-aeson and error messages
Thomas Koster
tkoster at gmail.com
Sun Jun 14 09:11:32 UTC 2015
Hi Alexey,
On Fri, Jun 12, 2015 at 7:11 AM, Thomas Koster <tkoster at gmail.com> wrote:
> tl;dr - I am using aeson and have an unusual JSON data structure to
> parse, so I have implemented the parseJSON function using the prisms
> and traversals in the lens-aeson package. Unfortunately, the only error
> message I ever get from my parser is "mempty". How can my parser give
> better error messages when using lens-aeson?
>
> instance FromJSON Row where
> parseJSON v =
> Row <$> (v ^. nth 0 . to parseJSON)
> <*> (v ^. nth 1 . to parseJSON)
> <*> (v ^. nth 2 . to parseJSON)
On 12 June 2015 at 16:28, Alexey Shmalko <rasen.dubi at gmail.com> wrote:
> I would come with implementation similar to this:
>
> instance FromJSON Row where
> parseJSON = withArray "Row" $ \ a ->
> case Vector.length a of
> 3 ->
> Row <$> parseJSON (a ! 0)
> <*> parseJSON (a ! 1)
> <*> parseJSON (a ! 2)
> _ -> fail "Invalid Row."
Thanks for your response.
I was so caught up in trying to do things "the Haskell, functional way",
with pattern matching, that I totally overlooked the plain old (!)
operator!
I have decided that Row is not complex enough to need lens-aeson and
will follow your recommendation.
I might add this (.!) operator to my toolkit though, analogous to (.:), to
remove the need for those "parseJSON"s.
(.!) :: FromJSON a => Array -> Int -> Parser a
I wonder why this is missing from aeson...
I am still interested if an answer to the original question exists,
should I need lens-aeson at some later time.
Thanks,
--
Thomas Koster
More information about the Beginners
mailing list