on master help sign in
Resolve #311
commitd4fa3312b189995545adbe5deea0005ad5146efe authorOleg Grenrus <oleg.grenrus@iki.fi> dateTue 2 Feb 2016, 20:39 (11 years ago) parentfdb8672
1 file changed+21−2
Data/Aeson/Types/Internal.hs+21−2
Data/Aeson/Types/Internal.hs+21−2
@@ -67,7 +67,7 @@ import Control.DeepSeq (NFData(..))
6767import Control.Monad (MonadPlus(..), ap)
6868import qualified Control.Monad.Fail as Fail
6969import Data.ByteString.Builder (Builder, char7, toLazyByteString)
70−import Data.Char (isLower, isUpper, toLower)
70+import Data.Char (isLower, isUpper, toLower, isAlpha, isAlphaNum)
7171import Data.Data (Data)
7272import Data.HashMap.Strict (HashMap)
7373import Data.Hashable (Hashable(..))
@@ -459,9 +459,28 @@ parseEither m v = runParser (m v) [] onError Right
459459formatError :: JSONPath -> String -> String
460460formatError path msg = "Error in " ++ (format "$" path) ++ ": " ++ msg
461461 where
462+ format :: String -> JSONPath -> String
462463 format pfx [] = pfx
463464 format pfx (Index idx:parts) = format (pfx ++ "[" ++ show idx ++ "]") parts
464− format pfx (Key key:parts) = format (pfx ++ "." ++ unpack key) parts
465+ format pfx (Key key:parts) = format (pfx ++ formatKey key) parts
466+
467+ formatKey :: Text -> String
468+ formatKey key
469+ | isIdentifierKey strKey = "." ++ strKey
470+ | otherwise = "['" ++ escapeKey strKey ++ "']"
471+ where strKey = unpack key
472+
473+ isIdentifierKey :: String -> Bool
474+ isIdentifierKey [] = False
475+ isIdentifierKey (x:xs) = isAlpha x && all isAlphaNum xs
476+
477+ escapeKey :: String -> String
478+ escapeKey = concatMap escapeChar
479+
480+ escapeChar :: Char -> String
481+ escapeChar '\'' = "\\'"
482+ escapeChar '\\' = "\\\\"
483+ escapeChar c = [c]
465484
466485-- | A key\/value pair for an 'Object'.
467486type Pair = (Text, Value)