Skip to content

Commit

Permalink
Merge pull request #1808 from mookid8000/master
Browse files Browse the repository at this point in the history
Enable ignoring fields by using [NonSerialized]
  • Loading branch information
AArnott committed Apr 24, 2024
2 parents 92d6365 + 9e555ea commit 76cc9a3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
Expand Up @@ -1766,7 +1766,7 @@ bool AddEmittableMemberOrIgnore(bool isIntKeyMode, EmittableMember member, bool

EmittableMember? CreateEmittableMember(MemberInfo m)
{
if (m.IsDefined(typeof(IgnoreMemberAttribute), true) || m.IsDefined(typeof(IgnoreDataMemberAttribute), true))
if (m.IsDefined(typeof(IgnoreMemberAttribute), true) || m.IsDefined(typeof(IgnoreDataMemberAttribute), true) || m.IsDefined(typeof(NonSerializedAttribute), true))
{
return null;
}
Expand Down
Expand Up @@ -195,6 +195,33 @@ public class Detail : IEquatable<Detail>
public bool Equals(Detail other) => other != null && this.B1 == other.B1 && this.B2 == other.B2;
}

[Fact]
public void Serialize_WithNonSerializedAttribute()
{
var mc = new ClassWithOldSchoolNonSerializedAttribute
{
PublicField = 1,
IgnoredPublicField = 2,
};

var options = ContractlessStandardResolverAllowPrivate.Options;
var bin = MessagePackSerializer.Serialize(mc, options);
var mc2 = MessagePackSerializer.Deserialize<ClassWithOldSchoolNonSerializedAttribute>(bin, options);

mc2.PublicField.Is(1);
mc2.IgnoredPublicField.Is(0);

MessagePackSerializer.ConvertToJson(bin).Is(@"{""PublicField"":1}");
}

public class ClassWithOldSchoolNonSerializedAttribute
{
public int PublicField;

[NonSerialized]
public int IgnoredPublicField;
}

#if !UNITY_2018_3_OR_NEWER

[Fact]
Expand Down

0 comments on commit 76cc9a3

Please sign in to comment.