-
-
Notifications
You must be signed in to change notification settings - Fork 673
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
Fix serialization of private fields in base classes #821
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this line fix? https://github.com/neuecc/MessagePack-CSharp/blob/268896d89e1565ed6cac1e685bbd4689ca5c3fcf/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Resolvers/DynamicObjectResolver.cs#L1426
this line run by forceStringKey || contractless || (contractAttr != null && contractAttr.KeyAsPropertyName
I don't think contractless wants to serialize private fields anyway, should it? |
At least, |
Ah! OK, I'll add a test for that and fix. |
So it's actually |
Given that enabling private fields in base classes greatly expand the problem of |
// [MessagePackObject(true)] is same as contractless.
[MessagePackObject(true)]
public class Base_1
{
private int field;
public int Field
{
get { return field; }
set { field = value; }
}
}
[MessagePackObject(true)]
public class Inheritance_1 : Base_1
{
private int field2;
public int Field2
{
get { return field2; }
set { field2 = value; }
}
} var data1 = new Base_1 { Field = 100 };
var data2 = new Inheritance_1 { Field = 100, Field2 = 200 };
// {"Field":100,"field":100}
Console.WriteLine(MessagePackSerializer.ConvertToJson(MessagePackSerializer.Serialize(data1, StandardResolverAllowPrivate.Options)));
// { "Field":100,"Field2":200,"field2":200}
Console.WriteLine(MessagePackSerializer.ConvertToJson(MessagePackSerializer.Serialize(data2, StandardResolverAllowPrivate.Options))); when data1 serialized result is If you want to change the behavior of this data1, it's another PR, I think that double serialization can be tolerated as it is now because Contractless cannot grasp the identity of field and property. |
I think we should consider changing the behavior for contractless, but I see that as orthogonal to the issue being fixed in this PR. This PR doesn't impact the contractless behavior, so can't we design any behavior change as a separate issue and PR? |
@AArnott |
Fixes #820