hjsonpointer/Example.hs

30 lines
922 B
Haskell
Raw Normal View History

2016-02-20 00:47:15 +03:00
2016-07-11 17:39:15 +03:00
module Example where
2016-02-20 00:47:15 +03:00
import Control.Monad (unless)
2016-02-20 00:47:15 +03:00
import Data.Aeson
import qualified JSONPointer as JP
2016-02-20 00:47:15 +03:00
main :: IO ()
main = do
2016-07-13 08:27:35 +03:00
-- JSON Pointers must either be empty or start with a /.
pntr1 <- case JP.unescape "/foo/0" of
2016-07-13 08:27:35 +03:00
Left _ -> error "Failed to construct JSON Pointer."
Right pntr -> return pntr
2016-02-20 00:47:15 +03:00
2016-07-13 08:27:35 +03:00
-- We can also write JSON Pointers in Haskell.
let pntr2 = JP.Pointer [JP.Token "/"]
2016-07-13 08:27:35 +03:00
-- 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!")
2016-02-20 00:47:15 +03:00
print (JP.resolve pntr1 document)
print (JP.resolve pntr2 document)
2016-02-20 00:47:15 +03:00
where
document :: Value
document = object [ "foo" .= [String "bar", String "baz"]
, "/" .= String "quux"
]