refactor
This commit is contained in:
parent
3e22d60549
commit
e2a69c2cee
2 changed files with 17 additions and 7 deletions
|
@ -80,9 +80,7 @@ complete v = Stream.start v `B.append` Stream.end
|
||||||
progress :: B.ByteString -> Integer -> BytesProcessed -> IO ()
|
progress :: B.ByteString -> Integer -> BytesProcessed -> IO ()
|
||||||
progress jsonbuffer size bytesprocessed = emit $ B.concat
|
progress jsonbuffer size bytesprocessed = emit $ B.concat
|
||||||
[ Stream.start $ Stream.AesonObject o
|
[ Stream.start $ Stream.AesonObject o
|
||||||
, ",\"action\":"
|
, Stream.addNestedObject "action" jsonbuffer
|
||||||
, jsonbuffer
|
|
||||||
, "}"
|
|
||||||
, Stream.end
|
, Stream.end
|
||||||
]
|
]
|
||||||
where
|
where
|
||||||
|
|
|
@ -5,30 +5,32 @@
|
||||||
- License: BSD-2-clause
|
- License: BSD-2-clause
|
||||||
-}
|
-}
|
||||||
|
|
||||||
{-# LANGUAGE GADTs #-}
|
{-# LANGUAGE GADTs, OverloadedStrings #-}
|
||||||
|
|
||||||
module Utility.JSONStream (
|
module Utility.JSONStream (
|
||||||
JSONChunk(..),
|
JSONChunk(..),
|
||||||
start,
|
start,
|
||||||
add,
|
add,
|
||||||
|
addNestedObject,
|
||||||
end
|
end
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Data.Aeson
|
import Data.Aeson
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
import qualified Data.ByteString.Lazy as B
|
import qualified Data.ByteString.Lazy as B
|
||||||
|
import qualified Data.ByteString.Lazy.UTF8 as BU8
|
||||||
import Data.Char
|
import Data.Char
|
||||||
import Data.Word
|
import Data.Word
|
||||||
|
|
||||||
data JSONChunk v where
|
data JSONChunk v where
|
||||||
JSONChunk :: ToJSON v => [(String, v)] -> JSONChunk [(String, v)]
|
|
||||||
AesonObject :: Object -> JSONChunk Object
|
AesonObject :: Object -> JSONChunk Object
|
||||||
|
JSONChunk :: ToJSON v => [(String, v)] -> JSONChunk [(String, v)]
|
||||||
|
|
||||||
encodeJSONChunk :: JSONChunk v -> B.ByteString
|
encodeJSONChunk :: JSONChunk v -> B.ByteString
|
||||||
|
encodeJSONChunk (AesonObject o) = encode o
|
||||||
encodeJSONChunk (JSONChunk l) = encode $ object $ map mkPair l
|
encodeJSONChunk (JSONChunk l) = encode $ object $ map mkPair l
|
||||||
where
|
where
|
||||||
mkPair (s, v) = (T.pack s, toJSON v)
|
mkPair (s, v) = (T.pack s, toJSON v)
|
||||||
encodeJSONChunk (AesonObject o) = encode o
|
|
||||||
|
|
||||||
{- Aeson does not support building up a larger JSON object piece by piece
|
{- Aeson does not support building up a larger JSON object piece by piece
|
||||||
- with streaming output. To support streaming, a hack:
|
- with streaming output. To support streaming, a hack:
|
||||||
|
@ -43,11 +45,21 @@ start a
|
||||||
|
|
||||||
add :: JSONChunk a -> B.ByteString
|
add :: JSONChunk a -> B.ByteString
|
||||||
add a
|
add a
|
||||||
| not (B.null b) && B.head b == startchar = B.cons addchar (B.drop 1 b)
|
| not (B.null b) && B.head b == startchar =
|
||||||
|
B.cons addchar (B.drop 1 b)
|
||||||
| otherwise = bad b
|
| otherwise = bad b
|
||||||
where
|
where
|
||||||
b = start a
|
b = start a
|
||||||
|
|
||||||
|
addNestedObject :: String -> B.ByteString -> B.ByteString
|
||||||
|
addNestedObject s b = B.concat
|
||||||
|
[ ",\""
|
||||||
|
, BU8.fromString s
|
||||||
|
, "\":"
|
||||||
|
, b
|
||||||
|
, "}"
|
||||||
|
]
|
||||||
|
|
||||||
end :: B.ByteString
|
end :: B.ByteString
|
||||||
end = endchar `B.cons` sepchar `B.cons` B.empty
|
end = endchar `B.cons` sepchar `B.cons` B.empty
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue