Error when trying to Deserialize binary data

Issue #311 resolved
Jérémy Guéry created an issue

Hey!

I'am trying to Deserialize data from a file in Binary format with the SerializationUtility.DeserializeValue method.

But I have an issue when I try to load the data InvalidOperationException: Invalid binary data stream: could not parse peeked BinaryEntryType byte '189' into a known entry type. For information, when I'am using the JSON format it works very well.

I'm building a save system on top of the Odin Serialization, so if you are interested I can submit issues like that when I encouter Binary or Json serialization problems.

I made a small repro case: https://pastebin.com/Wni5a6Fu You need to add the "TestSerialization" behaviour to an empty GameObject in an empty scene.

In the TestSerialization class you can set the format to use (JSON or Binary). And for saving time, I logged the path to the generated file.

Unity version: 2017.1.0p3.

Odin version: 1.0.6.1

Editor Only is disable, I am using Odin Serialization

OS: Windows 10

Thanks!

Comments (4)

  1. Bjarke Elias

    Hey J

    Don't believe this is a problem with the serializer, but instead a problem with how you are writing and reading the data from the file. Because Serializing and deserializing that data works fine for me.

    Try this instead:

        public void SaveData()
        {
            byte[] bytes = SerializationUtility.SerializeValue(data, format);
            using (var fs = new FileStream(SaveUtils.FilePath, FileMode.Create, FileAccess.Write, FileShare.Write))
            using (var writer = new BinaryWriter(fs))
            {
                writer.Write(bytes);
            }
        }
    
        public void LoadData()
        {
            using (var fs = new FileStream(SaveUtils.FilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
            using (var reader = new BinaryReader(fs))
            {
                this.data = SerializationUtility.DeserializeValue<Data>(reader.BaseStream, format);
            }
        }
    
  2. Bjarke Elias
    • changed status to open

    You might also want to take a look in SerializationUtility.cs, those are just handy utilities to quickly serialize something in a single line of code, but behind the scenes they also just use stream, so you can possible simplify what's happening if you want. But try that example first.

  3. Jérémy Guéry reporter

    You are right, with your code it's working great, my bad. I will take a look at SerializationUtility.cs. Thanks!

  4. Log in to comment