ondrap / json-stream

Incremental applicative JSON Haskell parser
BSD 3-Clause "New" or "Revised" License
58 stars 13 forks source link

Conduit? #27

Closed dylex closed 2 years ago

dylex commented 2 years ago

It seems there is a very straight-forward conversion to conduit:

-- |Convert a 'ParseOutput' to a conduit from 'ByteString' input chunks to results, finally returning any parse error or 'Nothing' on success. (Relies on "Data.Conduit.Internal".)
parseConduit :: ParseOutput a -> ConduitT ByteString a m (Maybe String)
parseConduit p0 = ConduitT (parsePipe p0) where
  parsePipe (ParseYield a p) r = HaveOutput (parsePipe p r) a
  parsePipe (ParseNeedData f) r = NeedInput (\i -> parsePipe (f i) r) (\() -> r (Just "Incomplete JSON"))
  parsePipe (ParseFailed e) r = r (Just e)
  parsePipe (ParseDone l) r = Leftover (r Nothing) l

Does this already exist somewhere or in some other library that you know of?