RobinBuschmann / xml-typescript

Decorators for xml serialization
MIT License
22 stars 19 forks source link

Creating mixed content #6

Open sshivananda opened 6 years ago

sshivananda commented 6 years ago

I am trying to model classes that can support mixed content. From the example at https://www.npmjs.com/package/js2xmlparser I could do this with

var js2xmlparser = require("js2xmlparser");

var obj = {
    "phone": [
        {
            "@": {
                "type": "home"
            },
            "#": "123-555-4567"
        },
        {
            "@": {
                "type": "cell"
            },
            "#": "890-555-1234"
        }
    ],
    "email": "john@smith.com"
};

console.log(js2xmlparser.parse("person", obj));

which gives me an xml

<?xml version='1.0'?>
<person>
    <phone type='home'>123-555-4567</phone>
    <phone type='cell'>890-555-1234</phone>
    <email>john@smith.com</email>
</person>

How would I go about doing this using xml-decorators?

RobinBuschmann commented 6 years ago

@sshivananda Unfortunately this is not possible 🤔There would be something like a @Value annotation needed to mark a property as the value, of course. Basically what # does for js2xmlparser.

sshivananda commented 6 years ago

I am trying to add support for this and would to happy to raise a PR if I succeed - do you have pointers on where I could start

RobinBuschmann commented 6 years ago

Hey @sshivananda, any help is appreciated 👍 So, here we go: The schema is processed in the XMLElement model. For instance the attributes (which are defined via @) are set here: https://github.com/RobinBuschmann/xml-typescript/blob/master/lib/models/XMLElement.ts#L163 Hope this helps!

rzacherl commented 5 years ago

Robin, I created a modified version which offers a @XMLText decorator (with a single option required). It creates the missing # key inside the schema consumed by js2xmlparser. My local tests were positive. Are you interested in a PR?

RobinBuschmann commented 5 years ago

@rzacherl Yes, for sure 👍 Just create the PR so that I can review it