Giter Site home page Giter Site logo

xmldom-decorators's People

Contributors

andersnm avatar drc-gcoakley avatar juhwon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

xmldom-decorators's Issues

Please support CDATA blocks

While XMLText is great, sometimes you want to force a CDATA block. For example, I work with XML where one of the tags can contain Java code, which contains < characters. You either have to escape all of them (ugly, hard to maintain) or enclose it in a CDATA.

Perhaps even allow CDATA to be variable, such as:

@XMLText(
  {
    // CDATA will be emitted when this expression resolves to true for the given text
    cdata: (text) => text.length > 80
  }
)

serialize bug with children decorators and inheritance

There is an issue with inheritance, when several child instances have implemented similar child elements.

when a base class with member decorators is involved, all children metadata is merge into one single metadata array.
This results in an error when serializing if multiple children have implemented the same element type name.

abstract class DerivedDecoratedBase {
	@XMLAttribute({ type: String})
	parentAttr = "parent"

	@XMLElement({ types: [{ name: "parentEl", itemType: () => String }]})
	parentEl = "parent"
}

@XMLRoot()
class DerivedDecoratedType1 extends DerivedDecoratedBase {
	@XMLAttribute({ type: String})
	type = "type1"

	@XMLElement({ types: [{ name: "typeEl", itemType: () => String }]})
	typeEl = "type1"
}

@XMLRoot()
class DerivedDecoratedType2 extends DerivedDecoratedBase {
	@XMLAttribute({ type: String})
	type = "type2"

	@XMLElement({ types: [{ name: "typeEl", itemType: () => String }]})
	typeEl = "type2"
}


@Test("Multi derived member instances")
public multiDerivedMemberInstances() {
    const o = new DerivedDecoratedType1();
    const a = serialize(o, DerivedDecoratedType1);

    expect(a).toBe('<DerivedDecoratedType1 parentAttr="parent" type="type1"><parentEl>parent</parentEl><typeEl>type1</typeEl></DerivedDecoratedType1>');
}
Expected: <DerivedDecoratedType1 parentAttr="parent" type="type1"><parentEl>parent</parentEl><typeEl>type1</typeEl></DerivedDecoratedType1>
  Actual: <DerivedDecoratedType1 parentAttr="parent" type="type1"><parentEl>parent</parentEl><typeEl>type1</typeEl><typeEl>type1</typeEl></DerivedDecoratedType1>

Question: Best practice/way to serialize an object

I'm tring to serialize an object to look like the following and seeking for the best practice:

<Soap-ENV:Envelope xmlns:Soap-Env="someEnvelopeUri">
    <Soap-ENV:Header xmlns:header="someHeaderUri">
        <header:name>Name</header:name>
    </Soap-ENV:Header>
    <Soap-ENV:Body>
        <ADynamicRequest>
            <service>SomeService</service>
        </ADynamicRequest>
    </Soap-ENV:Body>
</Soap-ENV:Envelope>

Hence my object would look like the following:

import { XMLDecoratorSerializer } from "xmldom-decorators";
import { XMLElement, XMLRoot } from "xmldom-decorators/lib/decorators";

export class Header {
    @XMLElement({ types: [{ name: 'name', namespaceUri: 'someHeaderUri'}]})
    name: string;
}

export class ADynamicRequest {
    @XMLElement({ types: [{ name: 'service'}]})
    service: string;
}

@XMLRoot({ name: 'Envelope', namespaceUri: 'someEnvelopeUri'})
export class SoapEnvelope {
    @XMLElement({ types: [{name: 'Header', namespaceUri: 'someEnvelopeUri'}]})
    header: Header;

    @XMLElement({ types: [{ name: 'Body', namespaceUri: 'someEnvelopeUri'}]})
    //@XMLArray({ name: 'Body', namespaceUri: 'someEnvelopeUri'})
    soapBody: any; // [any], [BodyWrapperClass] type maybe?
    // Works well when the Specific type is given like ADynamicRequest type , but I'm tring to make it more generic so we can serialize any type of Object
    // For example, the type for the body could be a GetDataRequest or GetWeatherRequest

    public static GenerateEnvelope(): string {
        const header: Header = {
            name : 'Name'
        }
        const body : ADynamicRequest = {
            service: 'SomeService'
        }


        const soapEnvelope: SoapEnvelope = {
            header: header,
            soapBody: body
        }

        let serializer = new XMLDecoratorSerializer();
        let serializedObject: string = 
            serializer.serialize(
                soapEnvelope,
                SoapEnvelope,
                {
                    'someEnvelopeUri': 'Soap-ENV',
                    'someHeaderUri': 'header',
                }
            )
        console.log(serializedObject)
        return serializedObject;
    }
}



I do not get the expected result. I'm asking for help/guidance on how should I structure this. Should I serialize bits by bit and append them together instead?
Also I notice that the xmlns prefix is not working like I expect.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.