aboutsummaryrefslogtreecommitdiff
path: root/node_modules/montage/core/serializer.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/montage/core/serializer.js')
-rwxr-xr-xnode_modules/montage/core/serializer.js328
1 files changed, 262 insertions, 66 deletions
diff --git a/node_modules/montage/core/serializer.js b/node_modules/montage/core/serializer.js
index 28f1b1d8..74080770 100755
--- a/node_modules/montage/core/serializer.js
+++ b/node_modules/montage/core/serializer.js
@@ -28,7 +28,7 @@ if (typeof window !== "undefined") {
28 */ 28 */
29var Serializer = Montage.create(Montage, /** @lends module:montage/serializer.Serializer# */ { 29var Serializer = Montage.create(Montage, /** @lends module:montage/serializer.Serializer# */ {
30 _MONTAGE_ID_ATTRIBUTE: {value: "data-montage-id"}, 30 _MONTAGE_ID_ATTRIBUTE: {value: "data-montage-id"},
31 _serializedObjects: {value: {}}, // uuid -> string 31 _serializedObjects: {value: {}}, // label -> string
32 _serializedReferences: {value: {}}, // uuid -> string 32 _serializedReferences: {value: {}}, // uuid -> string
33 _externalObjects: {value: null}, // label -> object 33 _externalObjects: {value: null}, // label -> object
34 _externalElements: {value: null}, 34 _externalElements: {value: null},
@@ -38,6 +38,7 @@ var Serializer = Montage.create(Montage, /** @lends module:montage/serializer.Se
38 _objectNamesIndex: {value: null}, 38 _objectNamesIndex: {value: null},
39 _objectLabels: {value: null}, // uuid -> label 39 _objectLabels: {value: null}, // uuid -> label
40 _serializationUnits: {value: []}, 40 _serializationUnits: {value: []},
41 _serializationUnitsIndex: {value: {}},
41 42
42 serializeNullValues: {value: false}, 43 serializeNullValues: {value: false},
43 44
@@ -48,7 +49,7 @@ var Serializer = Montage.create(Montage, /** @lends module:montage/serializer.Se
48 @param {function} funktion The delegate function that creates the serialization unit. This function accepts the object being serialized as an argument and should return an object to be be JSON'd. 49 @param {function} funktion The delegate function that creates the serialization unit. This function accepts the object being serialized as an argument and should return an object to be be JSON'd.
49 */ 50 */
50 defineSerializationUnit: {value: function(name, funktion) { 51 defineSerializationUnit: {value: function(name, funktion) {
51 this._serializationUnits.push({ 52 this._serializationUnits.push(this._serializationUnitsIndex[name] = {
52 name: name, 53 name: name,
53 funktion: funktion 54 funktion: funktion
54 }); 55 });
@@ -120,61 +121,117 @@ var Serializer = Montage.create(Montage, /** @lends module:montage/serializer.Se
120 }, 121 },
121 122
122 /** 123 /**
123 This function is to be used in the context of serializeSelf delegate used for custom object serializations. 124 This function is to be used in the context of serializeProperties delegate used for custom object serializations.
124 It adds an entry to the "properties" serialization unit of the object being serialized. 125 It adds an entry to the "properties" serialization unit of the object being serialized.
125 @function 126 @function
126 @param {string} name The name of the entry to be added. 127 @param {string} name The name of the entry to be added.
127 @param {string} value The value to be serialized. 128 @param {string} value The value to be serialized.
128 */ 129 */
129 set: {value: function(name, value) { 130 set: {value: function(name, value, type) {
130 var stack = this._objectStack;
131
132 return (stack[stack.length - 1][name] = value);
133 }},
134
135 /**
136 This function is to be used in the context of serializeSelf delegate used for custom object serializations.
137 It adds an entry to the "properties" serialization unit of the object being serialized. The value for this entry will be stored as a reference only and not the value itself.
138 @function
139 @param {string} name The name of the entry to be added.
140 @param {string} value The value to be referenced.
141 */
142 setReference: {value: function(name, value) {
143 var stack = this._objectStack, 131 var stack = this._objectStack,
144 stackElement = stack[stack.length - 1], 132 stackElement = stack[stack.length - 1],
145 objectReferences = this._objectReferences, 133 objectReferences, uuid;
146 uuid = stackElement.uuid;
147 134
148 if (!(uuid in objectReferences)) { 135 stackElement[name] = value;
149 objectReferences[uuid] = {}; 136 if (type === "reference") {
137 uuid = stackElement.uuid;
138 objectReferences = this._objectReferences;
139 if (!(uuid in objectReferences)) {
140 objectReferences[uuid] = {};
141 }
150 objectReferences[uuid][name] = true; 142 objectReferences[uuid][name] = true;
151 } 143 }
152
153 return (stackElement[name] = value);
154 }}, 144 }},
155 145
156 /** 146 /**
157 This function is to be used in the context of serializeSelf delegate used for custom object serializations. 147 This function is to be used in the context of serializeProperties delegate used for custom object serializations.
158 It serializes all properties specified as part of the "properties" serialization unit. 148 It serializes all properties specified as part of the "properties" serialization unit.
159 @function 149 @function
160 @param {array} propertyNames The array with the property names to be serialized. 150 @param {array} propertyNames The array with the property names to be serialized.
161 */ 151 */
162 setProperties: {value: function(propertyNames) { 152 setAll: {value: function(propertyNames) {
163 var ix = this._objectStack.length - 2, 153 var ix = this._objectStack.length - 2,
164 object = this._objectStack[ix]; 154 object = this._objectStack[ix];
165 155
156 if (!propertyNames) {
157 propertyNames = Montage.getSerializablePropertyNames(object);
158 }
159
166 for (var i = 0, l = propertyNames.length; i < l; i++) { 160 for (var i = 0, l = propertyNames.length; i < l; i++) {
167 var propertyName = propertyNames[i]; 161 var propertyName = propertyNames[i];
168 if (Montage.getPropertyAttribute(object, propertyName, "serializable") === "reference") { 162 this.set(propertyName, object[propertyName], Montage.getPropertyAttribute(object, propertyName, "serializable"));
169 this.setReference(propertyName, object[propertyName]); 163 }
170 } else { 164 }},
171 this.set(propertyName, object[propertyName]); 165
166 setProperty: {
167 value: function(name, value, type) {
168 var stack = this._objectStack,
169 stackElement = stack[stack.length - 1],
170 objectReferences, uuid;
171
172 stackElement.properties[name] = value;
173 if (type === "reference") {
174 objectReferences = this._objectReferences,
175 uuid = stackElement.properties.uuid;
176 if (!(uuid in objectReferences)) {
177 objectReferences[uuid] = {};
178 }
179 objectReferences[uuid][name] = true;
172 } 180 }
173 } 181 }
182 },
183
184 setProperties: {value: function(propertyNames) {
185 var ix = this._objectStack.length - 2,
186 object = this._objectStack[ix];
187
188 if (!propertyNames) {
189 propertyNames = Montage.getSerializablePropertyNames(object);
190 }
191
192 for (var i = 0, l = propertyNames.length; i < l; i++) {
193 var propertyName = propertyNames[i];
194 this.setProperty(propertyName, object[propertyName], Montage.getPropertyAttribute(object, propertyName, "serializable"));
195 }
174 }}, 196 }},
175 197
198 setType: {
199 value: function(type, value) {
200 if (type === "object" || type === "prototype" || type === "value") {
201 var stack = this._objectStack,
202 stackElement = stack[stack.length - 1];
203
204 delete stackElement.prototype;
205 delete stackElement.object;
206 delete stackElement.value;
207 stackElement[type] = value;
208 }
209 }
210 },
211
212 setUnit: {
213 value: function(name) {
214 var stack = this._objectStack,
215 stackElement = stack[stack.length - 1];
216
217 if (stackElement._units.indexOf(name) === -1) {
218 stackElement._units.push(this._serializationUnitsIndex[name]);
219 }
220 }
221 },
222
223 setAllUnits: {
224 value: function() {
225 var stack = this._objectStack,
226 stackElement = stack[stack.length - 1];
227
228 stackElement._units.length = 0;
229 stackElement._units.push.apply(stackElement._units, this._serializationUnits);
230 }
231 },
232
176 /** 233 /**
177 This function is to be used in the context of serializeSelf delegate used for custom object serializations. 234 This function is to be used in the context of serializeProperties delegate used for custom object serializations.
178 It adds an object to be serialized into the current serialization. 235 It adds an object to be serialized into the current serialization.
179 @function 236 @function
180 @param {object} object The object to be serialized. 237 @param {object} object The object to be serialized.
@@ -188,6 +245,23 @@ var Serializer = Montage.create(Montage, /** @lends module:montage/serializer.Se
188 } 245 }
189 }}, 246 }},
190 247
248 addObjectReference: {
249 value: function(object) {
250 var label = this._getObjectLabel(object);
251
252 if (!this._serializedObjects[label]) {
253 this._externalObjects[label] = object;
254 }
255 return {"@": label};
256 }
257 },
258
259 getObjectLabel: {
260 value: function(object) {
261 return this._getObjectLabel(object);
262 }
263 },
264
191 /** 265 /**
192 @private 266 @private
193 */ 267 */
@@ -216,7 +290,7 @@ var Serializer = Montage.create(Montage, /** @lends module:montage/serializer.Se
216 290
217 for (var label in externalObjects) { 291 for (var label in externalObjects) {
218 var object = externalObjects[label]; 292 var object = externalObjects[label];
219 if (this._serializedObjects[object.uuid]) {