Add options argument to #toJson() methods, and use to implement optional fractional digit precision control.

This commit is contained in:
Jürg Lehni 2013-02-12 14:57:54 -08:00
parent 053aa15ded
commit fdd15e675f
11 changed files with 40 additions and 28 deletions

View file

@ -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);
},
/**

View file

@ -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)];
},
/**

View file

@ -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;
},

View file

@ -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);
});
},

View file

@ -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);
},
/**

View file

@ -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);
},
/**

View file

@ -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) {

View file

@ -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);
}
}

View file

@ -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) {

View file

@ -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() {

View file

@ -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);
});
},