andersnm / xmldom-decorators Goto Github PK
View Code? Open in Web Editor NEWXML deserializer/serializer for TypeScript classes with decorators
License: MIT License
XML deserializer/serializer for TypeScript classes with decorators
License: MIT License
Any chance on getting a version bump and publish to npm for the fixes you added recently?
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
}
)
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>
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.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.