-
Notifications
You must be signed in to change notification settings - Fork 710
Convert Json module from String to ByteString #6948
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 RankNTypes #-} | ||
{-# 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 qualified Data.ByteString.Lazy as LBS | ||
import Data.ByteString.Builder | ||
( Builder, stringUtf8, intDec, toLazyByteString ) | ||
|
||
data Json = JsonArray [Json] | ||
| JsonBool !Bool | ||
| JsonNull | ||
| JsonNumber !Int -- No support for Floats, Doubles just yet | ||
| JsonObject [(String, Json)] | ||
| JsonString !String | ||
deriving Show | ||
|
||
-- | Convert a 'Json' into a 'ByteString' | ||
renderJson :: Json -> LBS.ByteString | ||
renderJson json = toLazyByteString (go json) | ||
where | ||
go (JsonArray objs) = | ||
surround "[" "]" $ mconcat $ intersperse "," $ 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 "," $ map render attrs | ||
where | ||
render (k,v) = (surround "\"" "\"" $ stringUtf8 (escape k)) <> ":" <> go v | ||
go (JsonString s) = surround "\"" "\"" $ stringUtf8 (escape s) | ||
|
||
surround :: Builder -> Builder -> Builder -> Builder | ||
surround begin end middle = mconcat [ begin , middle , end] | ||
|
||
escape :: String -> String | ||
escape ('\"':xs) = "\\\"" <> escape xs | ||
escape ('\\':xs) = "\\\\" <> escape xs | ||
escape ('\'':xs) = "\\\'" <> escape xs | ||
escape (x:xs) = x : escape xs | ||
escape [] = mempty | ||
|
||
-- | A shorthand for building up 'JsonObject's | ||
-- >>> JsonObject [ "a" .= JsonNumber 42, "b" .= JsonBool True ] | ||
-- JsonObject [("a",JsonNumber 42),("b",JsonBool True)] | ||
(.=) :: String -> Json -> (String, Json) | ||
k .= v = (k, v) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is wrong as is, but I'll fix it before merge. One can have older bytestring on newer GHC, though that is contrived corner-case. But Cabal should be exemplary in own definition :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we need a newer bytestring on an older GHC here though?