{-# LANGUAGE OverloadedStrings, FlexibleContexts, DeriveDataTypeable #-}-- |A collection of convenience functions for using and parsing JSON values-- within 'WD'. All monadic parse errors are converted to asynchronous -- 'BadJSON' exceptions.moduleTest.WebDriver.JSON(-- * Access a JSON object key(!:)-- * Conversion from JSON within WD-- |Apostrophes are used to disambiguate these functions-- from their "Data.Aeson" counterparts.,parseJSON',fromJSON'-- * Tuple functions-- |Convenience functions for working with tuples.-- ** JSON object constructors,single,pair,triple-- ** Extracting JSON objects into tuples,parsePair,parseTriple-- * Conversion from parser results to WD-- |These functions are used to implement the other functions-- in this module, and could be used to implement other JSON -- convenience functions,apResultToWD,aesonResultToWD-- * Parse exception,BadJSON(..))whereimportData.AesonasAesonimportData.Aeson.TypesimportData.Text(Text)importData.ByteString.Lazy.Char8(ByteString)importData.Attoparsec.ByteString.Lazy(Result(..))importqualifiedData.Attoparsec.ByteString.LazyasAPimportControl.ApplicativeimportControl.Monad.Trans.ControlimportControl.Exception.LiftedimportData.StringimportData.TypeableinstanceExceptionBadJSON-- |An error occured when parsing a JSON value.newtypeBadJSON=BadJSONStringderiving(Eq,Show,Typeable)-- |Construct a singleton JSON 'object' from a key and value.single::ToJSONa=>Text->a->Valuesingleax=object[a.=x]-- |Construct a 2-element JSON 'object' from a pair of keys and a pair of -- values. pair::(ToJSONa,ToJSONb)=>(Text,Text)->(a,b)->Valuepair(a,b)(x,y)=object[a.=x,b.=y]-- |Construct a 3-element JSON 'object' from a triple of keys and a triple of-- values.triple::(ToJSONa,ToJSONb,ToJSONc)=>(Text,Text,Text)->(a,b,c)->Valuetriple(a,b,c)(x,y,z)=object[a.=x,b.=y,c.=z]-- |Parse a lazy 'ByteString' as a top-level JSON 'Value', then convert it to an-- instance of 'FromJSON'..parseJSON'::MonadBaseControlIOwd=>FromJSONa=>ByteString->wdaparseJSON'=apResultToWD.AP.parsejson-- |Convert a JSON 'Value' to an instance of 'FromJSON'.fromJSON'::MonadBaseControlIOwd=>FromJSONa=>Value->wdafromJSON'=aesonResultToWD.fromJSON-- |This operator is a wrapper over Aeson's '.:' operator.(!:)::(MonadBaseControlIOwd,FromJSONa)=>Object->Text->wdao!:k=aesonResultToWD$parse(.:k)o-- |Parse a JSON 'Object' as a pair. The first two string arguments specify the-- keys to extract from the object. The third string is the name of the-- calling function, for better error reporting.parsePair::(MonadBaseControlIOwd,FromJSONa,FromJSONb)=>String->String->String->Value->wd(a,b)parsePairabfuncNamev=casevofObjecto->(,)<$>o!:fromStringa<*>o!:fromStringb_->throwIO.BadJSON$funcName++": cannot parse non-object JSON response as a ("++a++", "++b++") pair"++")"-- |Parse a JSON Object as a triple. The first three string arguments-- specify the keys to extract from the object. The fourth string is the name -- of the calling function, for better error reporting.parseTriple::(MonadBaseControlIOwd,FromJSONa,FromJSONb,FromJSONc)=>String->String->String->String->Value->wd(a,b,c)parseTripleabcfuncNamev=casevofObjecto->(,,)<$>o!:fromStringa<*>o!:fromStringb<*>o!:fromStringc_->throwIO.BadJSON$funcName++": cannot parse non-object JSON response as a ("++a++", "++b++", "++c++") pair"-- |Convert an attoparsec parser result to 'WD'. apResultToWD::(MonadBaseControlIOwd,FromJSONa)=>AP.ResultValue->wdaapResultToWDp=casepofDone_res->fromJSON'resFail__err->throwIO$BadJSONerr-- |Convert an Aeson parser result to 'WD'.aesonResultToWD::(MonadBaseControlIOwd)=>Aeson.Resulta->wdaaesonResultToWDr=caserofSuccessval->returnvalErrorerr->throwIO$BadJSONerr