serializing a c# string?
Just wanted to know if serializing a string is equivalent to getting its bytes?
开发者_如何学编程System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
Byte[] responseBytes = encoding.GetBytes(Reader.ReadToEnd());
Is that serializing? Assuming the Reader.ReadToEnd() is returning a string. If not, how do you serialize a string ?
Serializing an object means writing its bytes in a format that can be used to recreate the object elsewhere.
I'd expand on this and say that serializing an object means writing its bytes to an object that can be read by a deserializer, e.g. a BinaryFormatter.Deserialize()
, which takes a Stream.
Getting a string's bytes isn't the same thing, especially when you pick an arbitrary encoding mechanism. To use those bytes to serialize your string, you'll have to write your own deserializer.
You could argue that a UTF8Encoding
object is a kind of serializer, using GetBytes()
and GetString()
, but it's not a standard 'serializer', e.g. you can't use it to pass your string over a WCF Service.
To be deserialized, an object has to have a constructor in the form:
public MyObject(SerializationInfo info, StreamingContext ctx) {...}
and a method to add data to a serialization stream in the form:
public void GetObjectData(SerializationInfo info, StreamingContext ctxt){...}
A string
in .NET is marked [Serializable]
and has both of these... neither of which is called in the call to UTF8Encoding.GetBytes()
or UTF8Encoding.GetString()
There are lots of examples of how to serialize objects... one way is to use a BinaryFormatter
to write to a FileStream
(you could also use a MemoryStream
):
string myString = "Whee!";
FileStream fileStream = new FileStream("MyString.txt", FileMode.OpenOrCreate, FileAccess.Write);
try {
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fileStream, myString);
}
finally {
fileStream.Close();
}
To deserialize:
string myString;
FileStream fileStream = new FileStream("MyString.txt", FileMode.Open, FileAccess.Read);
try {
BinaryFormatter formatter = new BinaryFormatter();
myString= (string) formatter.Deserialize(fileStream);
}
finally {
fileStream.Close();
}
HTH,
James
Sure this can be known as serialization, any process which converts one thing into something else that is safe for transmission (or strange) can be considered serialization. The important part is that you can send those bytes to the destination and do the reverse process (deserialize) into the same format.
Another example of serialization might be to convert an object to an XML document. And then converting the XML document back into an object would be deserialization.
I think that may be subjective based on what you mean by serializing.. but yes the bytes of the string would be serialization.
But if there is a spec that is expected to be followed for "serializing" data, the particular method of converting the string to a certain byte encoding may or may not be what is expected.
Serializing just means that you are writing the object out to a datastore so that you could retrieve it later. In this case it looks like you are serializing in that you are putting the string in a format that it can be transmitted to a storage medium.
This is probably bad to do here, but wikipedia has a really good write up.
http://en.wikipedia.org/wiki/Serialization
精彩评论