From 0ca467981e17813d497ab13c01f5fcea79e51bb4 Mon Sep 17 00:00:00 2001 From: Robbie McMichael <2044464+robbiemcmichael@users.noreply.github.com> Date: Sun, 22 Sep 2019 22:27:03 +1000 Subject: [PATCH] Add functions for formatting JSONPaths Exposes functions for formatting a `JSONPath` as a `String` so that they can be used for more than formatting error messages. --- Data/Aeson/Types.hs | 2 ++ Data/Aeson/Types/Internal.hs | 14 +++++++++++++- tests/UnitTests.hs | 15 ++++++++++++++- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/Data/Aeson/Types.hs b/Data/Aeson/Types.hs index 690b6af86..6f52f355d 100644 --- a/Data/Aeson/Types.hs +++ b/Data/Aeson/Types.hs @@ -141,6 +141,8 @@ module Data.Aeson.Types , () , JSONPath , JSONPathElement(..) + , formatPath + , formatRelativePath ) where import Prelude.Compat diff --git a/Data/Aeson/Types/Internal.hs b/Data/Aeson/Types/Internal.hs index 17cde38db..a015c40b1 100644 --- a/Data/Aeson/Types/Internal.hs +++ b/Data/Aeson/Types/Internal.hs @@ -49,6 +49,8 @@ module Data.Aeson.Types.Internal , parserThrowError , parserCatchError , formatError + , formatPath + , formatRelativePath , () -- * Constructors and accessors , object @@ -458,7 +460,17 @@ parseEither m v = runParser (m v) [] onError Right -- | Annotate an error message with a -- error location. formatError :: JSONPath -> String -> String -formatError path msg = "Error in " ++ format "$" path ++ ": " ++ msg +formatError path msg = "Error in " ++ formatPath path ++ ": " ++ msg + +-- | Format a as a 'String', +-- representing the root object as @$@. +formatPath :: JSONPath -> String +formatPath path = "$" ++ formatRelativePath path + +-- | Format a as a 'String' +-- which represents the path relative to some root object. +formatRelativePath :: JSONPath -> String +formatRelativePath path = format "" path where format :: String -> JSONPath -> String format pfx [] = pfx diff --git a/tests/UnitTests.hs b/tests/UnitTests.hs index d4a5119ce..153c26078 100644 --- a/tests/UnitTests.hs +++ b/tests/UnitTests.hs @@ -34,7 +34,8 @@ import Data.Aeson.Parser , json', jsonLast', jsonAccum', jsonNoDup') import Data.Aeson.Types ( Options(..), Result(Success), ToJSON(..), Value(Array, Bool, Null, Object) - , camelTo, camelTo2, defaultOptions, omitNothingFields, parse) + , camelTo, camelTo2, defaultOptions, formatPath, formatRelativePath + , omitNothingFields, parse) import Data.Attoparsec.ByteString (Parser, parseOnly) import Data.Char (toUpper) import Data.Either.Compat (isLeft, isRight) @@ -253,6 +254,18 @@ formatErrorExample = lhs = "Error in $[0].foo.bar['a.b.c']['']['\\'\\\\'].end: error msg" in assertEqual "formatError example" lhs rhs +formatPathExample :: Assertion +formatPathExample = + let rhs = formatPath [Key "x", Index 0] + lhs = "$.x[0]" + in assertEqual "formatPath example" lhs rhs + +formatRelativePathExample :: Assertion +formatRelativePathExample = + let rhs = formatPath [Key "x", Index 0] + lhs = ".x[0]" + in assertEqual "formatRelativePath example" lhs rhs + ------------------------------------------------------------------------------ -- Comparison (.:?) and (.:!) ------------------------------------------------------------------------------