on master help sign in
Merge pull request #729 from robbiemcmichael/format-path
98bbc5bAdam Bergmark
Data/Aeson/Types.hs+2
@@ -141,6 +141,8 @@ module Data.Aeson.Types
141 , (<?>)
141 , (<?>)
142 , JSONPath
142 , JSONPath
143 , JSONPathElement(..)
143 , JSONPathElement(..)
144+ , formatPath
145+ , formatRelativePath
144 ) where
146 ) where
145
147
146import Prelude.Compat
148import Prelude.Compat
Data/Aeson/Types/Internal.hs+13−1
@@ -49,6 +49,8 @@ module Data.Aeson.Types.Internal
49 , parserThrowError
49 , parserThrowError
50 , parserCatchError
50 , parserCatchError
51 , formatError
51 , formatError
52+ , formatPath
53+ , formatRelativePath
52 , (<?>)
54 , (<?>)
53 -- * Constructors and accessors
55 -- * Constructors and accessors
54 , object
56 , object
@@ -458,7 +460,17 @@ parseEither m v = runParser (m v) [] onError Right
458-- | Annotate an error message with a
460-- | Annotate an error message with a
459-- <http://goessner.net/articles/JsonPath/ JSONPath> error location.
461-- <http://goessner.net/articles/JsonPath/ JSONPath> error location.
460formatError :: JSONPath -> String -> String
462formatError :: JSONPath -> String -> String
461−formatError path msg = "Error in " ++ format "$" path ++ ": " ++ msg
463+formatError path msg = "Error in " ++ formatPath path ++ ": " ++ msg
464+
465+-- | Format a <http://goessner.net/articles/JsonPath/ JSONPath> as a 'String',
466+-- representing the root object as @$@.
467+formatPath :: JSONPath -> String
468+formatPath path = "$" ++ formatRelativePath path
469+
470+-- | Format a <http://goessner.net/articles/JsonPath/ JSONPath> as a 'String'
471+-- which represents the path relative to some root object.
472+formatRelativePath :: JSONPath -> String
473+formatRelativePath path = format "" path
462 where
474 where
463 format :: String -> JSONPath -> String
475 format :: String -> JSONPath -> String
464 format pfx [] = pfx
476 format pfx [] = pfx
tests/UnitTests.hs+14−1
@@ -34,7 +34,8 @@ import Data.Aeson.Parser
34 , json', jsonLast', jsonAccum', jsonNoDup')
34 , json', jsonLast', jsonAccum', jsonNoDup')
35import Data.Aeson.Types
35import Data.Aeson.Types
36 ( Options(..), Result(Success), ToJSON(..), Value(Array, Bool, Null, Object)
36 ( Options(..), Result(Success), ToJSON(..), Value(Array, Bool, Null, Object)
37+ , camelTo, camelTo2, defaultOptions, formatPath, formatRelativePath
37− , camelTo, camelTo2, defaultOptions, omitNothingFields, parse)
38+ , omitNothingFields, parse)
38import Data.Attoparsec.ByteString (Parser, parseOnly)
39import Data.Attoparsec.ByteString (Parser, parseOnly)
39import Data.Char (toUpper)
40import Data.Char (toUpper)
40import Data.Either.Compat (isLeft, isRight)
41import Data.Either.Compat (isLeft, isRight)
@@ -253,6 +254,18 @@ formatErrorExample =
253 lhs = "Error in $[0].foo.bar['a.b.c']['']['\\'\\\\'].end: error msg"
254 lhs = "Error in $[0].foo.bar['a.b.c']['']['\\'\\\\'].end: error msg"
254 in assertEqual "formatError example" lhs rhs
255 in assertEqual "formatError example" lhs rhs
255
256
257+formatPathExample :: Assertion
258+formatPathExample =
259+ let rhs = formatPath [Key "x", Index 0]
260+ lhs = "$.x[0]"
261+ in assertEqual "formatPath example" lhs rhs
262+
263+formatRelativePathExample :: Assertion
264+formatRelativePathExample =
265+ let rhs = formatPath [Key "x", Index 0]
266+ lhs = ".x[0]"
267+ in assertEqual "formatRelativePath example" lhs rhs
268+
256------------------------------------------------------------------------------
269------------------------------------------------------------------------------
257-- Comparison (.:?) and (.:!)
270-- Comparison (.:?) and (.:!)
258------------------------------------------------------------------------------
271------------------------------------------------------------------------------