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
UTF8Encodingobject is a kind of serializer, usingGetBytes()andGetString(), 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:
and a method to add data to a serialization stream in the form:
A
stringin .NET is marked[Serializable]and has both of these… neither of which is called in the call toUTF8Encoding.GetBytes()orUTF8Encoding.GetString()There are lots of examples of how to serialize objects… one way is to use a
BinaryFormatterto write to aFileStream(you could also use aMemoryStream):To deserialize:
HTH,
James