-
Notifications
You must be signed in to change notification settings - Fork 84
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
Disable anchors in serialized yaml #404
Conversation
This seems like a possible YamlDotNet upstream issue to me, which should be using ReferenceEquals instead of Equals and GetHashCode for this kind of thing. Maybe worth noting in aaubry/YamlDotNet#571? |
Actually non-record classes have this issue too: public class Point : IEquatable<Point> {
public int X { get; set; }
public int Y { get; set; }
public Point() {}
public Point(int x, int y) {
X = x;
Y = y;
}
public bool Equals(Point other) {
if (ReferenceEquals(null, other)) {
return false;
}
if (ReferenceEquals(this, other)) {
return true;
}
return X == other.X && Y == other.Y;
}
public override bool Equals(object obj) {
if (ReferenceEquals(null, obj)) {
return false;
}
if (ReferenceEquals(this, obj)) {
return true;
}
if (obj.GetType() != this.GetType()) {
return false;
}
return Equals((Point)obj);
}
public override int GetHashCode() {
unchecked {
return (X * 397) ^ Y;
}
}
}
public static void Main(string[] args) {
List<Point> points = new List<Point> {
new Point(0, 0),
new Point(0, 0)
};
// - &o0
// X: 0
// Y: 0
// - *o0
YamlHelper.Serializer.Serialize(points);
} It uses a dictionary to store anchors in code, and uses the object as a key, so According to the doc:
I assume this is intended to decrease the output size (and maybe to solve cyclic reference issue?), rather than to let the output document have a closer structure with the original object. 😅 |
The docs also talk about "duplicate references", which means that they really should be using an equality comparer which uses |
Given that there haven't been any developments upstream regarding this / the related issue, I'd like to revisit the idea mentioned on (I think it was) Discord before falling back to merging this PR. Would you or anyone else be willing to try implementing a fix that changes the value equality checks to reference equality checks? Or should I attempt it in my spare time? |
I'll try it these days. |
Currently stuck because of aaubry/YamlDotNet#677, if there's still no response then I guess I need to use reflections to solve that. 😅 |
If the object has multiple same values (checked by
Equals()
andGetHashCode()
), the serializer will use anchors by default to reduce file size. However, those values will share the same reference after deserializing, causing some unexpected behaviors.Suppose the player wants to change the scale of the first node in mod settings, and the code is
result[0].Scale = 2
, then the second node is also changed.