Serialize A Dictionary

  • Thread starter Andrew Robinson
  • Start date
A

Andrew Robinson

I need to serialize a dictionary so I can encode the contents. I have the
following working but the size seems large. I am guessing that I am
serializing the entire object not just the data. Is there a better way?

MemoryStream stream = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();

Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary.Add("name", "andrew");
dictionary.Add("home", "bellingham");

formatter.Serialize(stream, dictionary);
byte[] b = stream.ToArray();

How do I use "dictionary.GetObjectData()" ?

Thanks,
 
N

Nicholas Paldino [.NET/C# MVP]

Andrew,

You don't use GetObjectData. This is a method on the ISerializable
interface that is implemented to provide custom serialization semantics.

You aren't going to be able to adjust the size of the output of
serialization. Yes, you are serializing the entire object, which means the
keys and values. If you need to serialize just the values, get the
ICollection implementation that returns the values (through the Values
property) and serialize that.

Hope this helps.
 
A

Andrew Robinson

Nicholas,

Thanks, but serializing dictionary.Values actually produces a larger stream.

I am thinking that I might have to roll my own very lightweight class.

-Andrew

Nicholas Paldino said:
Andrew,

You don't use GetObjectData. This is a method on the ISerializable
interface that is implemented to provide custom serialization semantics.

You aren't going to be able to adjust the size of the output of
serialization. Yes, you are serializing the entire object, which means
the keys and values. If you need to serialize just the values, get the
ICollection implementation that returns the values (through the Values
property) and serialize that.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Andrew Robinson said:
I need to serialize a dictionary so I can encode the contents. I have the
following working but the size seems large. I am guessing that I am
serializing the entire object not just the data. Is there a better way?

MemoryStream stream = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();

Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary.Add("name", "andrew");
dictionary.Add("home", "bellingham");

formatter.Serialize(stream, dictionary);
byte[] b = stream.ToArray();

How do I use "dictionary.GetObjectData()" ?

Thanks,
 
N

Nicholas Paldino [.NET/C# MVP]

Andrew,

Why not just compress the stream, if you are that concerned about it.

How many bytes are we talking about here? Why such a concern?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Andrew Robinson said:
Nicholas,

Thanks, but serializing dictionary.Values actually produces a larger
stream.

I am thinking that I might have to roll my own very lightweight class.

-Andrew

Nicholas Paldino said:
Andrew,

You don't use GetObjectData. This is a method on the ISerializable
interface that is implemented to provide custom serialization semantics.

You aren't going to be able to adjust the size of the output of
serialization. Yes, you are serializing the entire object, which means
the keys and values. If you need to serialize just the values, get the
ICollection implementation that returns the values (through the Values
property) and serialize that.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Andrew Robinson said:
I need to serialize a dictionary so I can encode the contents. I have the
following working but the size seems large. I am guessing that I am
serializing the entire object not just the data. Is there a better way?

MemoryStream stream = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();

Dictionary<string, string> dictionary = new Dictionary<string,
string>();
dictionary.Add("name", "andrew");
dictionary.Add("home", "bellingham");

formatter.Serialize(stream, dictionary);
byte[] b = stream.ToArray();

How do I use "dictionary.GetObjectData()" ?

Thanks,
 
A

Andrew Robinson

Ideally, I would like to pass my stream in a URL. Even with a few name value
pairs, I am up around 2000 bytes and that is before I encrypt it with the
rijndael managed provider.

Maybe there is a better approach to this whole thing. I need to pass a
number of name value pairs. I just don't know at design time how many pairs
or even what the name are. Typically 2 to 4. Originally, I thought that a
dictionary was my best approach.

Thanks,

Nicholas Paldino said:
Andrew,

Why not just compress the stream, if you are that concerned about it.

How many bytes are we talking about here? Why such a concern?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Andrew Robinson said:
Nicholas,

Thanks, but serializing dictionary.Values actually produces a larger
stream.

I am thinking that I might have to roll my own very lightweight class.

-Andrew

Nicholas Paldino said:
Andrew,

You don't use GetObjectData. This is a method on the ISerializable
interface that is implemented to provide custom serialization semantics.

You aren't going to be able to adjust the size of the output of
serialization. Yes, you are serializing the entire object, which means
the keys and values. If you need to serialize just the values, get the
ICollection implementation that returns the values (through the Values
property) and serialize that.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I need to serialize a dictionary so I can encode the contents. I have
the following working but the size seems large. I am guessing that I am
serializing the entire object not just the data. Is there a better way?

MemoryStream stream = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();

Dictionary<string, string> dictionary = new Dictionary<string,
string>();
dictionary.Add("name", "andrew");
dictionary.Add("home", "bellingham");

formatter.Serialize(stream, dictionary);
byte[] b = stream.ToArray();

How do I use "dictionary.GetObjectData()" ?

Thanks,
 
N

Nicholas Paldino [.NET/C# MVP]

Andrew,

Trying to send any significant amount of information through the URL is
not a good idea, mainly because of the limitation on the URL size.

A better idea would be to POST the data, and then have your ASPX page
(or whatever is processing it on the other side), deserialize it.

Is it .NET that is working on the other side?

Have you considered a web service in this case? It would be much better
suited for what you want to do.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Andrew Robinson said:
Ideally, I would like to pass my stream in a URL. Even with a few name
value pairs, I am up around 2000 bytes and that is before I encrypt it
with the rijndael managed provider.

Maybe there is a better approach to this whole thing. I need to pass a
number of name value pairs. I just don't know at design time how many
pairs or even what the name are. Typically 2 to 4. Originally, I thought
that a dictionary was my best approach.

Thanks,

Nicholas Paldino said:
Andrew,

Why not just compress the stream, if you are that concerned about it.

How many bytes are we talking about here? Why such a concern?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Andrew Robinson said:
Nicholas,

Thanks, but serializing dictionary.Values actually produces a larger
stream.

I am thinking that I might have to roll my own very lightweight class.

-Andrew

in message Andrew,

You don't use GetObjectData. This is a method on the ISerializable
interface that is implemented to provide custom serialization
semantics.

You aren't going to be able to adjust the size of the output of
serialization. Yes, you are serializing the entire object, which means
the keys and values. If you need to serialize just the values, get the
ICollection implementation that returns the values (through the Values
property) and serialize that.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I need to serialize a dictionary so I can encode the contents. I have
the following working but the size seems large. I am guessing that I am
serializing the entire object not just the data. Is there a better way?

MemoryStream stream = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();

Dictionary<string, string> dictionary = new Dictionary<string,
string>();
dictionary.Add("name", "andrew");
dictionary.Add("home", "bellingham");

formatter.Serialize(stream, dictionary);
byte[] b = stream.ToArray();

How do I use "dictionary.GetObjectData()" ?

Thanks,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top