@docx4j/jsonix 3.2.0

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 ADDED
@@ -0,0 +1,162 @@
1
+ # Jsonix
2
+
3
+ * Jsonix (JSON interfaces for XML) is a JavaScript library which allows you to convert between XML and JSON structures.
4
+ * With Jsonix you can parse XML into JSON (this process is called _unmarshalling_) or serialize JSON in XML form (this is called _marshalling_).
5
+ * These conversions are based on declarative XML/JSON mappings which can be written manually or generated from an XML Schema.
6
+
7
+ Jsonix advantages:
8
+
9
+ * Strongly structured
10
+ * Type-safe
11
+ * Bidirectional
12
+ * (Optionally) XML Schema-driven
13
+
14
+ See also the other [Jsonix features](#jsonix-features).
15
+
16
+ ## Example
17
+
18
+ Here's a working example for the [purchase order schema](http://www.w3.org/TR/xmlschema-0/#po.xsd) (try it [online in JSFiddle](http://jsfiddle.net/lexi/LP3DC/)).
19
+
20
+ ### Generate mappings
21
+
22
+ Mappings are generated by the separate [jsonix-schema-compiler](https://github.com/highsource/jsonix-schema-compiler)
23
+ (JDK 11+; its jar is not part of this package):
24
+
25
+ ```
26
+ java -jar jsonix-schema-compiler-full-<VERSION>.jar -d mappings -p PO purchaseorder.xsd [-b bindings.xjb]
27
+ ```
28
+
29
+ Generates mappings for the `purchaseorder.xsd` schema in `mappings/PO.js`; mappings will be placed in the variable `PO`.
30
+ Binding files (`.xjb`) must use the Jakarta namespace (`xmlns:jaxb="https://jakarta.ee/xml/ns/jaxb"`, `version="3.0"`)
31
+ with current compiler releases; customisations in the old `http://java.sun.com/xml/ns/jaxb` namespace are silently ignored.
32
+
33
+ ### Parse XML into JS
34
+
35
+ ```javascript
36
+ // Include or require PO.js so that PO variable is available
37
+ // For instance, in node.js:
38
+ var PO = require('./mappings/PO').PO;
39
+
40
+ // First we construct a Jsonix context - a factory for unmarshaller (parser)
41
+ // and marshaller (serializer)
42
+ var context = new Jsonix.Context([PO]);
43
+
44
+ // Then we create a unmarshaller
45
+ var unmarshaller = context.createUnmarshaller();
46
+
47
+ // Unmarshal an object from the XML retrieved from the URL
48
+ unmarshaller.unmarshalURL('po.xml',
49
+ // This callback function will be provided
50
+ // with the result of the unmarshalling
51
+ function (unmarshalled) {
52
+ // Alice Smith
53
+ console.log(unmarshalled.value.shipTo.name);
54
+ // Baby Monitor
55
+ console.log(unmarshalled.value.items.item[1].productName);
56
+ });
57
+ ```
58
+
59
+ You can also `unmarshalString`, `unmarshalDocument` and (under node.js) `unmarshalFile`.
60
+
61
+ ### Serialize JS as XML
62
+
63
+ ```javascript
64
+ // Create a marshaller
65
+ var marshaller = context.createMarshaller();
66
+
67
+ // Marshal a JavaScript Object as XML (DOM Document)
68
+ var doc = marshaller.marshalDocument({
69
+ name: {
70
+ localPart: "purchaseOrder"
71
+ },
72
+ value: {
73
+ orderDate: { year: 1999, month: 10, day: 20 },
74
+ shipTo: {
75
+ country: "US",
76
+ name: "Alice Smith",
77
+ street: "123 Maple Street",
78
+ city: "Mill Valley",
79
+ state: "CA",
80
+ zip: 90952
81
+ },
82
+ billTo: { /* ... */ },
83
+ comment: 'Hurry, my lawn is going wild!',
84
+ items: { /* ... */ }
85
+ }
86
+ });
87
+ ```
88
+
89
+ You can also `marshalString`.
90
+
91
+ ## TypeScript
92
+
93
+ Run the compiler with `-generateTypeScript` to get, next to each mapping file, a declaration file describing the
94
+ objects Jsonix unmarshals and marshals (`PO.d.ts`). Together with this package's own typings the results need no casts:
95
+
96
+ ```ts
97
+ import { Jsonix } from '@docx4j/jsonix';
98
+ import { PO } from './mappings/PO';
99
+ import type { PurchaseOrderElement, USAddress } from './mappings/PO';
100
+
101
+ const context = new Jsonix.Context([PO]);
102
+ const element = context.createUnmarshaller().unmarshalString(xml); // RootElement, inferred from PO
103
+ const po = context.createUnmarshaller().unmarshalString<PurchaseOrderElement>(xml).value;
104
+ po.shipTo.name; // string
105
+ po.orderDate?.year; // number | undefined: dates are Jsonix calendars, not JS Dates
106
+
107
+ const shipTo: USAddress = { name: 'Alice Smith', street: '123 Maple Street', city: 'Mill Valley', state: 'CA', zip: 90952 };
108
+ const text = context.createMarshaller().marshalString<PurchaseOrderElement>({
109
+ name: { namespaceURI: '', localPart: 'purchaseOrder' },
110
+ value: { shipTo, billTo: shipTo, items: {} }
111
+ });
112
+ ```
113
+
114
+ * An unmarshaller returns `Jsonix.TypedNamedValue<T>`, i.e. `{ name: QName, value: T }`; generated files alias it per
115
+ global element (`PurchaseOrderElement`) and as the union of all of them (`RootElement`). The generated mapping
116
+ constant is declared as `JsonixMapping<RootElement>`, so a context built from generated mappings infers that union as
117
+ the result of `unmarshal*` when no type argument is given; a type argument narrows it.
118
+ * Unmarshalled objects carry `TYPE_NAME` (e.g. `'PO.USAddress'`), which the generated interfaces declare as a literal
119
+ type usable as a discriminant. It is optional on input; you need not set it when marshalling.
120
+ * The compiler can also write the mapping as an ES module (`<jsonix:output format="esm"/>` gives `PO.mjs`), and this
121
+ package has an ES-module entry point, so `import { Jsonix } from '@docx4j/jsonix'` works in Node ESM and bundlers.
122
+ * `new Jsonix.Context(mappings, { parentPointers: true })` gives every unmarshalled typed object a non-enumerable
123
+ `PARENT` pointing at its containing typed object (root objects have none), and `Jsonix.Util.deepCopy(value, parent?)`
124
+ copies a subtree and re-links the pointers, like docx4j's `-Xparent-pointer` / `-Xdocx4j-copy` model. Generated
125
+ declarations type `PARENT` as the union of the types that can contain each type. `PARENT` is invisible to
126
+ `for...in`, `Object.keys`, JSON and the marshaller.
127
+
128
+ ## Jsonix Features
129
+
130
+ * Runs in almost any modern browser
131
+ * Runs in [Node.js](http://nodejs.org/)
132
+ * Runs with CommonJS modules, ES modules, AMD modules as well as vanilla (globals, without any module loader)
133
+ * Ships TypeScript typings that compose with the declarations generated by jsonix-schema-compiler (see [TypeScript](#typescript))
134
+ * Bidirectional (XML -> JS as well as JS -> XML)
135
+ * Implements *marshalling* (serializing the JavaScript object into XML)
136
+ * Supports string data and DOM nodes as result
137
+ * Implements *unmarshalling* (parsing a JavaScript object from XML)
138
+ * Supports string data, DOM nodes, URLs or files (with Node.js) as source
139
+ * Driven by declarative XML/JS mappings which control how JavaScript object is converted into XML or vice versa
140
+ * Mappings can be automatically generated based on the XML Schema
141
+ * Strongly-structured - XML/object mappings describe structures of JavaScript objects
142
+ * Strongly-typed - Conversion between string content on XML side and values on the JavaScript side is controlled by declared property types
143
+ * Provides extensible type system
144
+ * Supports most XML Schema simple types (inlcuding QNames)
145
+ * Supports enumerations, list and union simple types
146
+ * Allows adding own simple types
147
+ * Supports complex types consisting of several properties
148
+ * Supports deriving complex types by extension
149
+ * Provides advanced property system
150
+ * Value, attribute, element, element reference properties for string processing of XML content
151
+ * Any attribute, any element properties for "lax" processing for XML content
152
+
153
+ ## Documentation
154
+
155
+ * [Jsonix GitHub Project](https://github.com/highsource/jsonix)
156
+ * [Jsonix Wiki](https://github.com/highsource/jsonix/wiki)
157
+
158
+ ## Lineage
159
+
160
+ Forked from [highsource/jsonix](https://github.com/highsource/jsonix) (Dr. Alexey Valikov) via [MITRE's fork](https://github.com/mitre/jsonix)
161
+ (`@mitre/jsonix`, up to 3.0.11). From 3.2.0 maintained by [Plutext](https://www.plutext.com) at
162
+ [plutext/jsonix](https://github.com/plutext/jsonix) and published as `@docx4j/jsonix`.