-
Notifications
You must be signed in to change notification settings - Fork 696
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Port JSON module to Text based implementation
Changes the internal representation of JSON to Text rather than String, and introduces the buildinfo-components-only flag in the Cabal part to make it easier to stitch back the JSON into an array in cabal-install.
- Loading branch information
Showing
3 changed files
with
66 additions
and
52 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 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,65 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
-- | Extremely simple JSON helper. Don't do anything too fancy with this! | ||
module Distribution.Utils.Json | ||
( Json(..) | ||
, (.=) | ||
, renderJson | ||
) where | ||
|
||
import Data.Text (Text) | ||
import qualified Data.Text as Text | ||
|
||
data Json = JsonArray [Json] | ||
| JsonBool !Bool | ||
| JsonNull | ||
| JsonNumber !Int | ||
| JsonObject [(Text, Json)] | ||
| JsonRaw !Text | ||
| JsonString !Text | ||
|
||
-- | A type to mirror 'ShowS' | ||
type ShowT = Text -> Text | ||
|
||
renderJson :: Json -> ShowT | ||
renderJson (JsonArray objs) = | ||
surround "[" "]" $ intercalate "," $ map renderJson objs | ||
renderJson (JsonBool True) = showText "true" | ||
renderJson (JsonBool False) = showText "false" | ||
renderJson JsonNull = showText "null" | ||
renderJson (JsonNumber n) = showText $ Text.pack (show n) | ||
renderJson (JsonObject attrs) = | ||
surround "{" "}" $ intercalate "," $ map render attrs | ||
where | ||
render (k,v) = (surround "\"" "\"" $ showText' k) . showText ":" . renderJson v | ||
renderJson (JsonString s) = surround "\"" "\"" $ showText' s | ||
renderJson (JsonRaw s) = showText s | ||
|
||
surround :: Text -> Text -> ShowT -> ShowT | ||
surround begin end middle = showText begin . middle . showText end | ||
|
||
showText :: Text -> ShowT | ||
showText = (<>) | ||
|
||
showText' :: Text -> ShowT | ||
showText' xs = showStringWorker xs | ||
where | ||
showStringWorker :: Text -> ShowT | ||
showStringWorker t = | ||
case Text.uncons t of | ||
Just ('\r', as) -> showText "\\r" . showStringWorker as | ||
Just ('\n', as) -> showText "\\n" . showStringWorker as | ||
Just ('\"', as) -> showText "\\\"" . showStringWorker as | ||
Just ('\\', as) -> showText "\\\\" . showStringWorker as | ||
Just (x, as) -> showText (Text.singleton x) . showStringWorker as | ||
Nothing -> showText "" | ||
|
||
intercalate :: Text -> [ShowT] -> ShowT | ||
intercalate sep = go | ||
where | ||
go [] = id | ||
go [x] = x | ||
go (x:xs) = x . showText' sep . go xs | ||
|
||
(.=) :: Text -> Json -> (Text, Json) | ||
k .= v = (k, v) |