Fix mangling of --json output of utf-8 characters when not running in a utf-8 locale

As long as all code imports Utility.Aeson rather than Data.Aeson,
and no Strings that may contain utf-8 characters are used for eg, object
keys via T.pack, this is guaranteed to fix the problem everywhere that
git-annex generates json.

It's kind of annoying to need to wrap ToJSON with a ToJSON', especially
since every data type that has a ToJSON instance has to be ported over.
However, that only took 50 lines of code, which is worth it to ensure full
coverage. I initially tried an alternative approach of a newtype FileEncoded,
which had to be used everywhere a String was fed into aeson, and chasing
down all the sites would have been far too hard. Did consider creating an
intentionally overlapping instance ToJSON String, and letting ghc fail
to build anything that passed in a String, but am not sure that wouldn't
pollute some library that git-annex depends on that happens to use ToJSON
String internally.

This commit was supported by the NSF-funded DataLad project.
This commit is contained in:
Joey Hess 2018-04-16 15:42:45 -04:00
parent 6ddd374935
commit 89e1a05a8f
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
14 changed files with 173 additions and 62 deletions

View file

@ -57,9 +57,8 @@ module Remote (
) where
import Data.Ord
import Data.Aeson
import qualified Data.Map as M
import qualified Data.Text as T
import qualified Data.Vector as V
import Annex.Common
import Types.Remote
@ -74,6 +73,7 @@ import Config
import Config.DynamicConfig
import Git.Types (RemoteName)
import qualified Git
import Utility.Aeson
{- Map from UUIDs of Remotes to a calculated value. -}
remoteMap :: (Remote -> v) -> Annex (M.Map UUID v)
@ -197,7 +197,7 @@ prettyPrintUUIDsDescs header descm uuids =
{- An optional field can be included in the list of UUIDs. -}
prettyPrintUUIDsWith
:: ToJSON v
:: ToJSON' v
=> Maybe String
-> String
-> M.Map UUID RemoteName
@ -206,7 +206,7 @@ prettyPrintUUIDsWith
-> Annex String
prettyPrintUUIDsWith optfield header descm showval uuidvals = do
hereu <- getUUID
maybeShowJSON $ JSONChunk [(header, map (jsonify hereu) uuidvals)]
maybeShowJSON $ JSONChunk [(header, V.fromList $ map (jsonify hereu) uuidvals)]
return $ unwords $ map (\u -> "\t" ++ prettify hereu u ++ "\n") uuidvals
where
finddescription u = M.findWithDefault "" u descm
@ -224,11 +224,11 @@ prettyPrintUUIDsWith optfield header descm showval uuidvals = do
Nothing -> s
Just val -> val ++ ": " ++ s
jsonify hereu (u, optval) = object $ catMaybes
[ Just (T.pack "uuid", toJSON $ fromUUID u)
, Just (T.pack "description", toJSON $ finddescription u)
, Just (T.pack "here", toJSON $ hereu == u)
[ Just (packString "uuid", toJSON' $ fromUUID u)
, Just (packString "description", toJSON' $ finddescription u)
, Just (packString "here", toJSON' $ hereu == u)
, case (optfield, optval) of
(Just field, Just val) -> Just (T.pack field, toJSON val)
(Just field, Just val) -> Just (packString field, toJSON' val)
_ -> Nothing
]