constructJson method

Map<String, dynamic> constructJson(
  1. {Client? client}
)
inherited

Convert all JsonParameters to a Map. Use specifically when sending a JSON request to the API.

Implementation

Map<String, dynamic> constructJson({Client? client}) {
  // Verify that all required parameters are set in the dictionary
  validate();

  // Collect all properties for this class
  var propertiesAndMirror = _allPropertiesAndMirror(this);
  var properties = propertiesAndMirror.item1;
  var mirror = propertiesAndMirror.item2;

  // Initialize parameter map, using the override parameters if they exist
  _parameterMap = {};

  // Iterate over all properties
  for (var property in properties) {
    var parameterAttribute = JsonParameter.getJsonParameter(property);
    if (parameterAttribute == null) {
      // Ignore any properties that are not annotated with a JsonParameter attribute
      continue;
    }

    // get the value of the property
    try {
      var value = mirror.invokeGetter(property.simpleName);
      if (value == null &&
          parameterAttribute.necessity == Necessity.optional) {
        // Ignore any optional parameters that are null
        continue;
      }

      add(parameterAttribute, value);
    } catch (e) {
      // If the property is not set, ignore it
      continue;
    }
  }

  return _parameterMap;
}