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
Feature Request: in dump/safeDump support toJSON API #391
Comments
IMHO this is not related to yaml or i don't understand how this can work. Please provide more details or close issue. Yaml dumper is to write yaml. If you need to write json, you probably need another package. |
Trying again 😸 Suppose I have an object
However, This is handy, because it can spare me one step:
example: myInstance1 = {
sayHi: function(){
console.log('hello', this._name);
},
_name: 'world',
getData: function() {
return {name: this._name};
}
}
myInstance2 = {
sayHi: function(){
console.log('hello', this._name);
},
_name: 'world',
toJSON: function(){
return {name: this._name};
}
}
> JSON.stringify(myInstance1)
'{"_name":"world"}'
> JSON.stringify(myInstance2)
'{"name":"world"}'
Since the behavior of I would like to use current behavior:// ignores toJSON
> yaml.safeDump(myInstance2, {skipInvalid: true})
'\n_name: world\n'
// ignores toJSON
> yaml.safeDump(myInstance2, {skipInvalid: true, toJSON: true})
'\n_name: world\n' requested behavior:// ignores toJSON
> yaml.safeDump(myInstance1, {skipInvalid: true})
'\n_name: world\n'
// uses toJSON!
> yaml.safeDump(myInstance2, {skipInvalid: true, toJSON: true})
'\nname: world\n' NOTE: about |
IMHO it's better to keep things separate. If you know you can normalize object to json, then you can do: yaml.safeDump(JSON.parse(your_obj.toJSON())) This should work right now and does not require any changes. |
Yeah, but that wouldn't work with deeper structures.
I think it's better to be input-compatible with |
example deeper serialization: myInstance3 = {
sayHi: function(){
console.log('hello', this._name);
},
_name: 'world',
toJSON: function(){
return {name: this._name, deep: this.deep};
},
deep: {
_value: 1234,
toJSON: function(){
return {value: this._value};
}
}
}
> JSON.stringify(myInstance3)
'{"name":"world","deep":{"value":1234}}' Update with a more realistic example:To illustrate with a more lifelike use case, suppose I have a tree-structure made from instances of function Node(name) {
this._name = name;
this.children = [];
// well, this is not exactly beautiful, apparently toJSON must be an ownProperty
this.toJSON = this.toJSON;
}
Node.prototype.sayHi = function(){
console.log('hello', this._name);
}
Node.prototype.toJSON = function() {
return {name: this._name, children: this.children};
}
> parent = new Node('parent')
Node { _name: 'parent', children: [], toJSON: [Function] }
> parent.children.push(new Node('child1'), new Node('child2'));
2
> JSON.stringify(parent)
'{"name":"parent","children":[{"name":"child1","children":[]},{"name":"child2","children":[]}]}' Now, I don't have to teach |
About your example, that's not how it works: yaml.safeDump(JSON.parse(your_obj.toJSON())) this would actually work (but it is madness): yaml.safeDump(JSON.parse(JSON.stringify(your_obj))); Please note: |
IMHO this package should work according to yaml spec, without magic. Your features are out of yaml spec. Hear are enougth problems without additional magic. If problem can be splitted to 2 independent stages, it's better to keep things separate. What do you think? |
The YAML spec does not cover that at all, but the JSON spec doesn't cover
I think function prepareYAMLdump(obj) {
var obj_, i, l, keys, key,
replaceableTypes = new Set(['object', 'function']);
if(obj === null || !replaceableTypes.has(typeof obj))
return obj;
if(Object.prototype.hasOwnProperty.call(obj, 'toJSON')
&& typeof obj.toJSON === 'function')
// run the replacer
obj_ = obj.toJSON();
else
obj_ = obj;
keys = Object.keys(obj_);
if(keys.length && obj_ === obj)
// don't modify the original
obj_ = Object.assign({}, obj);
for(i=0,l=keys.length;i<l;i++) {
key = keys[i];
obj_[key] = prepareYAMLdump(obj_[key]);
}
return obj_;
} yaml.safeDump(prepareYAMLdump(your_obj)); Though, enabling |
Because there are no resources to write code just for fun :). When we follow spec - we know exactly where to stop. Without spec that can be endless investigations & iterations. I don't object against such feature, but will not participate myself :). |
I'd like to know how
That's totally OK. But, if I would implement it and PR it, it would still become part of the project and thus would add to the maintenance burden. |
Apparently, this is a duplicate of #339 |
Could you then refer/move new info to existing issue and close this one? |
toJSON
documented here forJSON.stringify
:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#toJSON()_behavior
If
dump
andsafeDump
would support this, js-yaml would be better as a direct replacement for JSON. For example, Immutable providestoJSON
. Could be optional, as far as I am concerned, so it's backwards compatible.The text was updated successfully, but these errors were encountered: