All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dart.libraries.dio.serialization.json_serializable.deserialize.mustache Maven / Gradle / Ivy

There is a newer version: 7.9.0
Show newest version
{{#models}}
  {{#model}}
  {{^isEnum}}
import 'package:{{pubName}}/{{sourceFolder}}/{{modelPackage}}/{{classFilename}}.dart';
{{/isEnum}}
{{/model}}
{{/models}}

final _regList = RegExp(r'^List<(.*)>$');
final _regSet = RegExp(r'^Set<(.*)>$');
final _regMap = RegExp(r'^Map$');

  ReturnType deserialize(dynamic value, String targetType, {bool growable= true}) {
      switch (targetType) {
        case 'String':
          return '$value' as ReturnType;
        case 'int':
          return (value is int ? value : int.parse('$value')) as ReturnType;
        case 'bool':
          if (value is bool) {
            return value as ReturnType;
          }
          final valueString = '$value'.toLowerCase();
          return (valueString == 'true' || valueString == '1') as ReturnType;
        case 'double':
          return (value is double ? value : double.parse('$value')) as ReturnType;
        {{#models}}
          {{#model}}
        case '{{{classname}}}':
            {{#isEnum}}
          {{#native_serialization}}return {{{classname}}}TypeTransformer().decode(value);{{/native_serialization}}
          {{#json_serializable}} return _$enumDecode(_${{{classname}}}EnumMap, value);{{/json_serializable}}
            {{/isEnum}}
            {{^isEnum}}
          return {{{classname}}}.fromJson(value as Map) as ReturnType;
            {{/isEnum}}
          {{/model}}
        {{/models}}
        default:
          RegExpMatch? match;

          if (value is List && (match = _regList.firstMatch(targetType)) != null) {
            targetType = match![1]!; // ignore: parameter_assignments
            return value
              .map((dynamic v) => deserialize(v, targetType, growable: growable))
              .toList(growable: growable) as ReturnType;
          }
          if (value is Set && (match = _regSet.firstMatch(targetType)) != null) {
            targetType = match![1]!; // ignore: parameter_assignments
            return value
              .map((dynamic v) => deserialize(v, targetType, growable: growable))
              .toSet() as ReturnType;
          }
          if (value is Map && (match = _regMap.firstMatch(targetType)) != null) {
            targetType = match![1]!.trim(); // ignore: parameter_assignments
            return Map.fromIterables(
              value.keys as Iterable,
              value.values.map((dynamic v) => deserialize(v, targetType, growable: growable)),
            ) as ReturnType;
          }
          break;
    }
    throw Exception('Cannot deserialize');
  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy