module Common.JSONOrXML where
import Common.Json
import Common.Result
import qualified Control.Monad.Fail as Fail
import PGIP.Output.Mime
import Text.XML.Light
data JSONOrXML = JSON Json | XML Element
prettyPrint :: JSONOrXML -> String
prettyPrint :: JSONOrXML -> String
prettyPrint (JSON json :: Json
json) = Json -> String
ppJson Json
json
prettyPrint (XML xml :: Element
xml) = Element -> String
ppElement Element
xml
joinData :: (String, JSONOrXML) -> (String, JSONOrXML) -> Result JSONOrXML
joinData :: (String, JSONOrXML) -> (String, JSONOrXML) -> Result JSONOrXML
joinData (str1 :: String
str1, (JSON json1 :: Json
json1)) (str2 :: String
str2, (JSON json2 :: Json
json2)) =
let
elem1 :: (String, Json)
elem1 = (String
str1, Json
json1)
elem2 :: (String, Json)
elem2 = (String
str2, Json
json2)
in
JSONOrXML -> Result JSONOrXML
forall (m :: * -> *) a. Monad m => a -> m a
return (JSONOrXML -> Result JSONOrXML) -> JSONOrXML -> Result JSONOrXML
forall a b. (a -> b) -> a -> b
$ Json -> JSONOrXML
JSON (Json -> JSONOrXML) -> Json -> JSONOrXML
forall a b. (a -> b) -> a -> b
$ [(String, Json)] -> Json
JObject [(String, Json)
elem1, (String, Json)
elem2]
joinData (str1 :: String
str1, (XML xml1 :: Element
xml1)) (str2 :: String
str2, (XML xml2 :: Element
xml2)) =
let
elem1 :: Element
elem1 = QName -> [Attr] -> [Content] -> Maybe Line -> Element
Element (String -> Maybe String -> Maybe String -> QName
QName String
str1 Maybe String
forall a. Maybe a
Nothing Maybe String
forall a. Maybe a
Nothing) [] [Element -> Content
Elem Element
xml1] Maybe Line
forall a. Maybe a
Nothing
elem2 :: Element
elem2 = QName -> [Attr] -> [Content] -> Maybe Line -> Element
Element (String -> Maybe String -> Maybe String -> QName
QName String
str2 Maybe String
forall a. Maybe a
Nothing Maybe String
forall a. Maybe a
Nothing) [] [Element -> Content
Elem Element
xml2] Maybe Line
forall a. Maybe a
Nothing
in
JSONOrXML -> Result JSONOrXML
forall (m :: * -> *) a. Monad m => a -> m a
return (JSONOrXML -> Result JSONOrXML) -> JSONOrXML -> Result JSONOrXML
forall a b. (a -> b) -> a -> b
$ Element -> JSONOrXML
XML (Element -> JSONOrXML) -> Element -> JSONOrXML
forall a b. (a -> b) -> a -> b
$ QName -> [Attr] -> [Content] -> Maybe Line -> Element
Element (String -> Maybe String -> Maybe String -> QName
QName "pair" Maybe String
forall a. Maybe a
Nothing Maybe String
forall a. Maybe a
Nothing) [] [Element -> Content
Elem Element
elem1, Element -> Content
Elem Element
elem2] Maybe Line
forall a. Maybe a
Nothing
joinData _ _ = String -> Result JSONOrXML
forall (m :: * -> *) a. MonadFail m => String -> m a
Fail.fail "Cannot join JSON and XML!"
prettyWithTag :: JSONOrXML -> (String, String)
prettyWithTag :: JSONOrXML -> (String, String)
prettyWithTag (JSON json :: Json
json) = (String
jsonC, JSONOrXML -> String
prettyPrint (Json -> JSONOrXML
JSON Json
json))
prettyWithTag (XML xml :: Element
xml) = (String
xmlC, JSONOrXML -> String
prettyPrint (Element -> JSONOrXML
XML Element
xml))