-
Notifications
You must be signed in to change notification settings - Fork 85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
wrapAsYamlNode should maybe call .toJson() #76
Comments
hmm... I'm not too sure I see the utility of switching over to It seems to me as though this change will only be valuable if for some reason the user is working with both JSON and YAML representations of their data and encoding them separately. In the cases where the user is expecting to save representations of their custom objects in various configuration files, it might be better for the user to call the
@jonasfj thoughts? |
No it converts to something "encodable". The following sample actually works: import 'dart:convert';
class CustomThing {
String value;
CustomThing(this.value);
Object toJson() => <String,Object>{'value': value};
}
void main() {
final data = {
'myThing:': CustomThing("hello world"),
'otherKey': 42,
};
print(json.encode(data));
} But if instead of using The convention appears to be that custom objects with a Alternatively, anyone who wants to do this, can just do: We could also make the convention that
Well, if you have a hierarchy of custom objects, or you are storing custom objects in a import 'dart:convert';
class CustomThing {
String value;
CustomThing(this.value);
}
void main() {
final data = {
'myThing:': CustomThing("hello world"),
'otherKey': 42,
};
print(json.encode(data, toEncodable: _toEncodable));
}
Object _toEncodable(Object o) {
if (o is CustomThing) {
return <String,Object>{'value': o.value};
}
return o; // I give up
} Again, I don't have to write something to convert My suspicion is that something like |
ah, my bad, I got the |
Closed as Issue was refiled as dart-lang/yaml_edit#1 |
I think the idea with wrapAsYamlNode was that you could pass it any value that
json.encode
(fromdart:convert
) would accept, and then this would wrap it as aYamlNode
.However, reading docs for jsonEncode I realize that unless some
toEncodable
function is given,json.encode
will default to calling.toJson()
on objects. This is in turn used by package:json_serializable which generates code that helps users add a.toJson()
on their custom objects, making it possible to pass these objects tojson.encode
.I wonder if we should consider calling
.toJson()
instead of throwing:If we do this, we should certainly document it, and probably give an example. I doubt it is possible to gracefully test if the object has a
.toJson()
method, even accessing the property likeobject.toJson is Function
is liable to throw and complain that no getter exists fortoJson
(if the method isn't present).EDIT: It seems
json.encode
just catches all errors / exceptions when calling.toJson
and then rethrows them as an exception saying:Converting object to an encodable object failed: Instance of MyCustomObject
.We could also consider adding a
toEncodable
function as an optional parameter similar to jsonEncode.@walnutdust, any thoughts?
The text was updated successfully, but these errors were encountered: