Skip to content

Commit 301ac84

Browse files
committed
Merge pull request #1808 from mookid8000/master
Enable ignoring fields by using [NonSerialized]
2 parents 32f059c + 9b7dfd0 commit 301ac84

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

Diff for: src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Resolvers/DynamicObjectResolver.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1766,7 +1766,7 @@ bool AddEmittableMemberOrIgnore(bool isIntKeyMode, EmittableMember member, bool
17661766

17671767
EmittableMember? CreateEmittableMember(MemberInfo m)
17681768
{
1769-
if (m.IsDefined(typeof(IgnoreMemberAttribute), true) || m.IsDefined(typeof(IgnoreDataMemberAttribute), true))
1769+
if (m.IsDefined(typeof(IgnoreMemberAttribute), true) || m.IsDefined(typeof(IgnoreDataMemberAttribute), true) || m.IsDefined(typeof(NonSerializedAttribute), true))
17701770
{
17711771
return null;
17721772
}

Diff for: src/MessagePack.UnityClient/Assets/Scripts/Tests/ShareTests/DataContractTest.cs

+27
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,33 @@ public class Detail : IEquatable<Detail>
195195
public bool Equals(Detail other) => other != null && this.B1 == other.B1 && this.B2 == other.B2;
196196
}
197197

198+
[Fact]
199+
public void Serialize_WithNonSerializedAttribute()
200+
{
201+
var mc = new ClassWithOldSchoolNonSerializedAttribute
202+
{
203+
PublicField = 1,
204+
IgnoredPublicField = 2,
205+
};
206+
207+
var options = ContractlessStandardResolverAllowPrivate.Options;
208+
var bin = MessagePackSerializer.Serialize(mc, options);
209+
var mc2 = MessagePackSerializer.Deserialize<ClassWithOldSchoolNonSerializedAttribute>(bin, options);
210+
211+
mc2.PublicField.Is(1);
212+
mc2.IgnoredPublicField.Is(0);
213+
214+
MessagePackSerializer.ConvertToJson(bin).Is(@"{""PublicField"":1}");
215+
}
216+
217+
public class ClassWithOldSchoolNonSerializedAttribute
218+
{
219+
public int PublicField;
220+
221+
[NonSerialized]
222+
public int IgnoredPublicField;
223+
}
224+
198225
#if !UNITY_2018_3_OR_NEWER
199226

200227
[Fact]

0 commit comments

Comments
 (0)