Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Added eitherDecode and eitherDecode' #69

Merged
merged 1 commit into from

6 participants

@basvandijk
Collaborator

Hi Bryan,

When debugging REST API's that accept and return JSON it's really helpful to receive error messages why JSON decoding failed. The attached patch adds eitherDecode and eitherDecode' which are just like decode and decode' respectively but return an error message when decoding fails.

I'm not sure about the names yet but I couldn't think of something better.

Bas

@basvandijk basvandijk Added eitherDecode and eitherDecode'
They are just like decode and decode' respectively
but return an error message when decoding fails.
41daf5d
@hvr

+1

I usually define helper functions with that very type signature in almost every program of mine which uses aeson... :-)

@ocharles

+1! This is really important, and I'm currently doing some overly painful debugging to figure out why I'm getting Nothing back

@tibbe

+1

I have needed this as well. Like Bas I'm not sure about the names.

@dysinger

+1 on the need! this exists in other libs like yaml (for example) as is really helpful to understand what is not formatting correctly. Getting a simple Nothing is :/

@bos bos merged commit 1fd60bc into bos:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 13, 2012
  1. @basvandijk

    Added eitherDecode and eitherDecode'

    basvandijk authored
    They are just like decode and decode' respectively
    but return an error message when decoding fails.
This page is out of date. Refresh to see the latest.
Showing with 23 additions and 1 deletion.
  1. +13 −1 Data/Aeson.hs
  2. +10 −0 Data/Aeson/Parser/Internal.hs
View
14 Data/Aeson.hs
@@ -16,6 +16,8 @@ module Data.Aeson
-- * Encoding and decoding
decode
, decode'
+ , eitherDecode
+ , eitherDecode'
, encode
-- * Core JSON types
, Value(..)
@@ -40,7 +42,7 @@ module Data.Aeson
) where
import Data.Aeson.Encode (encode)
-import Data.Aeson.Parser.Internal (decodeWith, json, json')
+import Data.Aeson.Parser.Internal (decodeWith, eitherDecodeWith, json, json')
import Data.Aeson.Types
import qualified Data.ByteString.Lazy as L
@@ -63,3 +65,13 @@ decode = decodeWith json fromJSON
decode' :: (FromJSON a) => L.ByteString -> Maybe a
decode' = decodeWith json' fromJSON
{-# INLINE decode' #-}
+
+-- | Like 'decode' but returns an error message when decoding fails.
+eitherDecode :: (FromJSON a) => L.ByteString -> Either String a
+eitherDecode = eitherDecodeWith json fromJSON
+{-# INLINE eitherDecode #-}
+
+-- | Like 'decode'' but returns an error message when decoding fails.
+eitherDecode' :: (FromJSON a) => L.ByteString -> Either String a
+eitherDecode' = eitherDecodeWith json' fromJSON
+{-# INLINE eitherDecode' #-}
View
10 Data/Aeson/Parser/Internal.hs
@@ -23,6 +23,7 @@ module Data.Aeson.Parser.Internal
, value'
-- * Helpers
, decodeWith
+ , eitherDecodeWith
) where
import Blaze.ByteString.Builder (fromByteString, toByteString)
@@ -238,6 +239,15 @@ decodeWith p to s =
_ -> Nothing
{-# INLINE decodeWith #-}
+eitherDecodeWith :: Parser Value -> (Value -> Result a) -> L.ByteString -> Either String a
+eitherDecodeWith p to s =
+ case L.parse p s of
+ L.Done _ v -> case to v of
+ Success a -> Right a
+ Error msg -> Left msg
+ L.Fail _ _ msg -> Left msg
+{-# INLINE eitherDecodeWith #-}
+
-- $lazy
--
-- The 'json' and 'value' parsers decouple identification from
Something went wrong with that request. Please try again.