473,379 Members | 1,330 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,379 software developers and data experts.

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
5 47093
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Benton | last post by:
Hi there, I could serialize a class until I added this member to it: private Dictionary<string><TableMapper> = new Dictionary<string><TableMapper>(); The class has the attribute, but now...
0
by: Tim_Mac | last post by:
hi, i am able to serialize a normal generic List<> no problem. But when i try to serialize a Dictionary<string,string>, i get an exception as shown below: "The type...
1
by: Gabe Covert | last post by:
I have the following classes defined (gets/sets removed for brevity): public class Warband { public Warband() { Contents = new Dictionary<string, WarbandContent>(); }
3
by: fstorck | last post by:
Hi, I'm kind of stuck with an serializing / deserializing problem using a generic dictionary holding references to various generic types. It goes as follows: <code> class MyBase :...
0
by: =?Utf-8?B?S2V2aW4gU2NobmVpZGVy?= | last post by:
I have two projects (one using .NET 1.1 the other using 2.0). When I call a particular method in a webservice from the 1.1 project everything works great. When I call the same method from the 2.0...
1
by: not_a_commie | last post by:
So I thought I saw recently some new MS serializer class (part of workflow foundation or something) that would serialize a lot of types the old XmlSerializer would not do. This included generic...
2
by: Andy B | last post by:
Is it possible to serialize to xml a dictionary<string, stringobject in ..net 3.5?
2
by: =?Utf-8?B?U2hhd24=?= | last post by:
Hi; I would like to be able to use the XMLSerializer to serialize and deserialize a dictionary. is that possible? i know that you can serialize an object that implements the ICollection interface....
2
by: Andy B | last post by:
I don't know if this is even working or not but here is the problem. I have a gridview that I databound to a dictionary<string, stringcollection: Contract StockContract = new Contract();...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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...

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.