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.

Implementation

@override
Map<String, dynamic> toJson({OptimizeFor optimizeFor = OptimizeFor.put}) {
  // Super JSON
  final Map<String, dynamic> superJson = {
    ApiFields.dimming: super.toJson(optimizeFor: optimizeFor)
  };

  // PUT
  if (identical(optimizeFor, OptimizeFor.put)) {
    Map<String, dynamic> toReturn = {};

    if (Map<String, dynamic>.from(superJson[ApiFields.dimming]).isNotEmpty) {
      toReturn.addAll(superJson);
    }

    if (!identical(mode, _originalMode)) {
      toReturn[ApiFields.mode] = mode.value;
    }

    return toReturn;
  }

  // DEFAULT
  return {
    ...superJson,
    ...{
      ApiFields.mode: mode.value,
    },
  };
}