1 | /**
|
---|
2 | * @class JSONSerialiser
|
---|
3 | *
|
---|
4 | * @param {Namespace} namespace
|
---|
5 | *
|
---|
6 | * @property {Namespace} namespace
|
---|
7 | */
|
---|
8 | class JSONSerialiser {
|
---|
9 | constructor(namespace) {
|
---|
10 | this.namespace = namespace || new this.Namespace();
|
---|
11 | }
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * @param {Element} element
|
---|
15 | * @returns {object}
|
---|
16 | */
|
---|
17 | serialise(element) {
|
---|
18 | if (!(element instanceof this.namespace.elements.Element)) {
|
---|
19 | throw new TypeError(`Given element \`${element}\` is not an Element instance`);
|
---|
20 | }
|
---|
21 |
|
---|
22 | const payload = {
|
---|
23 | element: element.element,
|
---|
24 | };
|
---|
25 |
|
---|
26 | if (element._meta && element._meta.length > 0) {
|
---|
27 | payload.meta = this.serialiseObject(element.meta);
|
---|
28 | }
|
---|
29 |
|
---|
30 | if (element._attributes && element._attributes.length > 0) {
|
---|
31 | payload.attributes = this.serialiseObject(element.attributes);
|
---|
32 | }
|
---|
33 |
|
---|
34 | const content = this.serialiseContent(element.content);
|
---|
35 |
|
---|
36 | if (content !== undefined) {
|
---|
37 | payload.content = content;
|
---|
38 | }
|
---|
39 |
|
---|
40 | return payload;
|
---|
41 | }
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * @param {object} value
|
---|
45 | * @returns {Element}
|
---|
46 | */
|
---|
47 | deserialise(value) {
|
---|
48 | if (!value.element) {
|
---|
49 | throw new Error('Given value is not an object containing an element name');
|
---|
50 | }
|
---|
51 |
|
---|
52 | const ElementClass = this.namespace.getElementClass(value.element);
|
---|
53 | const element = new ElementClass();
|
---|
54 |
|
---|
55 | if (element.element !== value.element) {
|
---|
56 | element.element = value.element;
|
---|
57 | }
|
---|
58 |
|
---|
59 | if (value.meta) {
|
---|
60 | this.deserialiseObject(value.meta, element.meta);
|
---|
61 | }
|
---|
62 |
|
---|
63 | if (value.attributes) {
|
---|
64 | this.deserialiseObject(value.attributes, element.attributes);
|
---|
65 | }
|
---|
66 |
|
---|
67 | const content = this.deserialiseContent(value.content);
|
---|
68 | if (content !== undefined || element.content === null) {
|
---|
69 | element.content = content;
|
---|
70 | }
|
---|
71 |
|
---|
72 | return element;
|
---|
73 | }
|
---|
74 |
|
---|
75 | // Private API
|
---|
76 |
|
---|
77 | serialiseContent(content) {
|
---|
78 | if (content instanceof this.namespace.elements.Element) {
|
---|
79 | return this.serialise(content);
|
---|
80 | }
|
---|
81 |
|
---|
82 | if (content instanceof this.namespace.KeyValuePair) {
|
---|
83 | const pair = {
|
---|
84 | key: this.serialise(content.key),
|
---|
85 | };
|
---|
86 |
|
---|
87 | if (content.value) {
|
---|
88 | pair.value = this.serialise(content.value);
|
---|
89 | }
|
---|
90 |
|
---|
91 | return pair;
|
---|
92 | }
|
---|
93 |
|
---|
94 | if (content && content.map) {
|
---|
95 | if (content.length === 0) {
|
---|
96 | return undefined;
|
---|
97 | }
|
---|
98 |
|
---|
99 | return content.map(this.serialise, this);
|
---|
100 | }
|
---|
101 |
|
---|
102 | return content;
|
---|
103 | }
|
---|
104 |
|
---|
105 | deserialiseContent(content) {
|
---|
106 | if (content) {
|
---|
107 | if (content.element) {
|
---|
108 | return this.deserialise(content);
|
---|
109 | }
|
---|
110 |
|
---|
111 | if (content.key) {
|
---|
112 | const pair = new this.namespace.KeyValuePair(this.deserialise(content.key));
|
---|
113 |
|
---|
114 | if (content.value) {
|
---|
115 | pair.value = this.deserialise(content.value);
|
---|
116 | }
|
---|
117 |
|
---|
118 | return pair;
|
---|
119 | }
|
---|
120 |
|
---|
121 | if (content.map) {
|
---|
122 | return content.map(this.deserialise, this);
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | return content;
|
---|
127 | }
|
---|
128 |
|
---|
129 | serialiseObject(obj) {
|
---|
130 | const result = {};
|
---|
131 |
|
---|
132 | obj.forEach((value, key) => {
|
---|
133 | if (value) {
|
---|
134 | result[key.toValue()] = this.serialise(value);
|
---|
135 | }
|
---|
136 | });
|
---|
137 |
|
---|
138 | if (Object.keys(result).length === 0) {
|
---|
139 | return undefined;
|
---|
140 | }
|
---|
141 |
|
---|
142 | return result;
|
---|
143 | }
|
---|
144 |
|
---|
145 | deserialiseObject(from, to) {
|
---|
146 | Object.keys(from).forEach((key) => {
|
---|
147 | to.set(key, this.deserialise(from[key]));
|
---|
148 | });
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 |
|
---|
153 | module.exports = JSONSerialiser;
|
---|