git.iko.soy/typeable/hjsonpointer
on master help sign in
44 lines 1.3 kB history raw
1# Summary
3[JSON Pointer](http://tools.ietf.org/html/rfc6901) library for Haskell.
5[Hackage](https://hackage.haskell.org/package/hjsonpointer) / [GitHub](https://github.com/seagreen/hjsonpointer) / [Travis CI](https://travis-ci.org/seagreen/hjsonpointer)
7# Example
9```haskell
10module Example where
12import Control.Monad (unless)
13import Data.Aeson
14import qualified JSONPointer as JP
16main :: IO ()
17main = do
18 -- JSON Pointers must either be empty or start with a /.
19 pntr1 <- case JP.unescape "/foo/0" of
20 Left _ -> error "Failed to construct JSON Pointer."
21 Right pntr -> return pntr
23 -- We can also write JSON Pointers in Haskell.
24 let pntr2 = JP.Pointer [JP.Token "/"]
25 -- When we do this we don't have to escape / or ~ characters
26 -- (as ~1 and ~0 respectively) like we do in an escaped JSON
27 -- Pointer string.
28 unless (JP.unescape "/~1" == Right pntr2) (error "ohno!")
30 print (JP.resolve pntr1 document)
31 print (JP.resolve pntr2 document)
33 where
34 document :: Value
35 document = object [ "foo" .= [String "bar", String "baz"]
36 , "/" .= String "quux"
37 ]
38```
40Output:
41```
42Right (String "bar")
43Right (String "quux")
44```