basic json support
This includes a generic JSONStream library built on top of Text.JSON (somewhat hackishly). It would be possible to stream out a single json document describing all actions, but it's probably better for consumers if they can expect one json document per line, so I did it that way instead. Output from external programs used for transferring files is not currently hidden when outputting json, which probably makes it not very useful there. This may be dealt with if there is demand for json output for --get or --move to be parsable. The version, status, and find subcommands have hand-crafted output and don't do json. The whereis subcommand needs to be modified to produce useful json.
This commit is contained in:
parent
57dd34c6be
commit
2f4d4d1c45
9 changed files with 123 additions and 22 deletions
44
Utility/JSONStream.hs
Normal file
44
Utility/JSONStream.hs
Normal file
|
@ -0,0 +1,44 @@
|
|||
{- Streaming JSON output.
|
||||
-
|
||||
- Copyright 2011 Joey Hess <joey@kitenet.net>
|
||||
-
|
||||
- Licensed under the GNU GPL version 3 or higher.
|
||||
-}
|
||||
|
||||
module Utility.JSONStream (
|
||||
start,
|
||||
add,
|
||||
end
|
||||
) where
|
||||
|
||||
import Text.JSON
|
||||
|
||||
{- Text.JSON does not support building up a larger JSON document piece by
|
||||
piece as a stream. To support streaming, a hack. The JSObject is converted
|
||||
to a string with its final "}" is left off, allowing it to be added to
|
||||
later. -}
|
||||
start :: JSON a => [(String, a)] -> String
|
||||
start l
|
||||
| last s == endchar = take (length s - 1) s
|
||||
| otherwise = bad s
|
||||
where
|
||||
s = encodeStrict $ toJSObject l
|
||||
|
||||
add :: JSON a => [(String, a)] -> String
|
||||
add l
|
||||
| head s == startchar = ',' : drop 1 s
|
||||
| otherwise = bad s
|
||||
where
|
||||
s = start l
|
||||
|
||||
end :: String
|
||||
end = [endchar, '\n']
|
||||
|
||||
startchar :: Char
|
||||
startchar = '{'
|
||||
|
||||
endchar :: Char
|
||||
endchar = '}'
|
||||
|
||||
bad :: String -> a
|
||||
bad s = error $ "Text.JSON returned unexpected string: " ++ s
|
Loading…
Add table
Add a link
Reference in a new issue