<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: serialization code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 17 May 2008 20:35:01 GMT</pubDate>
    <description>DZone Snippets: serialization code</description>
    <item>
      <title>Convert object to byte array and viceversa (serialization)</title>
      <link>http://snippets.dzone.com/posts/show/3897</link>
      <description>&lt;code&gt;&lt;br /&gt;&lt;br /&gt;// Convert an object to a byte array&lt;br /&gt;private byte[] ObjectToByteArray(Object obj)&lt;br /&gt;{&lt;br /&gt;    if(obj == null)&lt;br /&gt;        return null;&lt;br /&gt;    BinaryFormatter bf = new BinaryFormatter();&lt;br /&gt;    MemoryStream ms = new MemoryStream();&lt;br /&gt;    bf.Serialize(ms, obj);&lt;br /&gt;    return ms.ToArray();&lt;br /&gt;}&lt;br /&gt;// Convert a byte array to an Object&lt;br /&gt;private Object ByteArrayToObject(byte[] arrBytes)&lt;br /&gt;{&lt;br /&gt;    MemoryStream memStream = new MemoryStream();&lt;br /&gt;    BinaryFormatter binForm = new BinaryFormatter();&lt;br /&gt;    memStream.Write(arrBytes, 0, arrBytes.Length);&lt;br /&gt;    memStream.Seek(0, SeekOrigin.Begin);&lt;br /&gt;    Object obj = (Object) binForm.Deserialize(memStream);&lt;br /&gt;    return obj;&lt;br /&gt;}&lt;br /&gt; &lt;br /&gt;p.s. for custom classes add [Serializable] attribute to enable serialization &lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 24 Apr 2007 22:48:07 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3897</guid>
      <author>mstampar (Miroslav Stampar)</author>
    </item>
    <item>
      <title>C# - XML serialization from string</title>
      <link>http://snippets.dzone.com/posts/show/2552</link>
      <description>From http://weblogs.asp.net/whaggard/archive/2006/09/04/String-Streams-in-.Net.aspx&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;xmlSerializer.Deserialize(new StringReader(xmlString));&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 06 Sep 2006 00:18:56 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2552</guid>
      <author>hjnntaao (hjnntaao)</author>
    </item>
    <item>
      <title>All Exceptions Created Equal</title>
      <link>http://snippets.dzone.com/posts/show/1509</link>
      <description>All exceptions are created equal. But what if you have a very good and pressing reason to serialize one to save it in a database? For instance, you had a collection of checks you wanted to run against some data, and save the results?&lt;br /&gt;&lt;br /&gt;Ah, that's easy! serialize() was made for that! So off you trot, you make a few changes to the code, add a blank line here, a blank line there, and suddenly your code can't find the exact matches of the serialized object in the database.&lt;br /&gt;&lt;br /&gt;HUH? What's going on? You're the exact same Exception I just threw three minutes ago, and you've sporadically broken?&lt;br /&gt;&lt;br /&gt;It took me a while to twig. When you create an exception ($e = new Exception("foo");), it's shiny and new and listens when you do equality comparisions (==).&lt;br /&gt;&lt;br /&gt;But things go awry: you throw a new Exception from your filter, catch it, and serialize it. You haven't remembered that...&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;$a = new Exception("foo");&lt;br /&gt;try {&lt;br /&gt;    throw $a; //Line 1&lt;br /&gt;} catch (Exception $e) {&lt;br /&gt;    throw $a; //Line 10;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;will result in two difference traces. One saying "I was thrown on on line 1", the other "I was thrown on on line 10"...&lt;br /&gt;&lt;br /&gt;Fuck oath, hello stupid coder. You've been wracking your brains wondering why every time you go off and edit a different bit of code it serializes differently; and there you have it.&lt;br /&gt;&lt;br /&gt;How the hell do I fix it? Going to __sleep() on the job actually helps.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;class DumbException extends Exception {&lt;br /&gt;    /**&lt;br /&gt;     * Cleanup anything we need before serialisation&lt;br /&gt;     *&lt;br /&gt;     * @return  string[]    An array of member varible names to serialize&lt;br /&gt;     * @see     http://php.planetmirror.com/manual/en/language.oop5.magic.php&lt;br /&gt;     */&lt;br /&gt;    public function __sleep() {&lt;br /&gt;        return array('string','code');&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    /**&lt;br /&gt;     * Compare against another DumbException for equality.&lt;br /&gt;     *&lt;br /&gt;     * Since two exceptions can be !== because the trace / line / file&lt;br /&gt;     * information is different, we need to do this.&lt;br /&gt;     */&lt;br /&gt;    public function cmp(DumbException $e) {&lt;br /&gt;        return (serialize($e) == serialize($this));&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;print '&lt;pre&gt;';&lt;br /&gt;$a = new DumbException();&lt;br /&gt;$b = new DumbException();&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;try {&lt;br /&gt;    try {&lt;br /&gt;        throw $b;&lt;br /&gt;    } catch (Exception $e) {&lt;br /&gt;        throw $a;&lt;br /&gt;    }&lt;br /&gt;} catch (Exception $e) {&lt;br /&gt;&lt;br /&gt;    var_dump($a === $b);&lt;br /&gt;    var_dump($a == $b);&lt;br /&gt;    var_dump($b === $e);&lt;br /&gt;    var_dump($b == $e);&lt;br /&gt;    var_dump($a === $e);&lt;br /&gt;    var_dump($a == $e);&lt;br /&gt;&lt;br /&gt;    var_dump($b-&gt;cmp($e));&lt;br /&gt;    var_dump($a-&gt;cmp($e));&lt;br /&gt;}&lt;br /&gt;print '&lt;/pre&gt;';&lt;br /&gt;?&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 17 Feb 2006 10:01:44 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1509</guid>
      <author>CloCkWeRX (Daniel O'Connor)</author>
    </item>
  </channel>
</rss>
