Skip to content

Commit c18b775

Browse files
Robin BuschmannRobin Buschmann
Robin Buschmann
authored and
Robin Buschmann
committed
restructured; async serialization; travis
1 parent 6ff72ca commit c18b775

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+218
-643
lines changed

.gitignore

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ certificates/*
1616
# documentation
1717
documentation
1818

19-
# js files
20-
#**/*.js
21-
#**/*.js.map
19+
**/*.js
20+
**/*.js.map
21+
**/*.d.ts
2222

2323
# Elastic Beanstalk Files
2424
.elasticbeanstalk/*

.npmignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
node_modules
2+
npm-debug.log
3+
.travis.yml
4+
.gitignore
5+
tsconfig.json
6+
test
7+
.DS_Store
8+
.idea

.travis.yml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
language: node_js
2+
3+
node_js:
4+
- "6.1"
5+
6+
install:
7+
- npm install
8+
9+
before_script:
10+
11+
script:
12+
- tsc
13+
- mocha test/spec/**/*.js

README.md

+113-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,113 @@
1-
# xml-typescript
1+
[![Build Status](https://travis-ci.org/RobinBuschmann/ts-di.png?branch=master)](https://travis-ci.org/RobinBuschmann/xml-typescript)
2+
3+
# xml-decorators
4+
5+
Decorators for xml serialization. Uses [js2xmlparser](https://www.npmjs.com/package/js2xmlparser)
6+
under the hood.
7+
8+
## Use decorators
9+
10+
```typescript
11+
import {XMLElement, XMLAttribute, XMLChild, xml} from 'xml-decorators';
12+
13+
const HOBBY_NS = 'h';
14+
15+
class Hobby {
16+
17+
@XMLAttribute({namespace: HOBBY_NS})
18+
private name: string;
19+
20+
@XMLAttribute({namespace: HOBBY_NS})
21+
private description: string;
22+
}
23+
24+
const PERSON_ROOT = 'person';
25+
const PERSON_NS = 'ps';
26+
27+
@XMLElement({root: PERSON_ROOT}) // optional
28+
class Person {
29+
30+
@XMLAttribute({namespace: PERSON_NS})
31+
private firstname: string;
32+
33+
private lastname: string;
34+
35+
@XMLAttribute({namespace: PERSON_NS})
36+
get fullname(): string {
37+
38+
return this.firstname + ' ' + this.lastname;
39+
}
40+
41+
@XMLAttribute({namespace: PERSON_NS})
42+
private age: number;
43+
44+
@XMLChild({
45+
namespace: PERSON_NS,
46+
name: 'hobby'
47+
})
48+
private hobbies: Hobby[];
49+
50+
@XMLChild({
51+
namespace: PERSON_NS,
52+
stripPluralS: true
53+
})
54+
private friends: Person[];
55+
56+
@XMLChild({
57+
name: 'pet',
58+
implicitStructure: 'pets.$'
59+
})
60+
private pets: string[];
61+
62+
}
63+
```
64+
65+
## Serialization
66+
```typescript
67+
const hobbies = [
68+
new Hobby('reading', 'loves to read books, magazines and web articles'),
69+
new Hobby('listening to Music', 'loves to listen to rock music'),
70+
new Hobby('travelling', 'loves to travel around the world'),
71+
];
72+
const pets = ['dog', 'cat'];
73+
const bob = new Person('Bob', 'Mad', 29, hobbies, pets);
74+
75+
const bobXml = xml.serialize(bob);
76+
```
77+
78+
Or if you want to override the root tag name or did not used the `@XMLElement` annotation.
79+
```typescript
80+
const bob2Xml = xml.serialize('great-person', bob);
81+
```
82+
83+
### Result
84+
```xml
85+
<?xml version='1.0'?>
86+
<person ps:firstname='Bob' ps:fullname='Bob Mad' ps:age='29'>
87+
<ps:hobby h:name='reading' h:description='loves to read books, magazines and web articles'/>
88+
<ps:hobby h:name='listening to Music' h:description='loves to listen to rock music'/>
89+
<ps:hobby h:name='travelling' h:description='loves to travel around the world'/>
90+
<pets>
91+
<pet>dog</pet>
92+
<pet>cat</pet>
93+
</pets>
94+
</person>
95+
```
96+
97+
### Async
98+
```typescript
99+
xml
100+
.serializeAsync(bob)
101+
.then(bobXml => console.log(bobXml))
102+
;
103+
```
104+
105+
## Schema
106+
If you want to retrieve the "js2xmlparser" schema instead:
107+
```typescript
108+
xml.getSchema(bob);
109+
xml.getSchema('root', bob);
110+
xml.getSchemaAsync(bob) .then(/* */);
111+
xml.getSchemaAsync('root', bob).then(/* */);
112+
```
113+

annotations/XMLAttribute.d.ts

-3
This file was deleted.

annotations/XMLAttribute.js

-17
This file was deleted.

annotations/XMLAttribute.js.map

-1
This file was deleted.

annotations/XMLChild.d.ts

-3
This file was deleted.

annotations/XMLChild.js

-17
This file was deleted.

annotations/XMLChild.js.map

-1
This file was deleted.

annotations/XMLElement.d.ts

-2
This file was deleted.

annotations/XMLElement.js

-10
This file was deleted.

annotations/XMLElement.js.map

-1
This file was deleted.

index.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export {XMLElement as xml} from './lib/models/XMLElement';
2+
3+
export {ATTRIBUTE_PROPERTY, ns} from './lib/utils';
4+
5+
export {XMLAttribute} from './lib/annotations/XMLAttribute';
6+
export {XMLChild} from './lib/annotations/XMLChild';
7+
export {XMLElement} from './lib/annotations/XMLElement';
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

models/XMLElement.ts lib/models/XMLElement.ts

+35-18
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,21 @@ export class XMLElement {
1515
static serialize(root: string, entity: any): string;
1616
static serialize(...args: any[]): string {
1717

18-
let entity;
19-
let root;
20-
21-
if (args.length === 1) {
22-
entity = args[0];
23-
} else {
24-
root = args[0];
25-
entity = args[1];
26-
}
18+
const {root, entity} = this.getRootAndEntity(args);
19+
const schema = this.getSchema(entity);
2720

28-
const element = this.getXMLElement(entity.constructor, false);
21+
return js2xmlparser.parse(root, schema);
22+
}
2923

30-
if (element && element.root) {
31-
root = element.root;
32-
}
33-
if (!root) {
34-
throw new Error('No root defined for entity: ' + JSON.stringify(entity));
35-
}
24+
static serializeAsync(entity: any): Promise<string>;
25+
static serializeAsync(root: string, entity: any): Promise<string>;
26+
static serializeAsync(...args: any[]): Promise<string> {
3627

37-
const schema = this.getSchema(entity);
28+
const {root, entity} = this.getRootAndEntity(args);
3829

39-
return js2xmlparser.parse(root, schema);
30+
return this.getSchemaAsync(entity)
31+
.then(schema => js2xmlparser.parse(root, schema))
32+
;
4033
}
4134

4235
static getSchema(entities: any[]): any;
@@ -102,6 +95,30 @@ export class XMLElement {
10295
return entity;
10396
}
10497

98+
private static getRootAndEntity(args: any[]): {root: string, entity: any} {
99+
100+
let entity;
101+
let root;
102+
103+
if (args.length === 1) {
104+
entity = args[0];
105+
} else {
106+
root = args[0];
107+
entity = args[1];
108+
}
109+
110+
const element = this.getXMLElement(entity.constructor, false);
111+
112+
if (!root && element && element.root) {
113+
root = element.root;
114+
}
115+
if (!root) {
116+
throw new Error('No root defined for entity: ' + JSON.stringify(entity));
117+
}
118+
119+
return {root, entity};
120+
}
121+
105122
addAttribute(attribute: XMLAttribute): void {
106123

107124
if (!this.attributes) this.attributes = [];

utils.d.ts lib/utils.d.ts

File renamed without changes.

utils.js lib/utils.js

File renamed without changes.

utils.js.map lib/utils.js.map

File renamed without changes.

utils.ts lib/utils.ts

File renamed without changes.

models/XMLAttribute.d.ts

-7
This file was deleted.

models/XMLAttribute.js

-36
This file was deleted.

models/XMLAttribute.js.map

-1
This file was deleted.

models/XMLChild.d.ts

-11
This file was deleted.

0 commit comments

Comments
 (0)