xml-model 1.0.0-beta.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +30 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
## Usage
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Needs [typescript-rtti](https://github.com/typescript-rtti/typescript-rtti) to retrieve type at runtime.
|
|
6
6
|
|
|
7
7
|
To build something that relies in `xml-model` with vite
|
|
8
8
|
|
|
@@ -21,4 +21,32 @@ export default defineConfig({
|
|
|
21
21
|
|
|
22
22
|
## Documentation
|
|
23
23
|
|
|
24
|
-
|
|
24
|
+
### Example
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import "reflect-metadata";
|
|
28
|
+
import { Model, getModel, XML } from "xml-model";
|
|
29
|
+
|
|
30
|
+
@Model({
|
|
31
|
+
fromXML(ctx) {
|
|
32
|
+
const instance = new MyClass();
|
|
33
|
+
if (ctx.properties.foo) instance.foo = ctx.properties.foo;
|
|
34
|
+
return instance;
|
|
35
|
+
},
|
|
36
|
+
})
|
|
37
|
+
class MyClass {
|
|
38
|
+
foo = "bar";
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const model = getModel(MyClass);
|
|
42
|
+
|
|
43
|
+
const a: MyClass = model.fromXML("<my-class><foo>test</foo></my-class>");
|
|
44
|
+
console.log(JSON.stringify(a)); // {"foo":"test"}
|
|
45
|
+
|
|
46
|
+
const b = new MyClass();
|
|
47
|
+
console.log(XML.stringify(model.toXML(b))); // <my-class><foo>bar</foo></my-class>
|
|
48
|
+
b.foo = "other";
|
|
49
|
+
console.log(XML.stringify(model.toXML(b))); // <my-class><foo>other</foo></my-class>
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
See [source code](https://github.com/MathisTLD/xml-model) for more
|