Replace BinaryFormatter with DataContractSerializer#641
Replace BinaryFormatter with DataContractSerializer#641MiroBrodlovaUnityGit wants to merge 3 commits intomasterfrom
Conversation
BinaryFormatter is deprecated in future .NET versions and may prevent assemblies from unloading or return incorrect results. Replaced with System.Runtime.Serialization.DataContractSerializer as recommended for compatibility. Changes: - KdTree.cs: Replaced BinaryFormatter with DataContractSerializer in SaveToFile and LoadFromFile methods - KdTreeNode.cs: Added DataContract and DataMember attributes - Added [DataContract] and [DataMember] attributes to all serialized classes Related to SCP-1555
Codecov ReportAll modified and coverable lines are covered by tests ✅ @@ Coverage Diff @@
## master #641 +/- ##
=======================================
Coverage 35.57% 35.57%
=======================================
Files 277 277
Lines 34897 34897
=======================================
Hits 12416 12416
Misses 22481 22481
Flags with carried forward coverage won't be shown. Click here to find out more. 🚀 New features to boost your workflow:
|
|
Marking this ready for review just for the purpose of triggering the automatic PR CI jobs (to get a gauge for how these changes fare)! Hope that's OK :) |
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
🤖 Helpful? Please react with 👍/👎 | Questions❓Please reach out in Slack #ask-u-pr-agent |
PR Code Suggestions ✨Explore these optional code suggestions:
🤖 Helpful? Please react with 👍/👎 | Questions❓Please reach out in Slack #ask-u-pr-agent |
|||||||||
Switch from text XML to binary XML format using XmlDictionaryWriter.CreateBinaryWriter() and XmlDictionaryReader.CreateBinaryReader(). This provides: - Binary format similar to BinaryFormatter (more compact and efficient) - Better performance for large KdTree data structures - Still uses DataContractSerializer for CoreCLR compatibility Note: This changes the file format. Existing serialized KdTree files will need to be regenerated.
| public static KdTree<TKey, TValue> LoadFromFile(string filename) | ||
| { | ||
| BinaryFormatter formatter = new BinaryFormatter(); | ||
| var serializer = new DataContractSerializer(typeof(KdTree<TKey, TValue>)); |
There was a problem hiding this comment.
just wondering how does this work in terms of backward compatibility? For older projects tha thave saved using the old BinaryFormatter, would using DataContractSerializer work?
There was a problem hiding this comment.
This is an excellent question, thanks for asking it!
Replacing the BinaryFormatter is indeed a breaking change, or at least DataContractSerializer cannot correctly read data written by the BinaryFormatter. So old projects that have KdTree data saved using BinaryFormatter.Serialize() will not be able to load those files after this change. There are other packages that are facing the same situation, such as Addressables (you can see some of our discussion here in their PR about it)
There was a problem hiding this comment.
We've discussed this a bit within the team (I'm in Scripting Runtime), and have discussed how each team that uses BinaryFormatter will be most well-versed in the needs of their users and package, and so we leave it absolutely to your discretion how you wish to replace it of course. We need to have BinaryFormatter replaced in all packages before we'd turn on CoreCLR (as it is not compatible with CoreCLR), so it will be a blocker for CoreCLR (just sharing in case it's helpful to take into account)
What are your/your team's thoughts?
Overview
BinaryFormatter isn't compatible with CoreCLR and therefore its usages need to be removed as Unity moves into CoreCLR. This PR replaces all instances of
BinaryFormatterwithSystem.Runtime.Serialization.DataContractSerializerin this repo, and is part of Epic SCP-1555.Previous Behavior
The package used deprecated BinaryFormatter which is deprecated in future .NET versions and may prevent assemblies from unloading or return incorrect results.
New & Expected Behavior
The package now uses DataContractSerializer for serialization/deserialization with [DataContract] and [DataMember] attributes added to all serialized classes.
Changes
Further Context
JIRA-ticket
Testing
Running the default job-suite. Please share if there are additional steps needed!