<s> from rdflib . query import Result , ResultException , ResultSerializer , ResultParser <EOL> from rdflib import Literal , URIRef , BNode , Variable <EOL> import jsonlayer <EOL> """<STR_LIT>""" <EOL> class JSONResultParser ( ResultParser ) : <EOL> def parse ( self , source ) : <EOL> return JSONResult ( jsonlayer . decode ( source . read ( ) ) ) <EOL> class JSONResultSerializer ( ResultSerializer ) : <EOL> def __init__ ( self , result ) : <EOL> ResultSerializer . __init__ ( self , result ) <EOL> def serialize ( self , stream , encoding = None ) : <EOL> res = { } <EOL> if self . result . type == '<STR_LIT>' : <EOL> res [ "<STR_LIT>" ] = { } <EOL> res [ "<STR_LIT>" ] = self . result . askAnswer <EOL> else : <EOL> res [ "<STR_LIT>" ] = { } <EOL> res [ "<STR_LIT>" ] = { } <EOL> res [ "<STR_LIT>" ] [ "<STR_LIT>" ] = self . result . vars <EOL> res [ "<STR_LIT>" ] [ "<STR_LIT>" ] = [ self . _bindingToJSON ( x ) for x in self . result . bindings ] <EOL> r = jsonlayer . encode ( res ) <EOL> if encoding != None : <EOL> stream . write ( r . encode ( encoding ) ) <EOL> else : <EOL> stream . write ( r ) <EOL> def _bindingToJSON ( self , b ) : <EOL> res = { } <EOL> for var in b : <EOL> j = termToJSON ( self , b [ var ] ) <EOL> if j != None : <EOL> res [ var ] = termToJSON ( self , b [ var ] ) <EOL> return res <EOL> class JSONResult ( Result ) : <EOL> def __init__ ( self , json ) : <EOL> self . json = json <EOL> if "<STR_LIT>" in json : <EOL> type_ = '<STR_LIT>' <EOL> elif "<STR_LIT>" in json : <EOL> type_ = '<STR_LIT>' <EOL> else : <EOL> raise ResultException ( '<STR_LIT>' ) <EOL> Result . __init__ ( self , type_ ) <EOL> if type_ == '<STR_LIT>' : <EOL> self . askAnswer = bool ( json [ '<STR_LIT>' ] ) <EOL> else : <EOL> self . bindings = self . _get_bindings ( ) <EOL> self . vars = [ Variable ( x ) for x in json [ "<STR_LIT>" ] [ "<STR_LIT>" ] ] <EOL> def _get_bindings ( self ) : <EOL> ret = [ ] <EOL> for row in self . json [ '<STR_LIT>' ] [ '<STR_LIT>' ] : <EOL> outRow = { } <EOL> for k , v in row . items ( ) : <EOL> outRow [ Variable ( k ) ] = parseJsonTerm ( v ) <EOL> ret . append ( outRow ) <EOL> return ret <EOL> def parseJsonTerm ( d ) : <EOL> """<STR_LIT>""" <EOL> t = d [ '<STR_LIT:type>' ] <EOL> if t == '<STR_LIT>' : <EOL> return URIRef ( d [ '<STR_LIT:value>' ] ) <EOL> elif t == '<STR_LIT>' : <EOL> if '<STR_LIT>' in d : <EOL> return Literal ( d [ '<STR_LIT:value>' ] , lang = d [ '<STR_LIT>' ] ) <EOL> return Literal ( d [ '<STR_LIT:value>' ] ) <EOL> elif t == '<STR_LIT>' : <EOL> return Literal ( d [ '<STR_LIT:value>' ] , datatype = URIRef ( d [ '<STR_LIT>' ] ) ) <EOL> elif t == '<STR_LIT>' : <EOL> return BNode ( d [ '<STR_LIT:value>' ] ) <EOL> else : <EOL> raise NotImplementedError ( "<STR_LIT>" % t ) <EOL> def termToJSON ( self , term ) : <EOL> if isinstance ( term , URIRef ) : <EOL> return { '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT:value>' : unicode ( term ) } <EOL> elif isinstance ( term , Literal ) : <EOL> if term . datatype != None : <EOL> return { '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT:value>' : unicode ( term ) , <EOL> '<STR_LIT>' : unicode ( term . datatype ) } <EOL> else : <EOL> r = { '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT:value>' : unicode ( term ) } <EOL> if term . language != None : <EOL> r [ '<STR_LIT>' ] = term . language <EOL> return r <EOL> elif isinstance ( term , BNode ) : <EOL> return { '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT:value>' : str ( term ) } <EOL> elif term == None : <EOL> return None <EOL> else : <EOL> raise ResultException ( '<STR_LIT>' % ( term , type ( term ) ) ) </s>
