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

Use Object.toJSON() method to serialize #38

Merged
merged 5 commits into from
Jan 8, 2021

Conversation

misozask
Copy link
Contributor

@misozask misozask commented Dec 3, 2020

Fixes #20
Fixes #34

@misozask
Copy link
Contributor Author

misozask commented Dec 3, 2020

Solves following issues: #20 #34

@tjconcept
Copy link

What could be reasons for opting out of this functionality?

Apart from the annoyance of a new major version, I'd prefer it to be the default behavior, but it's probably for @sindresorhus to weigh in on.

readme.md Outdated Show resolved Hide resolved
readme.md Outdated
// => {date: '1970-01-01T00:00:00.000Z'}

const obj = {
foo: 'bar',

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider throwing in a rainbow, unicorn, heart or lightning here to increase the consistency, or just to appease @sindresorhus 😉

@misozask
Copy link
Contributor Author

misozask commented Dec 4, 2020

I'd prefer it to be the default behavior

It could be a breaking change for someone relying on Date to be serialized as {}

@tjconcept
Copy link

It could be a breaking change for someone relying on Date to be serialized as {}

Hence the annoyance of a new major version 🙂

@sindresorhus
Copy link
Owner

sindresorhus commented Dec 25, 2020

Yeah, I prefer a major version too. I don't really see the point in adding an option for this even. It can be added later if there's an actual need for it.

@sindresorhus sindresorhus changed the title Support Object.toJSON() method Use Object.toJSON() method to serialize Jan 1, 2021
index.js Outdated
@@ -30,11 +30,29 @@ const commonProperties = [
{property: 'code', enumerable: true}
];

const destroyCircular = ({from, seen, to_, forceEnumerable}) => {
const called = Symbol('.toJSON called');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const called = Symbol('.toJSON called');
const isCalled = Symbol('.toJSON called');

index.d.ts Outdated
class ErrorWithDate extends Error {
constructor() {
super()
this.date = new Date(0)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
this.date = new Date(0)
this.date = new Date()

readme.md Outdated
@@ -44,6 +44,36 @@ Custom properties are preserved.
Non-enumerable properties are kept non-enumerable (name, message, stack).
Enumerable properties are kept enumerable (all properties besides the non-enumerable ones).
Circular references are handled.
If the input object contains `.toJSON()` then its result will be returned instead of object properties.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
If the input object contains `.toJSON()` then its result will be returned instead of object properties.
If the input object has a `.toJSON()` method, then it's called instead of serializing the object's properties.

const err = new ErrorWithToJSON();
console.log(serializeError(err));
// => {date: '1970-01-01T00:00:00.000Z', message: '🦄', name, stack}
```
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

index.d.ts should be in sync with the readme.

readme.md Outdated
return serializeError(this);
}
}
const err = new ErrorWithToJSON();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const err = new ErrorWithToJSON();
const error = new ErrorWithToJSON();

readme.md Outdated
class ErrorWithDate extends Error {
constructor() {
super()
this.date = new Date(0)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
this.date = new Date(0)
this.date = new Date();

readme.md Outdated
```js
class ErrorWithDate extends Error {
constructor() {
super()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use semi-colons everywhere.

test.js Outdated
};
}
}
const err = new CustomError();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const err = new CustomError();
const error = new CustomError();

test.js Outdated
amount: '$20'
}
});
t.true(stack !== undefined);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use t.not

test.js Outdated
name: 'CustomError',
value: 30
});
t.true(stack !== undefined);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use t.not.

index.d.ts Outdated
@@ -13,6 +13,8 @@ Serialize an `Error` object into a plain object.
Non-error values are passed through.
Custom properties are preserved.
Circular references are handled.
If the input object has a `.toJSON()` method, then it's called instead of serializing the object's properties.
It's up on `.toJSON()` implementation to handle circular references and enumerability of the properties.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo

@sindresorhus sindresorhus merged commit 34655eb into sindresorhus:master Jan 8, 2021
@moltar
Copy link

moltar commented Jan 9, 2021

Thanks @misozask!

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.

serializeError converts a Date object into an empty plain object Consider Object.toJSON() method
4 participants