toList<T, R> method

List toList<T, R>({
  1. List<T>? valueList,
})

Create a (nested) List structure from width-depth notation.

The key String of this Map is expected to be a dot (.) separated list of numbers which act as width and depth coordinates for adding JsonNode values to the correct (sub) List.

Key is constructed by JsonArrayDecoder.

{@category decoder}

Implementation

List toList<T, R>({List<T>? valueList}) {
  // List contains one or more null values if true.
  final isNullable = values.any((o) => o is JsonNull);

  // List without any null values to simplify Type determination.
  final valuesNotNull = values.where((o) => o is! JsonNull);

  if (valuesNotNull.every((node) => node is JsonString)) {
    return isNullable ? _toNullableStringList : _toStringList;
  }

  if (valuesNotNull.every((node) => node is JsonIntegerNumber)) {
    return isNullable ? _toNullableIntegerList : _toIntegerList;
  }

  if (valuesNotNull.every((node) => node is JsonFloatingNumber)) {
    return isNullable ? _toNullableDoubleList : _toDoubleList;
  }

  /// If a List contains both integers and floating point numbers,
  /// then all integers should be cast to a floating point.
  if (valuesNotNull.every(
      (node) => node is JsonFloatingNumber || node is JsonIntegerNumber)) {
    return map((key, value) => MapEntry(
            key,
            value is JsonIntegerNumber
                ? JsonFloatingNumber(key: key, data: value.data.toDouble())
                : value))
        .toList(valueList: isNullable ? <double?>[] : <double>[]);
  }

  if (valuesNotNull.every((node) => node is JsonBoolean)) {
    return isNullable ? _toNullableBooleanList : _toBooleanList;
  }

  if (valuesNotNull.every((node) => node is JsonObject)) {
    return _toObjectList(valueList: valueList ?? []);
  }

  if (valuesNotNull.every((node) => node is CustomJsonNode)) {
    if (valueList == null) {
      throw SquintException(
          "Unable to build a List because not Type is specified for CustomJsonNode.");
    }
    return _toCustomObjectList<T>(valueList: valueList);
  }

  throw SquintException(
      "Unable to build a List because not all children are of the same Type.");
}