Skip to content

Commit

Permalink
enable ignoring fields by using [NonSerialized]
Browse files Browse the repository at this point in the history
  • Loading branch information
mookid8000 committed Apr 22, 2024
1 parent 43f1b1c commit 9e555ea
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 9e555ea

Please sign in to comment.