Comments (4)
ah, my bad, I got the json.encode
function mixed up with toJson
. Thank you for providing the examples and all! Those do sound like reasonable enhancements that will help wrapAsYamlNode
, and I'll work on them soon! :)
from dart-neats.
hmm... I'm not too sure I see the utility of switching over to toJson()
. The default toJson()
essentially looks at the object, determines if its a List/Map/bool/string
or some other primitive type to encode it in a string, and we are already doing something analogous in wrapAsYamlNode
. I also think that having the standard behaviour depends on toJson()
, when JSON is a separate (though compatible!) format and getting that to work with package:json_serializable
might feel kind of weird. Furthermore, toJson
converts it to a string, and the resulting YAML might not fit the expectations?
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 toJson()
on their side to obtain a representation that is entirely in terms of List/Map/bool/string/etc
for the various configuration languages rather than expecting that to be the default behavior?
toEncodable()
might provide some convenience, but I'm not sure if it's worth modifying the library for? I think the main argument for adding a toEncodable()
for us would be to help us achieve an API that is similar to json.encode
, but the user could simply have the encoding function as a class function and call it before passing their object to us.
@jonasfj thoughts?
from dart-neats.
Furthermore, toJson converts it to a string, and the resulting YAML might not fit the expectations?
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 json.encode
I try to do: wrapAsYamlNode(data)
that will throw.
The convention appears to be that custom objects with a .toJson()
method, can be encoded by json.encode
. I'm suggesting that we consider allowing wrapAsYamlNode
to wrap custom objects that have a toJson()
method.
Alternatively, anyone who wants to do this, can just do: wrapAsYamlNode(json.decode(json.encode(data)))
, and that'll work perfectly fine :D
We could also make the convention that wrapAsYamlNode
will try to call .toYaml()
and if that doesn't exist, then try to call .toJson()
.
toEncodable()
might provide some convenience, but I'm not sure if it's worth modifying the library for? I think the main argument for adding atoEncodable()
for us would be to help us achieve an API that is similar tojson.encode
, but the user could simply have the encoding function as a class function and call it before passing their object to us.
Well, if you have a hierarchy of custom objects, or you are storing custom objects in a Map<String,CustomThing>
then you would need to write a method to convert that too... toEncodable
gets called for all elements in tree, unless it's already encodable (ofcourse). Another way to do my sample above is:
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 Map<String,dynamic>
or Map<String,CustomThing>
or List<CustomThing>
, just something that converts CustomThing
into something that is encodable as JSON.
My suspicion is that something like toEncodable
and toJson
would be convenient for wrapAsYamlNode
.
from dart-neats.
Closed as package:yaml_edit
has moved to dart-lang/yaml_edit.
Issue was refiled as dart-lang/yaml_edit#1
from dart-neats.
Related Issues (20)
- Exception while closing redis connection HOT 1
- Trailing comma in mount force us to use trailing comma eveytime for root url HOT 1
- Doesn't work with Riverpod HOT 4
- router.get method should be able to pass context HOT 6
- add support to execute task in isolated (in background outside the main thread) HOT 3
- Migrate to use same pattern as UrlPattern HOT 3
- get current attempt times HOT 4
- [sanitize_html] Why section tag not allowed? HOT 1
- Should replace | with : in '/user/<userId|[0-9]+>', : looks more like a natural constraint symbol HOT 1
- consider updating the 'repository' pubspec fields with the repo+path_in_the_repo information HOT 4
- Put `lib/` directory into root of vendored package? HOT 4
- package retry -- terminology / semantics HOT 2
- consider this as a host for package:directories? HOT 4
- move top level pubspec into the tool/ dir?
- Enabling retry on any type of thrown errors HOT 2
- Callback for time left until next task. HOT 3
- Allow more/custom labels in `encodePemBlock` HOT 4
- Update to make RetryOptions final is breaking mocks HOT 2
- Could not start() after stop() HOT 1
- Proposal `obtain` / `getOrCreate` function HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dart-neats.