简体   繁体   中英

c# binary serialization to file line by line or how to seperate

I have a collection of objects at runtime, which is already serializable, I need to persist the state of the object to a file. I did a quick coding using BinaryFormatter and saved A serialized object to a file.

I was thinking that I can save object per line. but when i open the file in a notepad, it was longer than a line. It wasnt scrolling. How can i store an binary serialized object per line?

I am aware that i can use a separator after each object so while reading them back to the application, i can know the end of the object. Well, according to information theory, this increases the size of the data(Sipser book).

What s the best algorithm to come up with a separator that woudldnt break the information?

Instead of binary serialization? Do you think JSon format is more feasible? can i store the entity in a json format, line by line?

Also, serialization/deserialization introduces overhead, hits the performance. Would Json be faster?

ideas?

Thanks.

Thanks.

Serialization functions like a FIFO queue, you dont have to read parts of the file because the formatter does it for you you just have to know the order you pushed objects inside.

public class Test
    {

        public void testSerialize()
        {
            TestObj obj = new TestObj();
            obj.str = "Some String";
            IFormatter formatter = new BinaryFormatter();
            Stream stream = new FileStream("MyFile.bin", FileMode.Create, FileAccess.Write, FileShare.None);
            formatter.Serialize(stream, obj);
            formatter.Serialize(stream, 1);
            formatter.Serialize(stream, DateTime.Now);
            stream.Close();
        }

        public void TestDeserialize()
        {
            Stream stream = new FileStream("MyFile.bin", FileMode.Open, FileAccess.Read, FileShare.None);
            IFormatter formatter = new BinaryFormatter();
            TestObj obj = (TestObj)formatter.Deserialize(stream);
            int obj2 = (int)formatter.Deserialize(stream);
            DateTime dt = (DateTime)formatter.Deserialize(stream);
            stream.Close();
        }
    }

    [Serializable]
    class TestObj
    {
        public string str = "1";
        int i = 2;
    }

Well,

Serialization/deserialization introduces overhead, would Json be faster?

JSON is still a form of serialisation, and no it probably wouldn't be faster than binary serialisation - binary serialisation is intended to be compact and quick, wheras JSON serialisation puts more emphasis on readability and so many be slower as is very likely to be less compact.

You could serialise each object individually and emit some separator between each object (eg a newline character), but I don't know what separator you could use that is guarenteed to not appear in the serialised data (what happens if you serialise a string containing a newline character?).

If you use a separator that the.Net serialisation framework emits then obviously you will make it difficult (if not impossible) to correctly determine where the breaks between objects are leading to deserialisation failures.

Why exactly do you want to put each object on its own line?

Binary serialization saves the data to arbitrary bytes; these bytes can include newline characters.

You're asking to use newlines as separators. Newlines are no different from other separators; they will also increase the size of the data.

You could also create a ArrayList and add the objects to it and then serialize it;)

ArrayList list = new ArrayList();
list.Add(1);
list.Add("Hello World");
list.Add(DateTime.Now);

BinaryFormatter bf = new BinaryFormatter();

FileStream fsout = new FileStream("file.dat", FileMode.Create);
bf.Serialize(fsout, list);
fsout.Close();

FileStream fsin = new FileStream("file.dat", FileMode.Open);
ArrayList list2 = (ArrayList)bf.Deserialize(fsin);

fsin.Close();

foreach (object o in list2)
   Console.WriteLine(o.GetType());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM