toJson method

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

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 name does not have a length of 1 - 32 (inclusive), and optimizeFor is not set to OptimizeFor.dontOptimize.

Implementation

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

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

    if (!const DeepCollectionEquality.unordered()
        .equals(positions, _originalPositions)) {
      toReturn[ApiFields.positions] =
          positions.map((position) => position.toJson()).toList();
    }

    if (equalizationFactor != _originalEqualizationFactor) {
      toReturn[ApiFields.equalizationFactor] = equalizationFactor;
    }

    return toReturn;
  }

  // PUT FULL
  if (identical(optimizeFor, OptimizeFor.putFull)) {
    return {
      ApiFields.service: service.toJson(optimizeFor: optimizeFor),
      ApiFields.positions:
          positions.map((position) => position.toJson()).toList(),
      ApiFields.equalizationFactor: equalizationFactor,
    };
  }

  // POST
  if (identical(optimizeFor, OptimizeFor.post)) {
    return {
      ApiFields.service: service.toJson(optimizeFor: optimizeFor),
      ApiFields.positions:
          positions.map((position) => position.toJson()).toList(),
    };
  }

  // DEFAULT
  return {
    ApiFields.service: service.toJson(optimizeFor: optimizeFor),
    ApiFields.position: position.toJson(),
    ApiFields.positions:
        positions.map((position) => position.toJson()).toList(),
    ApiFields.equalizationFactor: equalizationFactor,
  };
}