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