The way I would decode a stream of JSON objects is to use `view Pipes.Aeson.Unchecked.decoded`:

    view Pipes.Aeson.Unchecked.decoded
        :: (Monad m, FromJSON a, ToJSON a)
        => Producer ByteString m r
-> Producer a m (Either (DecodingError, Producer ByteString mr) r)

That will take care of skipping blanks between and after parsed JSON elements:

https://github.com/k0001/pipes-aeson/blob/66d3773a50de27188018e53732a076bf804064f3/src/Pipes/Aeson/Internal.hs#L63

What's happening in your example is that you are parsing one element at a time, so after parsing the last element you are left with "\n" as the remaining input and then asking `pipes-aeson` to decode that single newline character as a JSON object, which causes it to fail. You can either use the `decoded` lens to handle skipping white space when parsing a stream or you can skip the whitespace within your own loop.

On 11/15/15 8:27 PM, Erik Rantapaa wrote:

I'm trying to use pipes-aeson to consume a "line-delimited" JSON file - this is a text file which has one JSON object per line. Some examples of this kind of file may be found at http://jsonstudio.com/resources/

You can see how I'm trying to use Pipes.Aeon.decode to do this at https://gist.github.com/erantapaa/4bf55d38865863529659#file-lib-hs-L90

The problem I'm running into is that Pipes.Aeson.decode seems to give a parse error if it begins parsing at the trailing whitespace at the end of the file. Since almost all text files end with a newline, I get a parse error after successfully parsing all of the objects in the file.

The function test2a in the gist demonstrates this problem -- calling test2a with the empty string returns "got Nothing", but test2a on a bunch of spaces produces "got a parse error".

So I'm thinking that Pipes.Aeson.decode isn't handling whitespace correctly. Or should I be doing something else?

--
You received this message because you are subscribed to the Google Groups "Haskell Pipes" group. To unsubscribe from this group and stop receiving emails from it, send an email to haskell-pipes+unsubscr...@googlegroups.com <mailto:haskell-pipes+unsubscr...@googlegroups.com>. To post to this group, send email to haskell-pipes@googlegroups.com <mailto:haskell-pipes@googlegroups.com>.

--
You received this message because you are subscribed to the Google Groups "Haskell 
Pipes" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to haskell-pipes+unsubscr...@googlegroups.com.
To post to this group, send email to haskell-pipes@googlegroups.com.

Reply via email to