- Home /
SerializationException: End of Stream encountered before parsing was completed
Hello everybody,
hours of research and debugging couldn't help me, which is why you are my last hope. The error "SerializationException: End of Stream encountered before parsing was completed" occurs when I try to read a file from a local folder.
I already tried:
using "using" instead of just stream.Close();
setting stream.position = 0
searching for errors like serialized class not serializable or other syntax errors
The main class to save:
using UnityEngine;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public static class SaveHandler
{
private static string filePath = Application.persistentDataPath + "/2401202.sav";
public static void save()
{
BinaryFormatter bf = new BinaryFormatter();
using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate))
{
//fs.Position = 0;
SaveDataCollector saveData = new SaveDataCollector();
bf.Serialize(fs, saveData);
}
}
public static void load()
{
if (File.Exists(filePath))
{
BinaryFormatter bif = new BinaryFormatter();
using (FileStream fes = new FileStream(filePath, FileMode.Open))
{
//fes.Position = 0;
SaveDataCollector savoDato = bif.Deserialize(fes) as SaveDataCollector;
savoDato.distributeData();
}
}
else
{
Debug.Log("Errorcode 401: Savefile not Found");
}
}
}
public static void load()
{
if (File.Exists(filePath))
{
BinaryFormatter bif = new BinaryFormatter();
using (FileStream fes = new FileStream(filePath, FileMode.Open))
{
//fes.Position = 0;
SaveDataCollector savoDato = bif.Deserialize(fes) as SaveDataCollector;
savoDato.distributeData();
}
}
else
{
Debug.Log("Errorcode 401: Savefile not Found");
}
}
The serialized class:
using UnityEngine;
[System.Serializable]
public class SaveDataCollector
{
public float highScore;
public bool legacyControl;
public SaveDataCollector()
{
legacyControl = InterData.legacyControl;
highScore = InterData.highScore;
}
public void distributeData()
{
InterData.legacyControl = this.legacyControl;
InterData.highScore = this.highScore;
}
}
Please help me, I'm really getting desperate. Thanks in advance!
Cannot give you a more precise answer right now but this error might occur if you changed your savedatacollector structure and are trying to deserialize a file with the old data structure.
Answer by Bunny83 · Jan 24, 2020 at 05:24 PM
You most likely try to load invalid or outdated data. Do you have modified your "SaveDataCollector" after you saved the file? This would completely break the file you saved. That's the main reason why the BinaryFormatter is a bad choice for a savegame solution apart from the large amount of overhead.
Have you tried actually deleting your save file and saving a new one? Have you checked if the file was actually created? Note that your save class can only store a single float and a single bool value at the moment.
Hi, first off, thank you very much for your answer.
Yes, the file has been created and I deleted and created new ones multiple times with different names. And yes my save class isnt generic at all, but its okay for my needs and I am already researching OR$$anonymous$$.
You said, that BinaryFormatter isn't the ideal choice. I learned it from Brackeyes YT Channel. What method would you suggest?
Sincerely, Skeasy
The BinaryFormatter uses $$anonymous$$icrosoft's Remoting protocol. It's a verbose binary format. However it is also quite intolerant to any kind of changes to the structure. Also note that your approach is quite dangerous. The constructor of your SaveDataCollector directly reads static fields on another class. You really shouldn't do that. Whenever an instance is created the constructor will be called.
Better data format choices are common flexible formats like JSON, X$$anonymous$$L or you could roll your own binary format which would be way smaller and you have full control over the content. Of course to actually create a binary format that doesn't have the same issues as the BinaryFormatter you have to take care of deciding what format you deal with. Almost all file formats contain some sort of version inside the file header. This ensures that you know for sure what data is actually stored in the file.
If you want to use JSON you can either use Unity's JsonUtility or any other JSON framework like my SimpleJSON framework for example. Unity's JsonUtility is also an object mapper. So you have to create serializable classes which Unity can serialize. However the serializer is a lot more tolerant and also allows partial overwriting. $$anonymous$$y SimpleJSON does not map the data to fix predefined classes but just parses the JSON and provide an easy interface to work with the data.
If you want to use a binary format, the easiest way is to use the BinaryWriter / BinaryReader. If you want to see an example use, have a look at my custom $$anonymous$$eshSerializer which can serialize any Unity $$anonymous$$esh into a custom binary format.
This was exactly the scenario in my case. Thanks for the answer!
Your answer
![](https://koobas.hobune.stream/wayback/20220612222047im_/https://answers.unity.com/themes/thub/images/avi.jpg)