tools
tools copied to clipboard
YamlMap.asMap & YamlList.asList
Would be nice to have something like the following. We use Yaml for configs-for-humans and json_serializable to handle parsing.
extension YamlMapToMap on YamlMap {
Map<String, Object?> get asMap => <String, Object?>{
for (final MapEntry(:key, :value) in entries)
if (value is YamlMap)
'$key': value.asMap
else if (value is YamlList)
'$key': value.asList
else
'$key': value,
};
}
extension YamlListToList on YamlList {
List<Object?> get asList => <Object?>[
for (final value in nodes)
if (value is YamlMap)
value.asMap
else if (value is YamlList)
value.asList
else
value,
];
}
That looks like a toJson function.
Seems wasteful to parts parse into Yaml, and then convert it (eagerly) into a simpler format.
Our config is in yaml. Yaml is a superset of json. If you told me we had a yaml direct to json (natural dart objects), then I'd switch.
Passing a 'yamlmap' to a json serializable will runtime fail with complex enough yaml.