toJson method

  1. @override
Map<String, dynamic> toJson({
  1. OptimizeFor optimizeFor = OptimizeFor.put,
})
override

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.

Throws GradientException if gradient.points has is not in the proper range. It must either have 0 elements or between 2 and 5 (inclusive), and optimizeFor is not set to OptimizeFor.dontOptimize.

Throws InvalidValueException if gradient.mode, effects.effect, timedEffects.effect, or alert.action are empty and optimizeFor is not set to OptimizeFor.dontOptimize.

Throws InvalidNameException if metadata.name doesn't have 1 to 32 characters (inclusive), 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 (metadata != _originalMetadata) {
      toReturn[ApiFields.metadata] =
          metadata.toJson(optimizeFor: OptimizeFor.putFull);
    }

    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 (colorTemperature != _originalColorTemperature) {
      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 (color != _originalColor) {
      toReturn[ApiFields.color] =
          color.toJson(optimizeFor: OptimizeFor.putFull);
    }

    if (dynamics != _originalDynamics) {
      toReturn[ApiFields.dynamics] =
          dynamics.toJson(optimizeFor: OptimizeFor.putFull);
    }

    if (alert != _originalAlert) {
      toReturn[ApiFields.alert] =
          alert.toJson(optimizeFor: OptimizeFor.putFull);
    }

    if (gradient != _originalGradient) {
      toReturn[ApiFields.gradient] =
          gradient.toJson(optimizeFor: OptimizeFor.putFull);
    }

    if (effects != _originalEffects) {
      toReturn[ApiFields.effects] =
          effects.toJson(optimizeFor: OptimizeFor.putFull);
    }

    if (timedEffects != _originalTimedEffects) {
      toReturn[ApiFields.timedEffects] =
          timedEffects.toJson(optimizeFor: OptimizeFor.putFull);
    }

    if (powerUp != _originalPowerUp) {
      toReturn[ApiFields.powerUp] =
          powerUp.toJson(optimizeFor: OptimizeFor.putFull);
    }

    return toReturn;
  }

  // PUT FULL
  if (identical(optimizeFor, OptimizeFor.putFull)) {
    return {
      ApiFields.type: type.value,
      ApiFields.metadata: metadata.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: color.toJson(optimizeFor: optimizeFor),
      ApiFields.dynamics: dynamics.toJson(optimizeFor: optimizeFor),
      ApiFields.alert: alert.toJson(optimizeFor: optimizeFor),
      ApiFields.gradient: gradient.toJson(optimizeFor: optimizeFor),
      ApiFields.effects: effects.toJson(optimizeFor: optimizeFor),
      ApiFields.timedEffects: timedEffects.toJson(optimizeFor: optimizeFor),
      ApiFields.powerUp: powerUp.toJson(optimizeFor: optimizeFor),
    };
  }

  // DEFAULT
  return {
    ApiFields.id: id,
    ApiFields.idV1: idV1,
    ApiFields.type: type.value,
    ApiFields.owner: owner.toJson(optimizeFor: optimizeFor),
    ApiFields.metadata: metadata.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: color.toJson(optimizeFor: optimizeFor),
    ApiFields.dynamics: dynamics.toJson(optimizeFor: optimizeFor),
    ApiFields.alert: alert.toJson(optimizeFor: optimizeFor),
    ApiFields.signaling: signaling.toJson(),
    ApiFields.mode: mode.value,
    ApiFields.gradient: gradient.toJson(optimizeFor: optimizeFor),
    ApiFields.effects: effects.toJson(optimizeFor: optimizeFor),
    ApiFields.timedEffects: timedEffects.toJson(optimizeFor: optimizeFor),
    ApiFields.powerUp: powerUp.toJson(optimizeFor: optimizeFor),
  };
}