-
-
Notifications
You must be signed in to change notification settings - Fork 549
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
Accept non-JSON types with .toJSON()
method in Jsonify
type
#257
Conversation
Maybe call the new type as |
@leosuncin - Is this the name you're suggesting if the existing I am hoping people will like be amicable to a single
If we agree to a single But if we really need to separate handling the 2 cases into separate methods, then we need to think about a good name. I am not sure about |
I think it makes sense to support anything with a |
// @BendingBender In case you have any opinions. Feel free to ignore. |
I don't have an opinion on this. |
@sindresorhus - Please review the updated documentation. |
Jsonify
type: Adds ability to Jsonify Non JSON that is Jsonable with toJSON().toJSON()
method in Jsonify
type
Fixes #256
@leosuncin pointed out the desire to
Jsonify
non-JSON that is Jsonable viaJSON.parse(JSON.stringify(v))
. The example provided was an interfaceProduct
that was clearly not assignable toJsonValue
because it containsDate
values. ButDate
implements atoJSON()
method so the non-JSON value can be converted to JSON in a predictable way.This pull request updates
Jsonify
to also transform non-JSON values that are Jsonable. Where Jsonable means the non-JSON value implements.toJSON()
method that returns a value that is assignable toJsonValue
.In the tests,
parsedStringifiedX
uses a non-JSON type that currently exists in the tests and is similar to the example @leosuncin provided. As well, I added a couple of classes of non-JSON objects. (One of the non JSON objects is Jsonable and the other is not)Some items to reflect on and possibly discuss in this review:
Jsonify
strictly apply to JSON values only? Or is it acceptable to include non-JSON values that are Jsonable withtoJSON()
?Jsonify
recursive cast properties of Date instead of cast to string #256 was raised.Jsonify
was originally designed to transform aJSON
interface
to atype
that is assignable toJsonValue
. It was not designed to transform non-JSON. So my initial feeling was thatJsonify
recursive cast properties of Date instead of cast to string #256 was not a bug. But the idea of usingJSON.stringify()
, which leverages the inner object's.toJSON()
methods, raises a compelling use case.Jsonify
as is... and have a newJsonifyJsonable
type to handle non-JSON that is Jsonable.JsonifyJsonable
type? (This is my only doubt because as I approached thinking aboutJsonify
recursive cast properties of Date instead of cast to string #256 my initial feeling was that it should be a separate type... I can't create a strong argument for it though. And I am leaning towards relaxingJsonify
to also support non-Json but Jsonable input.)