toJson method
Converts this object into JSON format.
optimizeFor
lets the program know what information to include in the
JSON data map.
- OptimizeFor.put (the default value) is used when making a data map that is being placed in a PUT request. This only includes data that has changed.
- OptimizeFor.putFull is used when a parent object updates; so, all of the children are required to be present for the PUT request.
- OptimizeFor.post is used when making a data map for a POST request.
- OptimizeFor.dontOptimize is used to get all of the data contained in this object.
Throws InvalidNameException if metadata.name
does not have a length of
1 - 32 (inclusive), and optimizeFor
is not set to
OptimizeFor.dontOptimize.
Throws InvalidIdException if group.id
or metadata.image.id
is empty
and optimizeFor
is not set to OptimizeFor.dontOptimize.
Implementation
@override
Map<String, dynamic> toJson({OptimizeFor optimizeFor = OptimizeFor.put}) {
// PUT
if (identical(optimizeFor, OptimizeFor.put)) {
Map<String, dynamic> toReturn = {};
if (!identical(type, originalType)) {
toReturn[ApiFields.type] = type.value;
}
if (!const DeepCollectionEquality.unordered()
.equals(actions, _originalActions)) {
toReturn[ApiFields.actions] = actions
.map((action) => action.toJson(optimizeFor: OptimizeFor.putFull))
.toList();
}
if (recall != _originalRecall) {
toReturn[ApiFields.recall] =
recall.toJson(optimizeFor: OptimizeFor.putFull);
}
if (metadata != _originalMetadata) {
toReturn[ApiFields.metadata] =
metadata.toJson(optimizeFor: OptimizeFor.putFull);
}
if (palette != _originalPalette) {
toReturn[ApiFields.palette] =
palette.toJson(optimizeFor: OptimizeFor.putFull);
}
if (speed != _originalSpeed) {
toReturn[ApiFields.speed] = speed;
}
if (autoDynamic != _originalAutoDynamic) {
toReturn[ApiFields.autoDynamic] = autoDynamic;
}
return toReturn;
}
// PUT FULL
if (identical(optimizeFor, OptimizeFor.putFull)) {
return {
ApiFields.type: type.value,
ApiFields.actions: actions
.map((action) => action.toJson(optimizeFor: optimizeFor))
.toList(),
ApiFields.recall: recall.toJson(optimizeFor: optimizeFor),
ApiFields.metadata: metadata.toJson(optimizeFor: optimizeFor),
ApiFields.palette: palette.toJson(optimizeFor: optimizeFor),
ApiFields.speed: speed,
ApiFields.autoDynamic: autoDynamic,
};
}
// POST
if (identical(optimizeFor, OptimizeFor.post)) {
return {
ApiFields.type: type.value,
ApiFields.actions: actions
.map((action) => action.toJson(optimizeFor: optimizeFor))
.toList(),
ApiFields.metadata: metadata.toJson(optimizeFor: optimizeFor),
ApiFields.group: group.toJson(optimizeFor: optimizeFor),
ApiFields.palette: palette.toJson(optimizeFor: optimizeFor),
ApiFields.speed: speed,
ApiFields.autoDynamic: autoDynamic,
};
}
// DEFAULT
return {
ApiFields.type: type.value,
ApiFields.id: id,
ApiFields.idV1: idV1,
ApiFields.actions: actions
.map((action) => action.toJson(optimizeFor: optimizeFor))
.toList(),
ApiFields.recall: recall.toJson(optimizeFor: optimizeFor),
ApiFields.metadata: metadata.toJson(optimizeFor: optimizeFor),
ApiFields.group: group.toJson(optimizeFor: optimizeFor),
ApiFields.palette: palette.toJson(optimizeFor: optimizeFor),
ApiFields.speed: speed,
ApiFields.autoDynamic: autoDynamic,
};
}