serialize
Defines custom serialization behavior for a property.
Boolean
Specifies if the property should be serialized. By default, all properties except for ones with defined getters are serialized. Prevent a property from being serialized like:
import {DefineMap} from "can";
const myMap = DefineMap.extend({
propertyName: {
serialize: false
},
secondPropertyName: "string"
});
const map = new myMap({ propertyName: "foobar", secondPropertyName: "bar" });
console.log( map.serialize() ); //-> {secondPropertyName: "bar"}
Make a getter property part of the serialized result like:
import {DefineMap} from "can";
const myMap = DefineMap.extend({
propertyName: {
get() { return "test"; },
serialize: true
}
});
const map = new myMap();
console.log( map.serialize() ); //-> { propertyName: "test" }
serialize( currentValue, propertyName )
Specifies the serialized value of a property.
import {DefineMap} from "can";
const myMap = DefineMap.extend({
example: {
serialize( currentValue, propertyName ) {
console.log( currentValue ); //-> "Value"
console.log( propertyName ); //-> "example"
}
}
});
const map = new myMap({ example: "Value" });
map.serialize();
Parameters
- currentValue
{*}
:The current value of the attribute.
- propertyName
{String}
:The name of the property being serialized.
Returns
{*|undefined}
:
If undefined
is returned, the value is not serialized.
Use
serialize is useful for serializing an instance into a more JSON-friendly form. This can be used for many reasons, including saving a can-connected instance on the server or serializing can-route.data's internal map for display in the hash or pushstate URL.
The serialize property allows an opportunity to define how each property will behave when the instance is serialized. This can be useful for:
- serializing complex types like dates, arrays, or objects into string formats
- causing certain properties to be ignored when serialize is called
The following causes a locationIds property to be serialized into the comma separated ID values of the location property on this instance:
import {DefineMap} from "can";
const myMap = DefineMap.extend({
locations: [],
locationIds: {
serialize() {
return this.locations.map( ( location ) => {
return location.id;
} ).join( "," );
}
}
});
const map = new myMap({
locations: [{id: 1}, {id: 2}, {id: 3}]
});
console.log( map.serialize().locationIds ); //-> "1,2,3"
Returning undefined
for any property means this property will not be part of the serialized
object. For example, if the property numPages is not greater than zero, the following example
won't include it in the serialized object.
import {DefineMap} from "can";
const myBook = DefineMap.extend({
prop: {
serialize: function( num ) {
if ( num <= 0 ) {
return undefined;
}
return num;
}
},
bar: "string"
});
const book = new myBook({ prop: -5, bar: "foo" });
console.log( book.serialize() ); //-> { bar: "foo" }