Skip to content
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

Add formatPath function #729

Merged
merged 1 commit into from
Oct 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Data/Aeson/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ module Data.Aeson.Types
, (<?>)
, JSONPath
, JSONPathElement(..)
, formatPath
, formatRelativePath
) where

import Prelude.Compat
Expand Down
14 changes: 13 additions & 1 deletion Data/Aeson/Types/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ module Data.Aeson.Types.Internal
, parserThrowError
, parserCatchError
, formatError
, formatPath
, formatRelativePath
, (<?>)
-- * Constructors and accessors
, object
Expand Down Expand Up @@ -458,7 +460,17 @@ parseEither m v = runParser (m v) [] onError Right
-- | Annotate an error message with a
-- <http://goessner.net/articles/JsonPath/ JSONPath> error location.
formatError :: JSONPath -> String -> String
formatError path msg = "Error in " ++ format "$" path ++ ": " ++ msg
formatError path msg = "Error in " ++ formatPath path ++ ": " ++ msg

-- | Format a <http://goessner.net/articles/JsonPath/ JSONPath> as a 'String',
-- representing the root object as @$@.
formatPath :: JSONPath -> String
formatPath path = "$" ++ formatRelativePath path

-- | Format a <http://goessner.net/articles/JsonPath/ JSONPath> 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
Expand Down
15 changes: 14 additions & 1 deletion tests/UnitTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 (.:!)
------------------------------------------------------------------------------
Expand Down