convertFromJson<E> function

E convertFromJson<E>(
  1. dynamic value, [
  2. dynamic defaultValue,
  3. bool useConverters = true
])

Implementation

E convertFromJson<E>(
  dynamic value, [
  dynamic defaultValue,
  bool useConverters = true,
]) {
  if (E == dynamic) {
    return value;
  }

  if (value == null) {
    if (defaultValue != null) return defaultValue;
    return null as E;
  }

  if (value is Map && (value).isEmpty) {
    if (defaultValue != null) return defaultValue;
    return null as E;
  }

  if (useConverters) {
    if (_converters.containsKey(E)) {
      final converter = _converters[E] as JsonConverter<E>;
      return converter.fromJson(value);
    }
  }

  if (_factories.containsKey(E)) {
    return _factories[E](value);
  }

  if (_factories.containsKey(_typeOf<E>())) {
    return _factories[_typeOf<E>()](value);
  }

  if (E == String || E == _typeOf<String>() || E == _typeOf<String?>()) {
    if (value == null) {
      if (E == _typeOf<String?>()) {
        return null as E;
      }

      return 0 as E;
    }

    return value.toString() as E;
  }

  if (E == int || E == _typeOf<int>() || E == _typeOf<int?>()) {
    if (value == null) {
      if (E == _typeOf<int?>()) {
        return null as E;
      }

      return 0 as E;
    }

    if (value is String) {
      return int.tryParse(value) ?? defaultValue;
    }

    if (value is num) {
      return value.toInt() as E;
    }

    return defaultValue as E;
  }

  if (E == double || E == _typeOf<double>() || E == _typeOf<double?>()) {
    if (value == null) {
      if (E == _typeOf<double?>()) {
        return null as E;
      }

      return 0.0 as E;
    }

    if (value is String) {
      return double.tryParse(value) ?? defaultValue;
    }

    if (value is num) {
      return value.toDouble() as E;
    }

    return defaultValue as E;
  }

  if (E == num || E == _typeOf<num>() || E == _typeOf<num?>()) {
    if (value == null) {
      if (E == _typeOf<num?>()) {
        return null as E;
      }

      return 0.0 as E;
    }

    if (value is String) {
      return num.tryParse(value) ?? defaultValue;
    }

    if (value is num) {
      return value as E;
    }

    return defaultValue as E;
  }

  if (E == DateTime || E == _typeOf<DateTime>() || E == _typeOf<DateTime?>()) {
    if (value == null) {
      if (E == _typeOf<DateTime?>()) {
        return null as E;
      }

      return 0 as E;
    }

    if (value is String) {
      var str = value;
      if (str.length > 10 && !str.contains('Z') && !str.contains('+')) {
        str += 'Z';
      }

      return DateTime.parse(str).toLocal() as E;
    }

    if (value is int) {
      return DateTime.fromMillisecondsSinceEpoch(value) as E;
    }

    if (value is DateTime) {
      return value as E;
    }

    return DateTime.fromMillisecondsSinceEpoch(0) as E;
  }

  if (E == bool || E == _typeOf<bool>() || E == _typeOf<bool?>()) {
    if (value is bool) {
      return value as E;
    }

    if (value is String) {
      return (value == '1' || value == 'true') as E;
    }

    if (value is int) {
      return (value == 1) as E;
    }

    return false as E;
  }

  if (E == List || E == _typeOf<List>() || E == _typeOf<List?>()) {
    return (value as List).convert();
  }

  if (value is List) {
    return (value).convert();
  }

  if (value is Map<String, dynamic>) {
    return (value).convert();
  }

  return value;
}