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 InvalidNameException if metadata.name doesn't have a length of 1 - 32 (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 (type != originalType) {
      toReturn[ApiFields.type] = type.value;
    }

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

    if (configurationType != _originalConfigurationType) {
      toReturn[ApiFields.configurationType] = configurationType;
    }

    if (action != _originalAction) {
      toReturn[ApiFields.action] = action;
    }

    if (streamProxy != _originalStreamProxy) {
      toReturn[ApiFields.streamProxy] = streamProxy;
    }

    if (!const DeepCollectionEquality.unordered()
        .equals(locations, _originalLocations)) {
      toReturn[ApiFields.locations] = {
        ApiFields.serviceLocations: locations
            .map((location) =>
                location.toJson(optimizeFor: OptimizeFor.putFull))
            .toList()
      };
    }

    return toReturn;
  }

  // PUT FULL
  if (identical(optimizeFor, OptimizeFor.putFull)) {
    return {
      ApiFields.type: type,
      ApiFields.metadata: metadata.toJson(optimizeFor: optimizeFor),
      ApiFields.configurationType: configurationType,
      ApiFields.action: action,
      ApiFields.streamProxy: streamProxy.toJson(optimizeFor: optimizeFor),
      ApiFields.locations: {
        ApiFields.serviceLocations: locations
            .map((location) =>
                location.toJson(optimizeFor: OptimizeFor.putFull))
            .toList(),
      },
    };
  }

  // POST
  if (identical(optimizeFor, OptimizeFor.post)) {
    return {
      ApiFields.type: type,
      ApiFields.metadata: metadata.toJson(optimizeFor: optimizeFor),
      ApiFields.configurationType: configurationType,
      ApiFields.streamProxy: streamProxy.toJson(optimizeFor: optimizeFor),
      ApiFields.locations: {
        ApiFields.serviceLocations: locations
            .map((location) => location.toJson(optimizeFor: optimizeFor))
            .toList(),
      },
    };
  }

  // DEFAULT
  return {
    ApiFields.type: type.value,
    ApiFields.id: id,
    ApiFields.idV1: idV1,
    ApiFields.metadata: metadata.toJson(optimizeFor: optimizeFor),
    ApiFields.name: name,
    ApiFields.configurationType: configurationType,
    ApiFields.status: status,
    ApiFields.activeStreamer: activeStreamer.toJson(optimizeFor: optimizeFor),
    ApiFields.streamProxy: streamProxy.toJson(optimizeFor: optimizeFor),
    ApiFields.channels: channels
        .map((channel) => channel.toJson(optimizeFor: optimizeFor))
        .toList(),
    ApiFields.locations: {
      ApiFields.serviceLocations: locations
          .map((location) => location.toJson(optimizeFor: optimizeFor))
          .toList(),
    },
    ApiFields.lightServices: lightServices
        .map((lightService) => lightService.toJson(optimizeFor: optimizeFor))
        .toList(),
    ApiFields.action: action,
  };
}