From: erickson Date: Fri, 21 Dec 2007 13:34:51 +0000 (+0000) Subject: added a flag to encode/decode flattened data as json X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=723456e75752e9b64bd3904af327b54a7d208915;p=opensrf%2Fbjwebb.git added a flag to encode/decode flattened data as json git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@1182 9efc2488-bf62-4759-914b-345cdb29e865 --- diff --git a/src/python/osrf/xml_obj.py b/src/python/osrf/xml_obj.py index e1a7526..f023bc3 100644 --- a/src/python/osrf/xml_obj.py +++ b/src/python/osrf/xml_obj.py @@ -98,6 +98,7 @@ class XMLFlattener(handler.ContentHandler): def __init__(self): self.result = {} self.elements = [] + self.use_json = None def startElement(self, name, attrs): self.elements.append(name) @@ -112,26 +113,39 @@ class XMLFlattener(handler.ContentHandler): key = key[:-1] if key in self.result: - data = self.result[key] + data = self._decode(self.result[key]) if isinstance(data, list): data.append(text) else: data = [data, text] - self.result[key] = data + self.result[key] = self._encode(data) else: - self.result[key] = text + self.result[key] = self._encode(text) def endElement(self, name): self.elements.pop() + def _decode(self, string): + if self.use_json: + return osrf.json.to_object(string) + return string - def __init__(self, xml_str): + def _encode(self, obj): + if self.use_json: + return osrf.json.to_json(obj) + return obj + + + + def __init__(self, xml_str, encode_as_json=False): self.xml_str = xml_str + self.use_json = encode_as_json def parse(self): ''' Parses the XML string and returns the dict of keys/values ''' sax_handler = XMLFlattener.Handler() + sax_handler.use_json = self.use_json parser = make_parser() parser.setContentHandler(sax_handler) try: