예제 #1
0
 def test_attributes_with_numbers_and_bool(self):
     json_data = {
         "root": {
             "@int": 1,
             "@float": 1.2,
             "@bool": True,
         }
     }
     xml2json.json2xml(json_data)
예제 #2
0
 def test_elements_with_numbers_and_bool(self):
     json_data = {
         "root": {
             "int": 1,
             "float": 1.2,
             "bool": True,
         }
     }
     xml2json.json2xml(json_data)
 def print_xml(self, data):
     jsonstring = json.dumps(data)
     decodedstring = json.loads(jsonstring)
     #print(decodedstring)
     jsondata = {data['name']: jsonstring}
     #print(jsondata)
     #d ={'r':{'@p': 'p1','#text': 't1', 'c': 't2'}}
     print(json2xml(jsondata))
 def print_xml(self, data):
     jsonstring = json.dumps(data)
     decodedstring=json.loads(jsonstring)
     #print(decodedstring)
     jsondata = {data['name']:jsonstring}
     #print(jsondata)
     #d ={'r':{'@p': 'p1','#text': 't1', 'c': 't2'}}
     print(json2xml(jsondata))
    def emit(self, ctx, modules, fd):
        me = ModuleCatalogEmitter()
        result = me.emit(ctx, modules)
        if ctx.opts.outputFormat == 'json':
            self.print_json(result)
        else:
            #self.print_xml(result)
            jsonstring = json.dumps(result)
            decodedstring = json.loads(jsonstring)
            jsondata = {result['name']: decodedstring}
            print(json2xml(jsondata))
            #pyOutput = json2xml(jsondata)
            #splitStr = ""
            #if ("\n" in pyOutput):
            #    splitStr = "}\n<"
            #else:
            #    splitStr = "}<"
            #outputList = pyOutput.split(splitStr)
            # jsonOutputTmp = outputList[0] + "}"
            # outputJson = json.loads(jsonOutputTmp)
            # jsonOutputTmp = json.dumps(outputJson, indent=2)
            #xmlOutputTmp = "<" + outputList[0]
            #xmlOutputTmp = xmlOutputTmp.replace("&lt;", "<")
            #xmlOutputTmp = xmlOutputTmp.replace("&gt;", ">")
            #outputXmlET_tmp = ET.fromstring(xmlOutputTmp)
            #outputXmlET = xml_indent(outputXmlET_tmp, 0)
            #xmlOutputTmp = ET.tostring(outputXmlET)
            #xmlOutputTmp = xmlOutputTmp.replace("<", "&lt;")
            #xmlOutputTmp = xmlOutputTmp.replace(">", "&gt;")
            #print(xmlOutputTmp)
        #if fd.name == '<stdout>':
        #    fn= ""
        #    print(fn)
        #else:
        #    fn=fd.name
        #    print(fn)
        #print(fd.name[1:])

        #if fn != "":
        #fqfd = os.getcwd() + '/' + fn
        #print(fqfd)
        #if os.path.isfile(fqfd):
        #    print("File '%s' exists" % fqfd)
        #    return
        #else:
        #    fd.write("%s" % result)
        #    fd.close()
        #else:
        #    print("output file name can not be determined")
        #    #else:
        #       #     fd.write("%s" % result)
        if fd:
            #            fd.write("%s" % result)
            ###         print json
            fd.write("%s" % json.dumps(result))
            fd.write("\r\n")

            ###         print xml
            jsonstring = json.dumps(result)
            decodedstring = json.loads(jsonstring)
            jsondata = {result['name']: decodedstring}
            print(json2xml(jsondata))
            fd.write("%s" % json2xml(jsondata))
            fd.close()
예제 #6
0
 def to_xml(self):
     """
     Provides a json representation of the invoice.
     """
     return xml2json.json2xml(simplejson.dumps({"Invoice": self}))
예제 #7
0
 def to_xml(self):
     """
     Provides a json representation of the contact.
     """
     return xml2json.json2xml(simplejson.dumps({"Contact": self}))
    def emit(self, ctx, modules, fd):
        me = ModuleCatalogEmitter()
        result = me.emit(ctx, modules)
        if ctx.opts.outputFormat == 'json':
            self.print_json(result)
        else:
            #self.print_xml(result)
            jsonstring = json.dumps(result)
            decodedstring=json.loads(jsonstring)
            jsondata = {result['name']:decodedstring}
            print(json2xml(jsondata))
            #pyOutput = json2xml(jsondata)
            #splitStr = ""
            #if ("\n" in pyOutput):
            #    splitStr = "}\n&lt;"
            #else:
            #    splitStr = "}&lt;"
            #outputList = pyOutput.split(splitStr)
            # jsonOutputTmp = outputList[0] + "}"
            # outputJson = json.loads(jsonOutputTmp)
            # jsonOutputTmp = json.dumps(outputJson, indent=2)
            #xmlOutputTmp = "&lt;" + outputList[0]
            #xmlOutputTmp = xmlOutputTmp.replace("&lt;", "<")
            #xmlOutputTmp = xmlOutputTmp.replace("&gt;", ">")
            #outputXmlET_tmp = ET.fromstring(xmlOutputTmp)
            #outputXmlET = xml_indent(outputXmlET_tmp, 0)
            #xmlOutputTmp = ET.tostring(outputXmlET)
            #xmlOutputTmp = xmlOutputTmp.replace("<", "&lt;")
            #xmlOutputTmp = xmlOutputTmp.replace(">", "&gt;")
            #print(xmlOutputTmp)
        #if fd.name == '<stdout>':
        #    fn= ""
        #    print(fn)
        #else:
        #    fn=fd.name
        #    print(fn)
        #print(fd.name[1:])

        #if fn != "":
                #fqfd = os.getcwd() + '/' + fn
                #print(fqfd)
                #if os.path.isfile(fqfd):
                #    print("File '%s' exists" % fqfd)
                #    return
                #else:
                #    fd.write("%s" % result)
                #    fd.close()
        #else:
        #    print("output file name can not be determined")
        #    #else:
        #       #     fd.write("%s" % result)
        if fd:
#            fd.write("%s" % result)
###         print json
            fd.write("%s" % json.dumps(result))
            fd.write("\r\n")

###         print xml
            jsonstring = json.dumps(result)
            decodedstring=json.loads(jsonstring)
            jsondata = {result['name']:decodedstring}
            print(json2xml(jsondata))
            fd.write("%s" % json2xml(jsondata))
            fd.close()
예제 #9
0
import json
import os
import re
from xml2json import json2xml

def env_replace(json_data):
    for k in json_data:
        v = json_data[k]
        if isinstance(v, (dict,)):
            json_data[k] = env_replace(v)
        if isinstance(v, (unicode,)):
            for ev in icecast_env:
                json_data[k] = re.sub(ev, os.environ.get(ev, ev), json_data[k])
    return json_data

icecast_env = [k for k in os.environ if re.match('^ICECAST_', k)]

for service in ("ez", "icecast"):
    json_data = json.load(open(service + ".json"))
    out_file = file(service + ".xml", "w")
    if len(icecast_env):
        json_data = env_replace(json_data)
    out_file.write(json2xml(json_data))
    out_file.close()