2021-10-29 15:01:50 -07:00
|
|
|
import JSONSerializer from '@ember-data/serializer/json';
|
|
|
|
|
|
2021-11-04 16:05:11 -07:00
|
|
|
export default class FragmentManagedGroupAttributesSerializer extends JSONSerializer {
|
2021-10-29 15:01:50 -07:00
|
|
|
/**
|
|
|
|
|
* If an attribute is annotated as readOnly in the model, don't serialize it.
|
|
|
|
|
* Otherwise delegate to default attribute serializer.
|
|
|
|
|
* @override
|
|
|
|
|
* @method serializeAttribute
|
|
|
|
|
* @param {Snapshot} snapshot
|
|
|
|
|
* @param {Object} json
|
|
|
|
|
* @param {String} key
|
|
|
|
|
* @param {Object} attribute
|
|
|
|
|
*/
|
|
|
|
|
serializeAttribute(snapshot, json, key, attribute) {
|
2021-11-05 08:55:22 -07:00
|
|
|
const { type, options } = attribute;
|
2021-10-29 15:01:50 -07:00
|
|
|
let value = super.serializeAttribute(...arguments);
|
2021-11-01 12:26:22 -07:00
|
|
|
// Convert empty string to null.
|
2021-10-29 15:01:50 -07:00
|
|
|
if (type === 'string' && json[key] === '') json[key] = null;
|
2021-11-05 08:55:22 -07:00
|
|
|
// Do not serialize read-only attributes.
|
|
|
|
|
if (options.readOnly) delete json[key];
|
|
|
|
|
// Version is sent only if it has a non-nullish value
|
|
|
|
|
if (key === 'version') {
|
|
|
|
|
if (json[key] === null || json[key] === undefined) delete json[key];
|
|
|
|
|
}
|
2021-10-29 15:01:50 -07:00
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
}
|