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 InvalidIdException if owner.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 (on != _originalOn) {
toReturn[ApiFields.isOn] = on.toJson(optimizeFor: OptimizeFor.putFull);
}
if (dimming != _originalDimming) {
toReturn[ApiFields.dimming] =
dimming.toJson(optimizeFor: OptimizeFor.putFull);
}
if (!(_originalDimmingDelta == null && dimmingDelta == null) &&
(((_originalDimmingDelta != null && dimmingDelta != null) &&
(_originalDimmingDelta != dimmingDelta)) ||
(_originalDimmingDelta == null || dimmingDelta == null))) {
toReturn[ApiFields.dimmingDelta] =
dimmingDelta?.toJson(optimizeFor: OptimizeFor.putFull);
}
if (!(_originalColorTemperature == null && colorTemperature == null) &&
(((_originalColorTemperature != null && colorTemperature != null) &&
(_originalColorTemperature != colorTemperature)) ||
(_originalColorTemperature == null ||
colorTemperature == null))) {
toReturn[ApiFields.colorTemperature] =
colorTemperature?.toJson(optimizeFor: OptimizeFor.putFull);
}
if (!(_originalColorTemperatureDelta == null &&
colorTemperatureDelta == null) &&
(((_originalColorTemperatureDelta != null &&
colorTemperatureDelta != null) &&
(_originalColorTemperatureDelta != colorTemperatureDelta)) ||
(_originalColorTemperatureDelta == null ||
colorTemperatureDelta == null))) {
toReturn[ApiFields.colorTemperatureDelta] =
colorTemperatureDelta?.toJson(optimizeFor: OptimizeFor.putFull);
}
if (!(_originalXy == null && xy == null) &&
(((_originalXy != null && xy != null) && (_originalXy != xy)) ||
(_originalXy == null || xy == null))) {
toReturn[ApiFields.xy] = xy?.toJson(optimizeFor: OptimizeFor.putFull);
}
if (alert != _originalAlert) {
toReturn[ApiFields.alert] =
alert.toJson(optimizeFor: OptimizeFor.putFull);
}
if (durationMilliseconds != _originalDurationMilliseconds) {
toReturn[ApiFields.dynamics] = {
ApiFields.duration: durationMilliseconds
};
}
return toReturn;
}
// PUT FULL
if (identical(optimizeFor, OptimizeFor.putFull)) {
return {
ApiFields.type: type.value,
ApiFields.isOn: on.toJson(optimizeFor: optimizeFor),
ApiFields.dimming: dimming.toJson(optimizeFor: optimizeFor),
ApiFields.dimmingDelta: dimmingDelta?.toJson(optimizeFor: optimizeFor),
ApiFields.colorTemperature:
colorTemperature?.toJson(optimizeFor: optimizeFor),
ApiFields.colorTemperatureDelta:
colorTemperatureDelta?.toJson(optimizeFor: optimizeFor),
ApiFields.color: {ApiFields.xy: xy?.toJson(optimizeFor: optimizeFor)},
ApiFields.alert: alert.toJson(optimizeFor: optimizeFor),
ApiFields.dynamics: {ApiFields.duration: durationMilliseconds},
};
}
// DEFAULT
return {
ApiFields.type: type.value,
ApiFields.id: id,
ApiFields.idV1: idV1,
ApiFields.owner: owner.toJson(optimizeFor: optimizeFor),
ApiFields.isOn: on.toJson(optimizeFor: optimizeFor),
ApiFields.dimming: dimming.toJson(optimizeFor: optimizeFor),
ApiFields.dimmingDelta: dimmingDelta?.toJson(optimizeFor: optimizeFor),
ApiFields.colorTemperature:
colorTemperature?.toJson(optimizeFor: optimizeFor),
ApiFields.colorTemperatureDelta:
colorTemperatureDelta?.toJson(optimizeFor: optimizeFor),
ApiFields.color: {ApiFields.xy: xy?.toJson(optimizeFor: optimizeFor)},
ApiFields.alert: alert.toJson(optimizeFor: optimizeFor),
ApiFields.dynamics: {ApiFields.duration: durationMilliseconds},
};
}