Skip to content
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

Add Jsonifiable type #492

Merged
merged 14 commits into from
Nov 5, 2022
Merged

Add Jsonifiable type #492

merged 14 commits into from
Nov 5, 2022

Conversation

itsjohncs
Copy link
Contributor

Closes #356.

@itsjohncs
Copy link
Contributor Author

Whoops, I somehow missed that undefined being allowed is desired! Admittedly I did a pretty cursory reading of #356. I'll update the type to permit undefined.

I didn't realize stringify will not recursively call `toJSON` on
objects returned from `toJSON`.
I realized that `JsonifiableObject` is more appropriately named to
accept *objects* with a `toJSON` method.
sindresorhus and others added 9 commits October 22, 2022 02:10
A previous experiment I did erroneously made me think that toJSON could
not be called on any subobjects that toJSON returned. But there was
clearly something wrong with my experiment because the object in the
new test case is serialized fine by `JSON.stringify`.
@sindresorhus sindresorhus changed the title Add Jsonifiable type. Add Jsonifiable type Oct 22, 2022
@sindresorhus sindresorhus requested a review from skarab42 November 4, 2022 06:47
@sindresorhus sindresorhus merged commit e11ab80 into sindresorhus:main Nov 5, 2022
@MichaelDeBoey
Copy link
Contributor

Should we use this type in Jsonify as well?

@skarab42
Copy link
Collaborator

skarab42 commented Nov 5, 2022

Should we use this type in Jsonify as well?

No

  • Jsonifiable is used to type values that you expect to pass to JSON.stringify() where { foo: undefined } and plain {} are functionally equivalent.
  • Jsonify is used to type parsed JSON value, coming from eg. JSON.parse() where undefined as a value is impossible.

FI #471 (comment)

@shaharmor
Copy link

shaharmor commented Jun 27, 2023

@skarab42 why not also export JsonifiableObject and JsonifiableArray?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Proposal: Jsonifyable
5 participants