467,923 Members | 1,449 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,923 developers. It's quick & easy.

Serialize A Dictionary

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,
Dec 2 '05 #1
  • viewed: 45455
Share:
5 Replies
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]
- mv*@spam.guard.caspershouse.com

"Andrew Robinson" <ne****@nospam.nospam> wrote in message
news:u6**************@TK2MSFTNGP10.phx.gbl...
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,

Dec 2 '05 #2
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uR*************@TK2MSFTNGP10.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Andrew Robinson" <ne****@nospam.nospam> wrote in message
news:u6**************@TK2MSFTNGP10.phx.gbl...
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,


Dec 2 '05 #3
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]
- mv*@spam.guard.caspershouse.com

"Andrew Robinson" <ne****@nospam.nospam> wrote in message
news:uW**************@TK2MSFTNGP11.phx.gbl...
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:uR*************@TK2MSFTNGP10.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Andrew Robinson" <ne****@nospam.nospam> wrote in message
news:u6**************@TK2MSFTNGP10.phx.gbl...
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,



Dec 2 '05 #4
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@TK2MSFTNGP14.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Andrew Robinson" <ne****@nospam.nospam> wrote in message
news:uW**************@TK2MSFTNGP11.phx.gbl...
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:uR*************@TK2MSFTNGP10.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Andrew Robinson" <ne****@nospam.nospam> wrote in message
news:u6**************@TK2MSFTNGP10.phx.gbl...
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,



Dec 2 '05 #5
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]
- mv*@spam.guard.caspershouse.com

"Andrew Robinson" <ne****@nospam.nospam> wrote in message
news:uj**************@TK2MSFTNGP10.phx.gbl...
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:%2****************@TK2MSFTNGP14.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Andrew Robinson" <ne****@nospam.nospam> wrote in message
news:uW**************@TK2MSFTNGP11.phx.gbl...
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:uR*************@TK2MSFTNGP10.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Andrew Robinson" <ne****@nospam.nospam> wrote in message
news:u6**************@TK2MSFTNGP10.phx.gbl...
>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,
>



Dec 2 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Benton | last post: by
reply views Thread by Tim_Mac | last post: by
reply views Thread by =?Utf-8?B?S2V2aW4gU2NobmVpZGVy?= | last post: by
1 post views Thread by not_a_commie | last post: by
2 posts views Thread by =?Utf-8?B?U2hhd24=?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.