473,394 Members | 1,709 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

C# App: Binary Serialization File Size Allocation

71
hi, I would just like to know if there is a way to manually calculate the size of my structure to anticipate the file size of the object after serialization?

Is it reliable to depend on the file size of the usual result after serializing a certain structure? I mean, if I serialize an object then get the size of the result, will the result on the next serialization will still be the same even I change the data inside the structure?

What I am trying to achieve here is to serialize multiple objects but assign a specific block/position for them inside a file.

I hope I delivered my questions clearly that someone could help me.

Thanks in advance.
Dec 29 '08 #1
8 3986
Plater
7,872 Expert 4TB
I think it depends on your structure?
If you only use hard sized Types (ints, bytes, etc) then I think it is fine.
But if you have an array ([] or string or Collection type) then I think the size of the serialized object will be different?
Might be better to have a "table" header at the begining of the file that points out where the offsets are?
Dec 29 '08 #2
dantz
71
Thanks for the reply.
But I still want to know the exact amount of bytes that my structure can consume.

I was trying to test a specific structure with a single property of int.
Please have a look on the code below.

Expand|Select|Wrap|Line Numbers
  1. public class GameState : ISerializable
  2.     {
  3.         private int inttest;
  4.         public int IntTester
  5.         {
  6.             get { return inttest; }
  7.             set { inttest = value; }
  8.         }
  9.  
  10.         //private GlobalState _gstate = new GlobalState();
  11.         //private PlayerState _pstate = new PlayerState();
  12.  
  13.  
  14.         public GameState() { }
  15.         public GameState(SerializationInfo info, StreamingContext ctxt)
  16.         {
  17.             inttest = (int)info.GetValue("IntTester", typeof(int));
  18.         }
  19.  
  20.         public void GetObjectData(SerializationInfo info, StreamingContext context)
  21.         {
  22.             info.AddValue("IntTester", inttest);
  23.         }
  24.     }
  25.  
This structure consumes 151bytes.
If I only have "int" inside that should consume only 4bytes,where did the other remaining bytes came from? Will this 151 bytes be enough to depend on(structure size will always be the same) that it will not change in the future for whatever reason?


Thanks in advance.
Dec 30 '08 #3
Plater
7,872 Expert 4TB
Serialization seems to be boggling a lot of minds lately.
You have to remember that serialization is supposed to be bi-directional. You are supposed to get the exact same object back. All the other bytes are data used to describe that exact class type.

Consider:
Expand|Select|Wrap|Line Numbers
  1. public class myclass1 : ISerializable 
  2.   public int myint=0;
  3. }
  4. public class myclass2 : ISerializable 
  5.    public int myint=0;
  6. }
  7.  
If it only stored the value of the int, how would it know which class to instanciate in memory when you reloaded it?
Dec 30 '08 #4
dantz
71
For that I was thinking that I need to create a separate function that checks the Type of the return object in Deserialization and a function that converts that object into its own type.

Do you think it is a good workaround?
I cant think of other idea,please let me know if you have one or any reaction on that.

I am really stuck into implementing serializing/deserializing any objects so my functions must most of the time dynamic.

Thanks in advance.
Dec 31 '08 #5
Plater
7,872 Expert 4TB
Well I am not an expert on serialization, but there are [attributes] that you can apply to your class I think to determine how(and what?) it serializes. Then you use the built in functions to recreate the object?
I don't think you need to do really any work about deciding what class it is, the built in deserialized portion should be able to do it
Dec 31 '08 #6
mldisibio
190 Expert 100+
When you serialize, two things happen:
1. The actual members are written to disk. If these are fixed-size value types, they should occupy the same amount of bytes each time.
2. Meta-data about your class is written to the serialization stream. As Plater said, the meta-data is used to determine the exact source object that needs to be re-created. This includes the assembly name, version and other reflection data. My opinion would be not to rely on that always being the same number of bytes. It probably would be the same if you never recompiled the assembly.

While the serialization attributes allow you to control which members are serialized or not, it will not be as easy to control what meta-data is written to your serialization stream.

Dantz, I think what you want to do is write only the member data to file (for example, your int field) and then on reconstruction, you are simply creating a new object and filling it with the data which was stored to file. Your code takes on the responsibility of knowing what object to create, rather than the serialization. To do so, you might add a "fixed-length" field which indicates the object type.

So you are "emulating" serialization, but you would not be using the built-in serialization libraries of .Net. Nor should you try to "override" the serialization methods. You need to decide if this work-around is what you want, but .Net serialization always needs the meta-data of the object it is dealing with.
Dec 31 '08 #7
RedSon
5,000 Expert 4TB
Also, don't forget you can write your own serializer and control it how you like.
Dec 31 '08 #8
dantz
71
Thank you for your replies and happy new year to all!

RedSon:
That idea sounds good, I think I may give it a try but with limited time I hope I can finish soon. :)

mldisibio:
For #1 I tested all of my structures and it all give the same sizes even for strings. So now I just use that size then double it(for safety reasons) to be able to get anticipate the size of my structure.

For #2 Yes that is what i want to do. I will try your suggestion then just get back if there are still problems but I think I achieve what I want based on all your opinions.

Plater:
Ok, I will check on those [attributes]


Again, thank you for all your replies.
Jan 2 '09 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Charles T. | last post by:
Hi, I currently writing a serialize/unserialize architecture. The read/write function will read/write from a binary file. My question is is there some sort on defined standart to use when...
9
by: Ching-Lung | last post by:
Hi all, I try to create a tool to check the delta (diff) of 2 binaries and create the delta binary. I use binary formatter (serialization) to create the delta binary. It works fine but the...
8
by: Roberto Rocco | last post by:
Hello, I have a struct and want to stream it to a file (binary). I cannot use because this is not supported under the Compact Framework. What is the best approach to use? Another question in...
2
by: phyzics | last post by:
I am porting an application from C++ to C#, and am having trouble finding a way to quickly and efficiently write structures to a binary file. In C++ this is trivial because all that is necessary is...
2
by: Dave Veeneman | last post by:
I'm working on a project where I have to persist data to a file, rather than to a database. Basically, I need to save the state of several classes, each of which will have a couple of dozen...
11
by: ajou_king | last post by:
I was running some tests on my Win32 1GHZ processor to see how long it would take to transmit objects numerous times via TCP/IP using C# ..NET Remoting vs the C++ trustworthy method of binary...
15
by: Jacques | last post by:
Hi I am an dotNet newby, so pardon my ignorance. I am looking for a method of saving/copying a managed class to a stream/file WITHOUT saving the object's state, eg. if I have a ref class with...
0
by: shantanuaggarwal | last post by:
Hi, My scenario is as follows: I have an application, in which in one component, I am calling a method in a different thread. Please note that this method will load a dynamic assembly. Now...
12
by: =?Utf-8?B?enRSb24=?= | last post by:
Hi all, I recently came across something really strange and after a couple of days of debugging, I finally nailed the cause of it. However, I have absolutely no idea what I am doing wrong or is...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.