aboutsummaryrefslogtreecommitdiff
path: root/src/Text/Pandoc
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2013-07-01 16:28:22 -0700
committerJohn MacFarlane <jgm@berkeley.edu>2013-07-01 16:28:22 -0700
commit55c8003e22deba0fc55013e187c5fd0f7d93e5ce (patch)
tree5bbbe1f0bce960af3134fc68b5ca24e47666e48b /src/Text/Pandoc
parent21a9b446093ed49c2067148b6a7701142a16b6a7 (diff)
downloadpandoc-55c8003e22deba0fc55013e187c5fd0f7d93e5ce.tar.gz
Shared: Export getField.
Diffstat (limited to 'src/Text/Pandoc')
-rw-r--r--src/Text/Pandoc/Shared.hs15
1 files changed, 14 insertions, 1 deletions
diff --git a/src/Text/Pandoc/Shared.hs b/src/Text/Pandoc/Shared.hs
index 9ee6b0f15..c9b5842b3 100644
--- a/src/Text/Pandoc/Shared.hs
+++ b/src/Text/Pandoc/Shared.hs
@@ -64,6 +64,7 @@ module Text.Pandoc.Shared (
addMetaField,
makeMeta,
metaToJSON,
+ getField,
setField,
defField,
-- * TagSoup HTML handling
@@ -113,7 +114,7 @@ import Network.Browser (browse, setAllowRedirects, setOutHandler, request)
import qualified Data.Traversable as Traversable
import qualified Data.HashMap.Strict as H
import qualified Data.Text as T
-import Data.Aeson (ToJSON (..), Value(Object), Result(..), fromJSON)
+import Data.Aeson (FromJSON(..), fromJSON, ToJSON (..), Value(Object), Result(..))
#ifdef EMBED_DATA_FILES
import Text.Pandoc.Data (dataFiles)
@@ -554,6 +555,18 @@ metaValueToJSON _ _ (MetaString s) = return $ toJSON s
metaValueToJSON blockWriter _ (MetaBlocks bs) = liftM toJSON $ blockWriter bs
metaValueToJSON _ inlineWriter (MetaInlines bs) = liftM toJSON $ inlineWriter bs
+-- | Retrieve a field value from a JSON object.
+getField :: FromJSON a
+ => String
+ -> Value
+ -> Maybe a
+getField field (Object hashmap) = do
+ result <- H.lookup (T.pack field) hashmap
+ case fromJSON result of
+ Success x -> return x
+ _ -> fail "Could not convert from JSON"
+getField field _ = fail "Not a JSON object"
+
setField :: ToJSON a
=> String
-> a