mirror of
https://github.com/typeable/hjsonpointer.git
synced 2024-10-26 16:08:30 +03:00
e9b9827cef
+ Rename the module to `JSONPointer`. + Bump aeson. + Derive `Semigroup` for `Pointer`. + Reorganize code.
30 lines
922 B
Haskell
30 lines
922 B
Haskell
|
|
module Example where
|
|
|
|
import Control.Monad (unless)
|
|
import Data.Aeson
|
|
import qualified JSONPointer as JP
|
|
|
|
main :: IO ()
|
|
main = do
|
|
-- JSON Pointers must either be empty or start with a /.
|
|
pntr1 <- case JP.unescape "/foo/0" of
|
|
Left _ -> error "Failed to construct JSON Pointer."
|
|
Right pntr -> return pntr
|
|
|
|
-- We can also write JSON Pointers in Haskell.
|
|
let pntr2 = JP.Pointer [JP.Token "/"]
|
|
-- When we do this we don't have to escape / or ~ characters
|
|
-- (as ~1 and ~0 respectively) like we do in an escaped JSON
|
|
-- Pointer string.
|
|
unless (JP.unescape "/~1" == Right pntr2) (error "ohno!")
|
|
|
|
print (JP.resolve pntr1 document)
|
|
print (JP.resolve pntr2 document)
|
|
|
|
where
|
|
document :: Value
|
|
document = object [ "foo" .= [String "bar", String "baz"]
|
|
, "/" .= String "quux"
|
|
]
|