dart-lang / yaml

A Dart YAML parser.
https://pub.dev/packages/yaml
MIT License
169 stars 58 forks source link

jsonEncode not working #90

Closed jaysson closed 3 years ago

jaysson commented 4 years ago
import 'dart:convert';

final yamlDoc = loadYaml(doc);
jsonEncode(yamlDoc);

throws

Converting object to an encodable object failed: Instance of 'YamlMap'
rithviknishad commented 3 years ago

jsonEncode cannot encode YamlMap but can encode a Map, and hence you can cast it as a Map and then try

jsonEncode(yamlDoc as Map);
jaysson commented 3 years ago

That would only work for top level. Any maps inside still throw the same error.

natebosch commented 3 years ago

We could consider adding a toJson, but since Yaml can't be round-tripped through JSON I'm not sure if it is a good idea.

Yaml has a superset of the semantics of json - if you have a YamlMap with keys of any type other than String the encoding would fail.

My preference would be to have authors convert to a Map<String, Object?> manually so they can make the decisions about how to handle Data that cannot be represented as JSON.