git.iko.soy/typeable/hjsonpointer
on ee70dcb help sign in
46 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
10{-# LANGUAGE OverloadedStrings #-}
12module Main where
14import Control.Monad (unless)
15import Data.Aeson
16import qualified Data.Aeson.Pointer as P
18main :: IO ()
19main = do
20 -- JSON Pointers must either be empty or start with a /.
21 pntr1 <- case P.unescape "/foo/0" of
22 Left _ -> error "Failed to construct JSON Pointer."
23 Right pntr -> pure pntr
25 -- We can also write JSON Pointers in Haskell.
26 let pntr2 = P.Pointer [P.Token "/"]
27 -- When we do this we don't have to escape / or ~ characters
28 -- (as ~1 and ~0 respectively) like we do in an escaped JSON
29 -- Pointer string.
30 unless (P.unescape "/~1" == Right pntr2) (error "ohno!")
32 print (P.resolve pntr1 document)
33 print (P.resolve pntr2 document)
35 where
36 document :: Value
37 document = object [ "foo" .= [String "bar", String "baz"]
38 , "/" .= String "quux"
39 ]
40```
42Output:
43```
44Right (String "bar")
45Right (String "quux")
46```