Ejemplo n.º 1
0
    def __call__(self, model):
        # this part deals with subviews that were already serialized
        cpy = copy.deepcopy(model)
        for key, valstr in model.items():
            if getattr(valstr, '__is_xml__', False):
                cpy[key] = ET.fromstring(valstr)

        def serialize(rootmodel, rootkeyname):
            res = ET.Element(rootkeyname)

            if isinstance(rootmodel, col.Mapping):
                for key in sorted(rootmodel):
                    res.append(serialize(rootmodel[key], key))
            elif (isinstance(rootmodel, col.Sequence)
                  and not isinstance(rootmodel, six.string_types)):
                for val in sorted(rootmodel, key=str):
                    res.append(serialize(val, 'item'))
            elif ET.iselement(rootmodel):
                res.append(rootmodel)
            else:
                res.text = str(rootmodel)

            return res

        str_ = ET.tostring(serialize(cpy, self.wrapper_name),
                           encoding="utf-8").decode("utf-8")
        res = utils.StringWithAttrs(str_)
        res.__is_xml__ = True
        return res
Ejemplo n.º 2
0
    def __call__(self, model):
        # this part deals with subviews that were already serialized
        cpy = copy.deepcopy(model)
        for key, valstr in model.items():
            if getattr(valstr, '__is_json__', False):
                cpy[key] = json.loads(valstr)

        res = utils.StringWithAttrs(json.dumps(cpy.data))
        res.__is_json__ = True
        return res
Ejemplo n.º 3
0
 def __call__(self, model):
     res = utils.StringWithAttrs(json.dumps(model.data))
     res.__is_json__ = True
     return res