-
Notifications
You must be signed in to change notification settings - Fork 504
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
question: How to serialize a class instance with default values automatically removed? #1505
Comments
Hello @ahochsteger, could you please provide an example for your question? If I understand correctly, there is no option for what you want, but I cannot say it for sure without an example. Thanks |
@diffy0712 have a look at this commented-out test case that is currently failing: // See the source for the class definitions
it("should serialize to JSON config without default values", () => {
const obj = getNewRootObj(jsonData)
const actual: any = instanceToPlain(obj, {
exposeDefaultValues: false,
exposeUnsetFields: false,
})
expect(actual).toMatchObject(jsonData) // <- this is OK
expect(actual.str).toBeUndefined() // <- this is failing
expect(actual.nested?.num).toBeUndefined() // <- this is failing
expect(actual.nested?.bool).toBeUndefined() // <- this is failing
expect(actual.nestedArray[0]?.bool).toBeUndefined() // <- this is failing
expect(actual.nestedArray[0]?.arr).toBeUndefined() // <- this is failing
}) It looks like it's the same as this issue: #1609 |
hello @ahochsteger, Closing this issue in favor of #1609, please follow the bugfix there. |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
I was trying to...
I have a large config class in TypeScript with a lot of default values defined in the class (e.g. empty strings, some boolean values, empty arrays) that I want to serialize back to a minimal JSON representation that just contain the essential part of the config with all properties removed that have values identical to their defined default values.
The problem:
How can I do that in a generic way by just using the information that is available from the class definition without having add extra annotation to these properties?
The text was updated successfully, but these errors were encountered: