git.iko.soy/typeable/hjsonpointer
on 1.4.0 help sign in
29 lines 922 B history raw
2module Example where
4import Control.Monad (unless)
5import Data.Aeson
6import qualified JSONPointer as JP
8main :: IO ()
9main = do
10 -- JSON Pointers must either be empty or start with a /.
11 pntr1 <- case JP.unescape "/foo/0" of
12 Left _ -> error "Failed to construct JSON Pointer."
13 Right pntr -> return pntr
15 -- We can also write JSON Pointers in Haskell.
16 let pntr2 = JP.Pointer [JP.Token "/"]
17 -- When we do this we don't have to escape / or ~ characters
18 -- (as ~1 and ~0 respectively) like we do in an escaped JSON
19 -- Pointer string.
20 unless (JP.unescape "/~1" == Right pntr2) (error "ohno!")
22 print (JP.resolve pntr1 document)
23 print (JP.resolve pntr2 document)
25 where
26 document :: Value
27 document = object [ "foo" .= [String "bar", String "baz"]
28 , "/" .= String "quux"
29 ]