Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serialize an object to string

I have the following method to save an Object to a file:

// Save an object out to the disk public static void SerializeObject<T>(this T toSerialize, String filename) {     XmlSerializer xmlSerializer = new XmlSerializer(toSerialize.GetType());     TextWriter textWriter = new StreamWriter(filename);      xmlSerializer.Serialize(textWriter, toSerialize);     textWriter.Close(); } 

I confess I did not write it (I only converted it to a extension method that took a type parameter).

Now I need it to give the xml back to me as a string (rather than save it to a file). I am looking into it, but I have not figured it out yet.

I thought this might be really easy for someone familiar with these objects. If not I will figure it out eventually.

like image 232
Vaccano Avatar asked Mar 12 '10 17:03

Vaccano


People also ask

How do you serialize an object to a string?

To serialize an object to a string, we can use the base 64 encodings. We can implement the serialization by creating two classes, one class will implement the Serializable class, and the other class will be used to create the object of the Serializable class and serialize it.

Can we serialize string in Java?

The Serializable interface must be implemented by the class whose object needs to be persisted. The String class and all the wrapper classes implement the java. io. Serializable interface by default.


2 Answers

Use a StringWriter instead of a StreamWriter:

public static string SerializeObject<T>(this T toSerialize) {     XmlSerializer xmlSerializer = new XmlSerializer(toSerialize.GetType());      using(StringWriter textWriter = new StringWriter())     {         xmlSerializer.Serialize(textWriter, toSerialize);         return textWriter.ToString();     } } 

Note, it is important to use toSerialize.GetType() instead of typeof(T) in XmlSerializer constructor: if you use the first one the code covers all possible subclasses of T (which are valid for the method), while using the latter one will fail when passing a type derived from T.    Here is a link with some example code that motivate this statement, with XmlSerializer throwing an Exception when typeof(T) is used, because you pass an instance of a derived type to a method that calls SerializeObject that is defined in the derived type's base class: http://ideone.com/1Z5J1.

Also, Ideone uses Mono to execute code; the actual Exception you would get using the Microsoft .NET runtime has a different Message than the one shown on Ideone, but it fails just the same.

like image 174
dtb Avatar answered Sep 22 '22 03:09

dtb


Serialize and Deserialize XML/JSON (SerializationHelper.cs):

using Newtonsoft.Json; using System.IO; using System.Xml.Serialization;  namespace MyProject.Helpers {     public static class SerializationHelper     {         public static T DeserializeXml<T>(this string toDeserialize)         {             XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));             using (StringReader textReader = new StringReader(toDeserialize))             {                 return (T)xmlSerializer.Deserialize(textReader);             }         }          public static string SerializeXml<T>(this T toSerialize)         {             XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));             using (StringWriter textWriter = new StringWriter())             {                 xmlSerializer.Serialize(textWriter, toSerialize);                 return textWriter.ToString();             }         }          public static T DeserializeJson<T>(this string toDeserialize)         {             return JsonConvert.DeserializeObject<T>(toDeserialize);         }          public static string SerializeJson<T>(this T toSerialize)         {             return JsonConvert.SerializeObject(toSerialize);         }     } } 
like image 33
ADM-IT Avatar answered Sep 24 '22 03:09

ADM-IT