Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

decode strict bytestrings? #99

jhickner opened this Issue Dec 10, 2012 · 2 comments


None yet
4 participants

Several times I've run into the situation where I'd like to use Aeson to decode strict bytestrings, as in the case of decoding many very small json files, or when reading from then writing to the same file, in which case the lazy readFile seems to keep the handle open too long in some instances.

I've been using this function, which is just a slight variation on Aeson's decodeWith:

import qualified Data.ByteString as B
import qualified Data.Aeson as A
import qualified Data.Attoparsec as AP

decodeStrict :: A.FromJSON a => B.ByteString -> Maybe a
decodeStrict bs = 
    case AP.parse A.json' bs of
      AP.Done _ v -> case A.fromJSON v of
                       A.Success a -> Just a
                       _            -> Nothing
      _           -> Nothing

It would be nice to have this in the core Aeson package. I can put together a pull request if there's no objection.

I found this issue while looking for the same. The Snaplet.Auth.SqliteSimple backend for the auth snaplet in snap has an AuthUser which uses strict ByteStrings. It would be nice to be able to encode/decode to sqlite directly.

Yes, I would also like this feature.

@bos bos closed this Sep 4, 2013

tolysz pushed a commit to tolysz/aeson that referenced this issue May 18, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment