Enable ignoring fields by using [NonSerialized] by mookid8000 · Pull Request #1808 · MessagePack-CSharp/MessagePack-CSharp · GitHub
Skip to content

Enable ignoring fields by using [NonSerialized]#1808

Merged
AArnott merged 1 commit into
MessagePack-CSharp:masterfrom
mookid8000:master
Apr 24, 2024
Merged

Enable ignoring fields by using [NonSerialized]#1808
AArnott merged 1 commit into
MessagePack-CSharp:masterfrom
mookid8000:master

Conversation

@mookid8000

Copy link
Copy Markdown
Contributor

MessagePack-CSharp is nice enough to simply skip serialization of properties and fields tagged with [IgnoreDataMember] and [IgnoreMember] without fuzz, which seems appropriate.

Since .NET also contains the [NonSerialized] attribute, which I believe is the oldest "dear serializer, please ignore this field"-type of serializer hint, it seems natural to also ignore these. This will in turn make migration from the old BinaryFormatter (which is deprecated and no longer exists in .NET 9) much easier, because [NonSerialized] was how it was told to skip specific fields.

This PR adds an extra case to the CreateEmittableMember method of DynamicObjectResolver, which causes fields decorated with [NonSerialized] to be ignored. It's backed up by an additional test Serialize_WithNonSerializedAttribute in DataContractTest.

@AArnott AArnott left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks great. Thank you for the PR and the test.

@AArnott AArnott merged commit 76cc9a3 into MessagePack-CSharp:master Apr 24, 2024
@AArnott AArnott added this to the v2.5 milestone Apr 24, 2024
This was referenced Jun 15, 2026
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.

2 participants