decodeMap<K extends Object, V extends Object> method
Map<K, V>
decodeMap<K extends Object, V extends Object>(
- String jsonPath,
- Object? jsonData, {
- JsonDecoderCallback<
K> ? keyDecoder, - JsonDecoderCallback<
V> ? valueDecoder,
inherited
Decode a JSON object that is expected to be a Map. keyDecoder
is used
to decode the keys, and valueDecoder
is used to decode the values.
Implementation
Map<K, V> decodeMap<K extends Object, V extends Object>(
String jsonPath, Object? jsonData,
{JsonDecoderCallback<K>? keyDecoder,
JsonDecoderCallback<V>? valueDecoder}) {
if (jsonData == null) {
return {};
} else if (jsonData is Map) {
var result = <K, V>{};
jsonData.forEach((key, value) {
K decodedKey;
if (keyDecoder != null) {
decodedKey = keyDecoder('$jsonPath.key', key);
} else {
decodedKey = key as K;
}
if (valueDecoder != null) {
value = valueDecoder('$jsonPath[${json.encode(key)}]', value);
}
result[decodedKey] = value as V;
});
return result;
} else {
throw mismatch(jsonPath, 'Map', jsonData);
}
}