diff --git a/lib/src/modelos/Definic.dart b/lib/src/modelos/Definic.dart index bd22c3f..3c9ac3d 100644 --- a/lib/src/modelos/Definic.dart +++ b/lib/src/modelos/Definic.dart @@ -40,7 +40,8 @@ abstract class Definic { * Al ser una clase abstracta, hay que crear un constructor propio: * https://github.com/dart-lang/json_serializable/issues/606#issuecomment-587993029 */ - factory Definic.fromJson(Map json) { + factory Definic.fromJson (Map json) { + switch (json ["clase"]) { @@ -55,8 +56,9 @@ abstract class Definic { return Expr.fromJson (json); default: - throw ArgumentError ("Invalid value provided: ${json["clase"]}. " - " Should be one of ${ClaseAcepc.values}" + throw ArgumentError ("Invalid class value provided: ${json["clase"]}. " + "Should be one of ${ClaseAcepc.values}. " + "This JSON's keys are: ${json.keys}" ); } } diff --git a/lib/src/modelos/Expr.dart b/lib/src/modelos/Expr.dart index 4330ef7..cd5aac3 100644 --- a/lib/src/modelos/Expr.dart +++ b/lib/src/modelos/Expr.dart @@ -52,9 +52,12 @@ class Expr extends Definic { /** * Constructor por defecto. * Simplemente inicializa los atributos + * + * El parámetro "clase" se declara explícitamente para poder serializar correctamente + * (ver https://github.com/dart-lang/json_serializable/issues/274). */ Expr (String this.texto - , { String id = null, List definiciones = null } + , { String id = null, List definiciones = null , ClaseAcepc clase} ): this.definiciones = (definiciones == null)? [] : definiciones , super (id, ClaseAcepc.frase_hecha) diff --git a/lib/src/modelos/Expr.g.dart b/lib/src/modelos/Expr.g.dart index c0edc98..9adab94 100644 --- a/lib/src/modelos/Expr.g.dart +++ b/lib/src/modelos/Expr.g.dart @@ -16,11 +16,53 @@ Expr _$ExprFromJson(Map json) { e == null ? null : Acepc.fromJson(e as Map)) ?.toList() ?? [], + clase: _$enumDecodeNullable(_$ClaseAcepcEnumMap, json['clase']) ?? + ClaseAcepc.manual, ); } Map _$ExprToJson(Expr instance) => { 'id': instance.id, + 'clase': _$ClaseAcepcEnumMap[instance.clase], 'texto': instance.texto, 'definiciones': instance.definiciones?.map((e) => e?.toJson())?.toList(), }; + +T _$enumDecode( + Map enumValues, + dynamic source, { + T unknownValue, +}) { + if (source == null) { + throw ArgumentError('A value must be provided. Supported values: ' + '${enumValues.values.join(', ')}'); + } + + final value = enumValues.entries + .singleWhere((e) => e.value == source, orElse: () => null) + ?.key; + + if (value == null && unknownValue == null) { + throw ArgumentError('`$source` is not one of the supported values: ' + '${enumValues.values.join(', ')}'); + } + return value ?? unknownValue; +} + +T _$enumDecodeNullable( + Map enumValues, + dynamic source, { + T unknownValue, +}) { + if (source == null) { + return null; + } + return _$enumDecode(enumValues, source, unknownValue: unknownValue); +} + +const _$ClaseAcepcEnumMap = { + ClaseAcepc.manual: 'manual', + ClaseAcepc.normal: 'normal', + ClaseAcepc.frase_hecha: 'frase_hecha', + ClaseAcepc.enlace: 'enlace', +};