diff --git a/src/basic/Matrix.js b/src/basic/Matrix.js index 73ed3d13..518b0643 100644 --- a/src/basic/Matrix.js +++ b/src/basic/Matrix.js @@ -72,8 +72,8 @@ var Matrix = this.Matrix = Base.extend(/** @lends Matrix# */{ throw new Error('Unsupported matrix parameters'); }, - _serialize: function() { - return this.getValues(); + _serialize: function(options) { + return Base.serialize(this.getValues(), options); }, /** diff --git a/src/basic/Point.js b/src/basic/Point.js index 729aab56..c045cf34 100644 --- a/src/basic/Point.js +++ b/src/basic/Point.js @@ -164,8 +164,13 @@ var Point = this.Point = Base.extend(/** @lends Point# */{ } }, - _serialize: function() { - return [this.x, this.y]; + _serialize: function(options) { + var format = Base.formatFloat; + // For speed reasons, we directly call formatFloat() here with + // precision, instead of converting array through Base.serialize() which + // makes a copy. + return [format(this.x, options.precision), + format(this.y, options.precision)]; }, /** diff --git a/src/color/Color.js b/src/color/Color.js index 19aae8d8..f3979b7e 100644 --- a/src/color/Color.js +++ b/src/color/Color.js @@ -278,13 +278,13 @@ var Color = this.Color = Base.extend(new function() { return res; }, - _serialize: function() { + _serialize: function(options) { var res = []; for (var i = 0, l = this._components.length; i < l; i++) { var component = this._components[i], value = this['_' + component]; if (component !== 'alpha' || value != null && value < 1) - res.push(value); + res.push(Base.formatFloat(value, options.precision)); } return res; }, diff --git a/src/color/Gradient.js b/src/color/Gradient.js index 85d46390..304cdfc4 100644 --- a/src/color/Gradient.js +++ b/src/color/Gradient.js @@ -33,10 +33,10 @@ var Gradient = this.Gradient = Base.extend(/** @lends Gradient# */{ this.type = type || 'linear'; }, - _serialize: function(dictionary) { + _serialize: function(options, dictionary) { return dictionary.add(this, function() { return Base.serialize([this._type, this._stops, this.type], - true, dictionary); + options, true, dictionary); }); }, diff --git a/src/color/GradientColor.js b/src/color/GradientColor.js index 346f8d57..000a7956 100644 --- a/src/color/GradientColor.js +++ b/src/color/GradientColor.js @@ -98,11 +98,11 @@ var GradientColor = this.GradientColor = Color.extend(/** @lends GradientColor# this._hilite); }, - _serialize: function(dictionary) { + _serialize: function(options, dictionary) { var values = [ this.gradient, this._origin, this._destination ]; if (this._hilite) values.push(this._hilite); - return Base.serialize(values, true, dictionary); + return Base.serialize(values, options, true, dictionary); }, /** diff --git a/src/color/GradientStop.js b/src/color/GradientStop.js index 965d3b5d..abe8dad9 100644 --- a/src/color/GradientStop.js +++ b/src/color/GradientStop.js @@ -53,8 +53,9 @@ var GradientStop = this.GradientStop = Base.extend(/** @lends GradientStop# */{ return new GradientStop(this._color.clone(), this._rampPoint); }, - _serialize: function(dictionary) { - return Base.serialize([this._color, this._rampPoint], false, dictionary); + _serialize: function(options, dictionary) { + return Base.serialize([this._color, this._rampPoint], options, false, + dictionary); }, /** diff --git a/src/core/Base.js b/src/core/Base.js index 9d9481b4..4987b164 100644 --- a/src/core/Base.js +++ b/src/core/Base.js @@ -46,8 +46,8 @@ this.Base = Base.inject(/** @lends Base# */{ }, []).join(', ') + ' }'; }, - toJson: function() { - return Base.toJson(this); + toJson: function(options) { + return Base.toJson(this, options); }, statics: /** @lends Base */{ @@ -247,7 +247,8 @@ this.Base = Base.inject(/** @lends Base# */{ * Serializes the passed object into a format that can be passed to * JSON.stringify() for JSON serialization. */ - serialize: function(obj, compact, dictionary) { + serialize: function(obj, options, compact, dictionary) { + options = options || {}; var root = !dictionary, res; if (root) { @@ -276,7 +277,7 @@ this.Base = Base.inject(/** @lends Base# */{ }; } if (obj && obj._serialize) { - res = obj._serialize(dictionary); + res = obj._serialize(options, dictionary); // If we don't serialize to compact form (meaning no type // identifier), see if _serialize didn't already add the type, // e.g. for types that do not support compact form. @@ -285,12 +286,16 @@ this.Base = Base.inject(/** @lends Base# */{ } else if (Array.isArray(obj)) { res = []; for (var i = 0, l = obj.length; i < l; i++) - res[i] = Base.serialize(obj[i], compact, dictionary); + res[i] = Base.serialize(obj[i], options, compact, + dictionary); } else if (Base.isPlainObject(obj)) { res = {}; for (var i in obj) if (obj.hasOwnProperty(i)) - res[i] = Base.serialize(obj[i], compact, dictionary); + res[i] = Base.serialize(obj[i], options, compact, + dictionary); + } else if (typeof obj === 'number') { + res = Base.formatFloat(obj, options.precision); } else { res = obj; } @@ -349,8 +354,8 @@ this.Base = Base.inject(/** @lends Base# */{ return res; }, - toJson: function(obj) { - return JSON.stringify(Base.serialize(obj)); + toJson: function(obj, options) { + return JSON.stringify(Base.serialize(obj, options)); }, fromJson: function(json) { diff --git a/src/item/Item.js b/src/item/Item.js index fc75811c..bad94a69 100644 --- a/src/item/Item.js +++ b/src/item/Item.js @@ -154,7 +154,7 @@ var Item = this.Item = Base.extend(Callback, { return this; }, - _serialize: function(dictionary) { + _serialize: function(options, dictionary) { var props = {}, that = this; @@ -162,7 +162,8 @@ var Item = this.Item = Base.extend(Callback, { for (var key in fields) { var value = that[key]; if (!Base.equals(value, fields[key])) - props[key] = Base.serialize(value, compact, dictionary); + props[key] = Base.serialize(value, options, compact, + dictionary); } } diff --git a/src/path/Segment.js b/src/path/Segment.js index c9af054f..2209aad3 100644 --- a/src/path/Segment.js +++ b/src/path/Segment.js @@ -85,10 +85,10 @@ var Segment = this.Segment = Base.extend(/** @lends Segment# */{ createPoint(this, '_handleOut', handleOut); }, - _serialize: function() { + _serialize: function(options) { return Base.serialize(this._handleIn.isZero() && this._handleOut.isZero() ? this._point - : [this._point, this._handleIn, this._handleOut], true); + : [this._point, this._handleIn, this._handleOut], options, true); }, _changed: function(point) { diff --git a/src/project/Project.js b/src/project/Project.js index 9adbc180..c3c776af 100644 --- a/src/project/Project.js +++ b/src/project/Project.js @@ -63,12 +63,12 @@ var Project = this.Project = PaperScopeItem.extend(/** @lends Project# */{ // this._changesById = {}; }, - _serialize: function(dictionary) { + _serialize: function(options, dictionary) { // Just serialize layers to an array for now, they will be unserialized // into the active project automatically. We might want to add proper // project serialization later, but deserialization of a layers array // will always work. - return Base.serialize(this.layers, false, dictionary); + return Base.serialize(this.layers, options, false, dictionary); }, _needsRedraw: function() { diff --git a/src/project/Symbol.js b/src/project/Symbol.js index 6b32dc7c..6ee6546c 100644 --- a/src/project/Symbol.js +++ b/src/project/Symbol.js @@ -68,10 +68,10 @@ var Symbol = this.Symbol = Base.extend(/** @lends Symbol# */{ this._instances = {}; }, - _serialize: function(dictionary) { + _serialize: function(options, dictionary) { return dictionary.add(this, function() { return Base.serialize([this._type, this._definition], - false, dictionary); + options, false, dictionary); }); },