-
Notifications
You must be signed in to change notification settings - Fork 701
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert Json module from String to ByteString
- Loading branch information
Showing
6 changed files
with
80 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
-- | Extremely simple JSON helper. Don't do anything too fancy with this! | ||
|
||
module Distribution.Utils.Json | ||
( Json(..) | ||
, (.=) | ||
, renderJson | ||
) where | ||
|
||
import Distribution.Compat.Prelude | ||
import Data.ByteString.Lazy (ByteString) | ||
import qualified Data.ByteString.Lazy as L | ||
import qualified Data.ByteString.Lazy.Char8 as C | ||
import Data.ByteString.Builder | ||
|
||
data Json = JsonArray [Json] | ||
| JsonBool !Bool | ||
| JsonNull | ||
| JsonNumber !Int | ||
| JsonObject [(ByteString, Json)] | ||
| JsonString !ByteString | ||
|
||
renderJson :: Json -> L.ByteString | ||
renderJson json = toLazyByteString (go json) | ||
where | ||
go (JsonArray objs) = | ||
surround "[" "]" $ mconcat $ intersperse (stringUtf8 ",") $ map go objs | ||
go (JsonBool True) = stringUtf8 "true" | ||
go (JsonBool False) = stringUtf8 "false" | ||
go JsonNull = stringUtf8 "null" | ||
go (JsonNumber n) = intDec n | ||
go (JsonObject attrs) = | ||
surround "{" "}" $ mconcat $ intersperse (stringUtf8 ",") $ map render attrs | ||
where | ||
render (k,v) = (surround "\"" "\"" $ escape k) <> stringUtf8 ":" <> go v | ||
go (JsonString s) = surround "\"" "\"" $ escape s | ||
|
||
surround :: ByteString -> ByteString -> Builder -> Builder | ||
surround begin end middle = mconcat [ lazyByteString begin , middle , lazyByteString end] | ||
|
||
escape :: ByteString -> Builder | ||
escape = escapeWorker | ||
where | ||
escapeWorker :: ByteString -> Builder | ||
escapeWorker s = | ||
case C.uncons s of | ||
Just ('\"', xs) -> stringUtf8 "\\\"" <> escapeWorker xs | ||
Just ('\\', xs) -> stringUtf8 "\\\\" <> escapeWorker xs | ||
Just ('\'', xs) -> stringUtf8 "\\\'" <> escapeWorker xs | ||
Just (x, xs) -> charUtf8 x <> escapeWorker xs | ||
Nothing -> mempty | ||
|
||
(.=) :: ByteString -> Json -> (ByteString, Json) | ||
k .= v = (k, v) |