aboutsummaryrefslogtreecommitdiff
path: root/node_modules/montage/core/serializer.js
blob: 725433280c1bb466203ca1086d68b85a33f7f382 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
/* <copyright>
 This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
 No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
 (c) Copyright 2011 Motorola Mobility, Inc.  All Rights Reserved.
 </copyright> */
/**
 @module montage/core/serializer
 @requires montage
 @requires montage/core/uuid
 @requires montage/core/deserializer
 */
var Montage = require("montage").Montage;
var Uuid = require("core/uuid").Uuid;
var Deserializer = require("core/deserializer").Deserializer;
var logger = require("core/logger").logger("serializer");
var Element;

// Shadowing the global with a local allows us to feature-test without typeof
// Element does not exist on the server-side
if (typeof window !== "undefined") {
    Element = window.Element;
}

/**
 @class module:montage/core/serializer.Serializer
 @classdesc Serialized objects are indexed by uuid.
 @extends module:montage/core/core.Montage
 */
var Serializer = Montage.create(Montage, /** @lends module:montage/serializer.Serializer# */ {
    _MONTAGE_ID_ATTRIBUTE: {value: "data-montage-id"},
    _serializedObjects: {value: {}}, // label -> string
    _serializedReferences: {value: {}}, // uuid -> string
    _externalObjects: {value: null}, // label -> object
    _externalElements: {value: null},
    _objectStack: {value: null},
    _objectReferences: {value: null}, // uuid -> object with properies
    // last used index of an objectName to create a label
    _objectNamesIndex: {value: null},
    _objectLabels: {value: null}, // uuid -> label
    _serializationUnits: {value: []},
    _serializationUnitsIndex: {value: {}},

    serializeNullValues: {value: false},

    /**
     Defines a serialization unit for an object.
     @function
     @param {string} name The unit name.
     @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.
     */
    defineSerializationUnit: {value: function(name, funktion) {
        this._serializationUnits.push(this._serializationUnitsIndex[name] = {
            name: name,
            funktion: funktion
        });
    }},

    /**
     Defines the require function to be used.
     @function
     @param {function} require The require function to be used to identify module ids of the objects being serialized.
     */
    initWithRequire: {
        value: function(require) {
            this._require = require;
            return this;
        }
    },

    /**
     Serializes a single object.
     @function
     @param {object} object The object to be serialized.
     @returns {string} The serialized object.
     */
    serializeObject: {
        value: function(object) {
            return this.serialize({root: object});
        }
    },

    /**
     Serialize several objects under specific labels.
     @function
     @param {object} objects A label->object mapping of the objects to be serialized.
     @returns {string} The serialized objects.
     */
    serialize: {
        value: function(objects) {
            var serialization,
                valueSerialization,
                label,
                serializeNullValues = this.serializeNullValues;

            this._serializedObjects = {};
            this._serializedReferences = {};
            this._externalObjects = {};
            this._externalElements = [];
            this._objectNamesIndex = {};
            this._objectLabels = {};
            this._objectReferences = {};

            for (label in objects) {
                if (objects[label] != null) {
                    this._objectLabels[objects[label].uuid] = label;
                }
            }

            for (label in objects) {
                if (objects[label] != null || serializeNullValues) {
                    valueSerialization = this._serializeValue(objects[label], null, 2);
                    // objects are automatically inserted as top level objects after calling _serializeValue, but native objects have to be manually inserted them.
                    if (!(label in this._serializedObjects)) {
                        this._serializedObjects[label] = {value: valueSerialization};
                    }
                }
            }

            serialization = this._getSerialization(this._serializedObjects);
            //console.log(serialization);
            // save the require used for this serialization
            this._require = require;
            this._serialization = serialization;
            return serialization;
        }
    },

    /**
     This function is to be used in the context of serializeProperties delegate used for custom object serializations.
     It adds an entry to the "properties" serialization unit of the object being serialized.
     @function
     @param {string} name The name of the entry to be added.
     @param {string} value The value to be serialized.
     */
    set: {value: function(name, value, type) {
        var stack = this._objectStack,
            stackElement = stack[stack.length - 1],
            objectReferences, uuid;

        stackElement[name] = value;
        if (type === "reference") {
            uuid = stackElement.uuid;
            objectReferences = this._objectReferences;
            if (!(uuid in objectReferences)) {
                objectReferences[uuid] = {};
            }
            objectReferences[uuid][name] = true;
        }
    }},

    /**
     This function is to be used in the context of serializeProperties delegate used for custom object serializations.
     It serializes all properties specified as part of the "properties" serialization unit.
     @function
     @param {array} propertyNames The array with the property names to be serialized.
     */
    setAll: {value: function(propertyNames) {
        var ix = this._objectStack.length - 2,
            object = this._objectStack[ix];

        if (!propertyNames) {
            propertyNames = Montage.getSerializablePropertyNames(object);
        }

        for (var i = 0, l = propertyNames.length; i < l; i++) {
            var propertyName = propertyNames[i];
            this.set(propertyName, object[propertyName], Montage.getPropertyAttribute(object, propertyName, "serializable"));
        }
    }},

    setProperty: {
        value: function(name, value, type) {
            var stack = this._objectStack,
                stackElement = stack[stack.length - 1],
                objectReferences, uuid;

            stackElement.properties[name] = value;
            if (type === "reference") {
                objectReferences = this._objectReferences,
                uuid = stackElement.properties.uuid;
                if (!(uuid in objectReferences)) {
                    objectReferences[uuid] = {};
                }
                objectReferences[uuid][name] = true;
            }
        }
    },

    setProperties: {value: function(propertyNames) {
        var ix = this._objectStack.length - 2,
            object = this._objectStack[ix];

        if (!propertyNames) {
            propertyNames = Montage.getSerializablePropertyNames(object);
        }

        for (var i = 0, l = propertyNames.length; i < l; i++) {
            var propertyName = propertyNames[i];
            this.setProperty(propertyName, object[propertyName], Montage.getPropertyAttribute(object, propertyName, "serializable"));
        }
    }},

    setType: {
        value: function(type, value) {
            if (type === "object" || type === "prototype" || type === "value") {
                var stack = this._objectStack,
                stackElement = stack[stack.length - 1];

                delete stackElement.prototype;
                delete stackElement.object;
                delete stackElement.value;
                stackElement[type] = value;
            }
        }
    },

    setUnit: {
        value: function(name) {
            var stack = this._objectStack,
                stackElement = stack[stack.length - 1];

            if (stackElement._units.indexOf(name) === -1) {
                stackElement._units.push(this._serializationUnitsIndex[name]);
            }
        }
    },

    setAllUnits: {
        value: function() {
            var stack = this._objectStack,
                stackElement = stack[stack.length - 1];

            stackElement._units.length = 0;
            stackElement._units.push.apply(stackElement._units, this._serializationUnits);
        }
    },

    /**
     This function is to be used in the context of serializeProperties delegate used for custom object serializations.
     It adds an object to be serialized into the current serialization.
     @function
     @param {object} object The object to be serialized.
     */
    addObject: {value: function(object) {
        var valueSerialization = this._serializeValue(object, null, 2);
        var label = this._getObjectLabel(object);
        // objects are automatically inserted as top level objects after calling _serializeValue, but native objects have to be manually inserted them.
        if (!(label in this._serializedObjects)) {
            this._serializedObjects[label] = {value: valueSerialization};
        }
    }},

    addObjectReference: {
        value: function(object) {
            var label = this._getObjectLabel(object);

            if (!this._serializedObjects[label]) {
                this._externalObjects[label] = object;
            }
            return {"@": label};
        }
    },

    getObjectLabel: {
        value: function(object) {
            return this._getObjectLabel(object);
        }
    },

    /**
     @private
     */
    _pushContextObject: {value: function(object) {
        if (this._objectStack === null) {
            this._objectStack = [object];
        } else {
            this._objectStack.push(object);
        }
    }},

    /**
     @private
     */
    _popContextObject: {value: function() {
        return this._objectStack.pop();
    }},

    /**
     Returns a dictionary of the external objects that were referenced in the last serialization.
     @function
     @returns {object} The dictionary of external objects {label: object}
     */
    getExternalObjects: {value: function() {
        var externalObjects = this._externalObjects;

        for (var label in externalObjects) {
            var object = externalObjects[label];
            if (this._serializedObjects[label]) {
                delete externalObjects[label];
            }
        }

        return externalObjects;
    }},

    /**
     Returns a list of the external elements that were referenced in the last serialization.
     @function
     @returns {array} The array of external elements.
     */
    getExternalElements: {value: function() {
        return this._externalElements;
    }},

    /**
     @private
     */
    _getSerialization: {value: function(objects) {
        var objectsString = [],
            propsString,
            serialization = "",
            object;

        for (var key in objects) {
            object = objects[key];
            propsString = [];
            if ("prototype" in object) {
                propsString.push('"prototype":' + object.prototype);
                delete object.prototype;
            } else if ("object" in object) {
                propsString.push('"object":' + object.object);
                delete object.object;
            }
            for (var prop in object) {
                propsString.push('"' + prop + '":' + object[prop]);
            }
            objectsString.push('"' + key + '":{\n    ' + propsString.join(",\n    ") + '}');
        }

        if (objectsString.length > 0) {
            serialization = "{\n  " + objectsString.join(",\n\n  ") + "\n}";
        }

        return serialization;
    }},

    _getObjectLabel: {value: function(object) {
        var uuid = object.uuid,
            label = this._objectLabels[uuid];

        if (!label) {
            this._objectLabels[uuid] = label = this._generateLabelForObject(object);
        }

        return label;
    }},

    _labelRegexp: {
        enumerable: false,
        value: /^[a-zA-Z_$][0-9a-zA-Z_$]*$/
    },

    _generateLabelForObject: {value: function(object) {
        var objectName = (this._labelRegexp.test(object.identifier) ? object.identifier : null) ||  Montage.getInfoForObject(object).objectName.toLowerCase(),
            index = this._objectNamesIndex[objectName];

        if (index) {
            this._objectNamesIndex[objectName] = index + 1;
            return objectName + index;
        } else {
            this._objectNamesIndex[objectName] = 2;
            return objectName;
        }
    }},

    _applySerializationUnits: {value: function(serializedUnits, object, units) {
        var value;

        if (!units) {
            units = this._serializationUnits;
        }

        for (var i = 0, unit; (unit = units[i]); i++) {
            value = unit.funktion(object, this);
            if (typeof value !== "undefined") {
                serializedUnits[unit.name] = this._serializeValue(value, null, 2);
            }
        }
    }},

    _isValueType: {
        value: function(object) {
            var typeOfObject = typeof object;

            return object instanceof RegExp || object instanceof Element || Array.isArray(object) || Object.getPrototypeOf(object) === Object.prototype || object.constructor === Function || !(typeOfObject === "object" || typeOfObject === "function");
        }
    },

    _applyTypeUnit: {
        value: function(serializedUnits, object) {
            if (this._isValueType(object)) {
                serializedUnits.value = this._serializeValue(object);
            } else {
                var objectInfo = Montage.getInfoForObject(object),
                    moduleId = this._require.identify(
                        objectInfo.moduleId,
                        objectInfo.require
                    ),
                    name = objectInfo.objectName;

                this._findObjectNameRegExp.test(moduleId);
                var defaultName = RegExp.$1.replace(this._toCamelCaseRegExp, this._replaceToCamelCase);

                if (defaultName === name) {
                    name = moduleId;
                } else {
                    name = moduleId + "[" + name + "]";
                }

                if (objectInfo.isInstance) {
                    serializedUnits.prototype = this._serializeValue(name);
                } else {
                    serializedUnits.object = this._serializeValue(name);
                }
            }
        }
    },

    /**
    @private
     */
    _findObjectNameRegExp: {
        value: /([^\/]+?)(\.reel)?$/
    },
    _toCamelCaseRegExp: {
        value: /(?:^|-)([^-])/g
    },
    _replaceToCamelCase: {
        value: function(_, g1) { return g1.toUpperCase() }
    },
    _serializeObject: {value: function(object, properties, type) {
        var uuid = object.uuid,
            serializedReference = this._serializedReferences[uuid],
            serializedUnits,
            propertyNames,
            objectInfo,
            label, moduleId, name, defaultName;

        if (serializedReference) {
            return serializedReference;
        }

        label = this._getObjectLabel(object);
        serializedReference = '{"@":"' + label + '"}';

        if (type === "reference") {
            if (!this._serializedObjects[label]) {
                this._externalObjects[label] = object;
            }
        } else {
            this._serializedReferences[uuid] = serializedReference;

            serializedUnits = {};
            objectInfo = Montage.getInfoForObject(object);

            if (!this._require) {
                throw new Error("Cannot serialize Montage objects without a require function to identify the corresponding package.");
            }

            moduleId = this._require.identify(
                objectInfo.moduleId,
                objectInfo.require
            );
            name = objectInfo.objectName;

            this._findObjectNameRegExp.test(moduleId);
            defaultName = RegExp.$1.replace(this._toCamelCaseRegExp, this._replaceToCamelCase);

            if (defaultName === name) {
                name = moduleId;
            } else {
                name = moduleId + "[" + name + "]";
            }

            if (objectInfo.isInstance) {
                serializedUnits.prototype = this._serializeValue(name);
            } else {
                serializedUnits.object = this._serializeValue(name);
            }

            if (typeof object.serializeProperties === "function") {
                this._pushContextObject(object);
                this._pushContextObject({});
                object.serializeProperties(this, Montage.getSerializablePropertyNames(object));
                serializedUnits.properties = this._serializeObjectLiteral(this._popContextObject(), null, 3);
                this._popContextObject();
            } else {
                // if no set of properties were passed then serialize all the
                // serializable properties of the object itself.
                if (!properties) {
                    properties = object;
                    propertyNames = Montage.getSerializablePropertyNames(object);
                }
                serializedUnits.properties = this._serializeObjectLiteral(properties, propertyNames, 3);
            }

            this._applySerializationUnits(serializedUnits, object);
            this._serializedObjects[label] = serializedUnits;
        }

        return serializedReference;
    }},

    /**
     @private
     */
    _serializeValue: {value: function(value, type, indent) {
        var indent = arguments[2] || 0,
            typeOfValue = typeof value;

            // typeof regexp will be "function" on WebKit because it's a callable
            // object.
            // http://www.mail-archive.com/es-discuss@mozilla.org/msg02824.html
            // https://bugs.webkit.org/show_bug.cgi?id=22082
            if (value instanceof RegExp) {
                return this._serializeRegExp(value);
            } else if (value != null && typeOfValue === "object" || typeOfValue === "function") {
                if (Element && value instanceof Element) {
                    return this._serializeElement(value);
                } else if (Array.isArray(value)) {
                    return this._serializeArray(value, indent + 1);
                } else if (Object.getPrototypeOf(value) === Object.prototype) {
                    return this._serializeObjectLiteral(value, null, indent + 1);
                } else if (value.constructor === Function) {
                    return this._serializeFunction(value, indent);
                } else {
                    // TODO: should refactor this to handle references here, doesn't make
                    //       sense to wait until it hits _serializeObject for that to happen
                    //       if we already have that information here, also, we need to
                    //       support references to values in the future, not just objects.
                    if (typeof value.serializeSelf === "function" && !(value.uuid in this._serializedReferences)) {
                        return this._customSerialization(value, indent + 1);
                    } else {
                        return this._serializeObject(value, null, type);
                    }
                }
            } else {
                return JSON.stringify(value);
            }
    }},

    _customSerialization: {
        value: function(object, indent) {
            this._pushContextObject(object);
            this._pushContextObject({properties: {}, _units: []});
            var newObject = object.serializeSelf(this);
            var objectDescriptor = this._popContextObject();
            this._popContextObject();

            if (typeof newObject === "undefined") {
                return this._serializeValueWithDescriptor(object, objectDescriptor, indent);
            } else {
                // make sure the new returned object is serialized under the same label
                this._objectLabels[newObject.uuid] = this._objectLabels[object.uuid]
                return this._serializeValue(newObject, indent);
            }
        }
    },

    /**
     @private
     */
    _serializeElement: {value: function(element) {
        var attribute = element.getAttribute(this._MONTAGE_ID_ATTRIBUTE),
            id = attribute;

        if (id) {
            this._externalElements.push(element);
            return '{"#":"' + id + '"}';
        } else {
            logger.error("Error: Not possible to serialize a DOM element with no " + this._MONTAGE_ID_ATTRIBUTE + " assigned: " + element.outerHTML);
        }
    }},

    /**
     @private
     */
    _serializeRegExp: {value: function(regexp) {
        return this._serializeValue({"/": {source: regexp.source, flags: (regexp.global ? "g" : "") + (regexp.ignoreCase ? "i" : "") + (regexp.multiline ? "m" : "")}});
    }},

    /**
     @private
     */
    _serializeObjectLiteral: {value: function(object, propertyNames) {
        propertyNames = propertyNames || Object.keys(object);

        var indent = arguments[2] || 0,
            propCount = propertyNames.length,
            serializedProperties = [],
            serializedReferenceProperties = this._objectReferences[object.uuid],
            i,
            key,
            value,
            serializationType;

        for (i = 0; i < propCount; i++) {
            key = propertyNames[i];
            value = object[key];

            if (typeof value !== "undefined" && (this.serializeNullValues || value !== null)) {
                serializationType = Montage.getPropertyAttribute(object, key, "serializable") || ((serializedReferenceProperties && serializedReferenceProperties.hasOwnProperty(key)) ? "reference" : "auto");
                serializedProperties.push(JSON.stringify(key) + ":" + this._serializeValue(value, serializationType, indent));
            }
        }

        var space = new Array(indent + 1).join("  ");
        return "{\n" + space + serializedProperties.join(",\n" + space) + "}";
    }},

    /**
     @private
     */
    _serializeArray: {value: function(array) {
        var indent = arguments[1] || 0;

        var serializedElements = [];
        for (var i = 0, j = array.length; i < j; i++) {
            serializedElements.push(this._serializeValue(array[i], null, indent));
        }
        var space = new Array(indent + 1).join("  ");
        return "[\n" + space + serializedElements.join(",\n" + space) + "]";
    }},

    _serializeFunctionRegexp: {
        enumerable: false,
        value: /^function[^(]*\(([^\)]+)\)\s*\{([\s\S]*)\}$/m
    },

    _serializeFunction: {value: function(funktion) {
        var indent = arguments[1] || 0,
            space = new Array(indent + 1).join("  "),
            string = funktion.toString(),
            parseString = this._serializeFunctionRegexp.exec(string);

        return this._serializeValue({"->": {arguments: parseString[1].split(/\s*,\s*/), body: parseString[2]}}, null, indent);
    }},

    _serializeValueWithDescriptor: {
        value: function(object, objectDescriptor, indent) {
            var label;

            if (!("prototype" in objectDescriptor || "object" in objectDescriptor || "value" in objectDescriptor)) {
                this._applyTypeUnit(objectDescriptor, object);
            }

            if ("value" in objectDescriptor) {
                return this._serializeValue(objectDescriptor.value);
            } else {
                objectDescriptor.properties = this._serializeObjectLiteral(objectDescriptor.properties, null, 3);
                var units;
                if (units = /* assignment */ objectDescriptor._units) {
                    delete objectDescriptor._units;
                    this._applySerializationUnits(objectDescriptor, object, units);
                }
                label = this._getObjectLabel(object);
                this._serializedObjects[label] = objectDescriptor;
                return this._serializedReferences[object.uuid] = '{"@":"' + label + '"}';
            }
        }
    },
});

/**
Creates a serialization for an object from the perspective of the
given package.
@function
@param {Object} object
@param require
@returns serialization
*/
function serialize(object, require) {
    return Serializer.create().initWithRequire(require).serializeObject(object);
}

if (typeof exports !== "undefined") {
    exports.Serializer = Serializer;
    exports.serialize = serialize;
}