You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm currently working on the JSON.stringify replacer and I noticed something that caught me a little bit off guard. When I call someValue.to_json() it is not removing properties whose values are undefined. Since value.to_json() and JSON.stringify(...) are both taking things and turning them into "JSON", I would expect them to behave similarly. MDN gives this blurb on the JSON.stringify(...) page:
undefined, Functions, and Symbols are not valid JSON values. If any such values are encountered during conversion they are either omitted (when found in an object) or changed to null (when found in an array). JSON.stringify() can return undefined when passing in "pure" values like JSON.stringify(function(){}) or JSON.stringify(undefined).
However, Boa's to_json function blindly turns null, symbols and undefined all into json null values.
If this is very intentional behavior I can certainly do some checks in the JSON.stringify() to not add properties whose values are undefined, but thought I would pose the question since putting it in the to_json function would reproduce that behavior throughout the codebase.
does not remove key/value pairs whose value is undefined
does not remove key/value pairs whose value is a function
does not remove key/value pairs whose value is a symbol
Hey thanks for raising this, I don’t think it is intentional behaviour it most likely hasn’t been dealt with. So if we should be omitting instead of Nulling them then go ahead on making that change.
Razican
changed the title
Value to_jsonValue::to_json() does not omit null or undefined values
May 16, 2020
@Razican to clarify, I do not believe that Value::to_json should omit null, nor should it always omit undefined. It depends on the situation. For example, when undefined is found as a value in an object, that key/value pair should be omitted; however, when undefined is found in an array, then it should be converted to null. Does that make sense?
Razican
changed the title
Value::to_json() does not omit null or undefined valuesValue::to_json() does not handle undefined correctly
May 16, 2020
@Razican to clarify, I do not believe that Value::to_json should omit null, nor should it always omit undefined. It depends on the situation. For example, when undefined is found as a value in an object, that key/value pair should be omitted; however, when undefined is found in an array, then it should be converted to null. Does that make sense?
I'm currently working on the JSON.stringify replacer and I noticed something that caught me a little bit off guard. When I call
someValue.to_json()
it is not removing properties whose values are undefined. Sincevalue.to_json()
andJSON.stringify(...)
are both taking things and turning them into "JSON", I would expect them to behave similarly. MDN gives this blurb on the JSON.stringify(...) page:However, Boa's to_json function blindly turns null, symbols and undefined all into json null values.
https://github.com/jasonwilliams/boa/blob/402041d43251017ab772fe6f9920e41f496a359d/boa/src/builtins/value/mod.rs#L685
If this is very intentional behavior I can certainly do some checks in the
JSON.stringify()
to not add properties whose values are undefined, but thought I would pose the question since putting it in the to_json function would reproduce that behavior throughout the codebase.e.g.
The text was updated successfully, but these errors were encountered: