handle<V> method

  1. @override
V handle<V>({
  1. required ParameterType parameterType,
  2. required V value,
  3. required String name,
  4. Kind? kind,
  5. V? defaultConstant,
  6. String? jsonName,
  7. List<Trait>? tags,
})
override

Implementation

@override
V handle<V>({
  required ParameterType parameterType,
  required V value,
  required String name,
  Kind? kind,
  V? defaultConstant,
  String? jsonName,
  List<Trait>? tags,
}) {
  if (tags != null && tags.contains(Trait.noSerialization)) {
    return value;
  }

  // JSON name defaults to Dart identifier
  jsonName ??= name;

  // Does the JSON object contain the JSON field?
  final jsonObject = this.jsonObject;
  if (!jsonObject.containsKey(jsonName)) {
    // No, use default value.
    return value;
  }

  // Get JSON value
  var jsonValue = this.jsonObject[jsonName];

  // Simple case?
  if (jsonValue is V) {
    return jsonValue;
  }

  // Just in case the JSON tree has `int` rather than `double`,
  // convert it to double for consistency:
  if (jsonValue is int) {
    jsonValue = jsonValue.toDouble();
  }

  // Determine kind.
  //
  // If V is nullable:
  //   No need for toNullable() because already handled `null` case when we
  //   did `jsonValue is V`.
  final actualKind = kind ?? Kind.find<V>();
  return actualKind.decodeJsonTree(jsonValue);
}