Reputation: 2843
I want to serialize objects to strings, and back.
We use protobuf-net to turn an object into a Stream and back, successfully.
However, Stream to string and back... not so successful. After going through StreamToString
and StringToStream
, the new Stream
isn't
deserialized by protobuf-net; it raises an Arithmetic Operation resulted in an Overflow
exception. If we deserialize the original stream, it works.
Our methods:
public static string StreamToString(Stream stream)
{
stream.Position = 0;
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
return reader.ReadToEnd();
}
}
public static Stream StringToStream(string src)
{
byte[] byteArray = Encoding.UTF8.GetBytes(src);
return new MemoryStream(byteArray);
}
Our example code using these two:
MemoryStream stream = new MemoryStream();
Serializer.Serialize<SuperExample>(stream, test);
stream.Position = 0;
string strout = StreamToString(stream);
MemoryStream result = (MemoryStream)StringToStream(strout);
var other = Serializer.Deserialize<SuperExample>(result);
Upvotes: 284
Views: 577711
Reputation: 1062855
This is so common but so profoundly wrong. Protobuf data is not string data. It certainly isn't ASCII. You are using the encoding backwards. A text encoding transfers:
You do not have "formatted bytes". You have arbitrary bytes. You need to use something like a base-n (commonly: base-64) encode. This transfers
Look at Convert.ToBase64String
and Convert.FromBase64String
.
Upvotes: 72
Reputation: 353
StreamReader reader = new StreamReader(strm, System.Text.Encoding.UTF8);
var final1 = reader.ReadToEnd();
The main reason for most issue is encoding type... here by default System.Text.Encoding.UTF8 fixes the issue...
Enjoy...
Upvotes: 9
Reputation: 1008
a UTF8 MemoryStream to String conversion:
var res = Encoding.UTF8.GetString(stream.GetBuffer(), 0 , (int)stream.Length)
Upvotes: 16
Reputation: 2416
I want to serialize objects to strings, and back.
Different from the other answers, but the most straightforward way to do exactly that for most object types is XmlSerializer:
Subject subject = new Subject();
XmlSerializer serializer = new XmlSerializer(typeof(Subject));
using (Stream stream = new MemoryStream())
{
serializer.Serialize(stream, subject);
// do something with stream
Subject subject2 = (Subject)serializer.Deserialize(stream);
// do something with subject2
}
All your public properties of supported types will be serialized. Even some collection structures are supported, and will tunnel down to sub-object properties. You can control how the serialization works with attributes on your properties.
This does not work with all object types, some data types are not supported for serialization, but overall it is pretty powerful, and you don't have to worry about encoding.
Upvotes: 1
Reputation: 32681
I have just tested this and works fine.
string test = "Testing 1-2-3";
// convert string to stream
byte[] byteArray = Encoding.ASCII.GetBytes(test);
MemoryStream stream = new MemoryStream(byteArray);
// convert stream to string
StreamReader reader = new StreamReader(stream);
string text = reader.ReadToEnd();
If stream
has already been written to, you might want to seek to the beginning before first before reading out the text: stream.Seek(0, SeekOrigin.Begin);
Upvotes: 381
Reputation: 2942
I wrote a useful method to call any action that takes a StreamWriter
and write it out to a string instead. The method is like this;
static void SendStreamToString(Action<StreamWriter> action, out string destination)
{
using (var stream = new MemoryStream())
using (var writer = new StreamWriter(stream, Encoding.Unicode))
{
action(writer);
writer.Flush();
stream.Position = 0;
destination = Encoding.Unicode.GetString(stream.GetBuffer(), 0, (int)stream.Length);
}
}
And you can use it like this;
string myString;
SendStreamToString(writer =>
{
var ints = new List<int> {1, 2, 3};
writer.WriteLine("My ints");
foreach (var integer in ints)
{
writer.WriteLine(integer);
}
}, out myString);
I know this can be done much easier with a StringBuilder
, the point is that you can call any method that takes a StreamWriter
.
Upvotes: 2
Reputation: 556
In usecase where you want to serialize/deserialize POCOs, Newtonsoft's JSON library is really good. I use it to persist POCOs within SQL Server as JSON strings in an nvarchar field. Caveat is that since its not true de/serialization, it will not preserve private/protected members and class hierarchy.
Upvotes: 0
Reputation: 61
Try this.
string output1 = Encoding.ASCII.GetString(byteArray, 0, byteArray.Length)
Upvotes: 6
Reputation: 63065
When you testing try with UTF8
Encode stream like below
var stream = new MemoryStream();
var streamWriter = new StreamWriter(stream, System.Text.Encoding.UTF8);
Serializer.Serialize<SuperExample>(streamWriter, test);
Upvotes: 6