# Ruby Marshal unexpected results

**URL:** <https://rubytalk.org/t/ruby-marshal-unexpected-results/55711>\
**Category:** ruby-talk\
**Created:** [7 October 2009 15:13 UTC](https://rubytalk.org/t/ruby-marshal-unexpected-results/55711 "2009-10-07T15:13:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aaron\_Vegh](https://avatars.discourse-cdn.com/v4/letter/a/45deac/32.png) [@Aaron\_Vegh](https://rubytalk.org/u/Aaron_Vegh)\
**Post date:** [7 October 2009 15:13 UTC](https://rubytalk.org/t/ruby-marshal-unexpected-results/55711/1 "2009-10-07T15:13:18Z")

</div>

Hi there,  
For a Rails project, I'm trying to serialize some data in a hash, using  
Marshal. However, this data is getting serialized in unexpected ways.  
I'm not sure if there's a problem with Marshal, my data, or me.

In Rails, I have a parameter that is a hash of data from a form:

{"305"=\>"Complete trim detail at top of stringer where basement  
staircase meets first floor level. ",  
"25"=\>"The vertical joint between the two adjacent masonry wythes at  
the ... do not appear to be bonded or tied together. Cracking of the  
mortar is resulting. \t\t\t\t\t\t  
",  
"728"=\>"The mantle above factory built fireplace in the family room has  
been installed in an un-workmanlike manner. Gaps between the mantle and  
wall are too excessive. "}

In my controller, I serialize the data with Marshal:

data = Marshal.dump(p)

But when I save it, I get an error. Here's the beginning of it:

SQLite3::SQLException: unrecognized token:  
"'C:HashWithIndifferentAccess{"305"": UPDATE "reports" SET  
"updated\_at" = '2009-10-07 14:49:26', "data" =  
'C:HashWithIndifferentAccess..."

I logged the result of that Marshal call, and this is what I got:

C:HashWithIndifferentAccess305"`Complete trim detail at top of stringer  
where basement staircase meets first floor level. "25"?The vertical  
joint between the two adjacent masonry wythes at the ... do not appear  
to be bonded or tied together. Cracking of the mortar is resulting.  
728"?The mantle above factory built fireplace in the family room has  
been installed in an un-workmanlike manner. Gaps between the mantle and  
wall are too excessive.

Which to me doesn't look at all like what Marshal should be producing.  
So what gives?

Thanks,  
Aaron.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [7 October 2009 18:06 UTC](https://rubytalk.org/t/ruby-marshal-unexpected-results/55711/2 "2009-10-07T18:06:01Z")

</div>

Aaron Vegh wrote:

> Hi there,  
> For a Rails project, I'm trying to serialize some data in a hash, using  
> Marshal. However, this data is getting serialized in unexpected ways.  
> I'm not sure if there's a problem with Marshal, my data, or me.
> 
> In Rails, I have a parameter that is a hash of data from a form:
> 
> {"305"=\>"Complete trim detail at top of stringer where basement  
> staircase meets first floor level. ",  
> "25"=\>"The vertical joint between the two adjacent masonry wythes at  
> the ... do not appear to be bonded or tied together. Cracking of the  
> mortar is resulting. \t\t\t\t\t\t  
> ",  
> "728"=\>"The mantle above factory built fireplace in the family room has  
> been installed in an un-workmanlike manner. Gaps between the mantle and  
> wall are too excessive. "}
> 
> In my controller, I serialize the data with Marshal:
> 
> data = Marshal.dump(p)
> 
> But when I save it, I get an error. Here's the beginning of it:
> 
> SQLite3::SQLException: unrecognized token:  
> "'C:HashWithIndifferentAccess{"305"": UPDATE "reports" SET  
> "updated\_at" = '2009-10-07 14:49:26', "data" =  
> 'C:HashWithIndifferentAccess..."
> 
> I logged the result of that Marshal call, and this is what I got:
> 
> C:HashWithIndifferentAccess305"`Complete trim detail at top of stringer  
> where basement staircase meets first floor level. "25"?The vertical  
> joint between the two adjacent masonry wythes at the ... do not appear  
> to be bonded or tied together. Cracking of the mortar is resulting.  
> 728"?The mantle above factory built fireplace in the family room has  
> been installed in an un-workmanlike manner. Gaps between the mantle and  
> wall are too excessive.
> 
> Which to me doesn't look at all like what Marshal should be producing.  
> So what gives?
> 
> Thanks,  
> Aaron.

require 'rubygems'  
require 'sqlite3'

db = SQLite3::Database.new( "mytest.db" )  
db.execute("drop table mytable")  
db.execute("create table mytable (data TEXT)")

h = {  
"305"=\>"Complete trim detail at top of stringer where basement  
staircase meets first floor level. ",

"25"=\>"The vertical joint between the two adjacent masonry wythes at  
the ... do not appear to be bonded or tied together. Cracking of the  
mortar is resulting. \t\t\t\t\t\t  
",

"728"=\>"The mantle above factory built fireplace in the family room has  
been installed in an un-workmanlike manner. Gaps between the mantle and  
wall are too excessive. "  
}

str = Marshal.dump(h)  
p str

--output:--  
"\004\b{\b\"\b305\"`Complete trim detail at top of stringer where  
basement\nstaircase meets first floor level. \"\a25\"\001\241The  
vertical joint between the two adjacent masonry wythes at\nthe ... do  
not appear to be bonded or tied together. Cracking of the\nmortar is  
resulting. \t\t\t\t\t\t\n\"\b728\"\001\242The mantle above factory  
built fireplace in the family room has\nbeen installed in an  
un-workmanlike manner. Gaps between the mantle and\nwall are too  
excessive. "

command = "insert into mytable (data) values ('#{str}')"  
db.execute(command)  
command = "insert into mytable (data) values ('hello world')"  
db.execute(command)

rows = db.execute("select \* from mytable")  
p rows

--output:--  
[["\004\b{\b\"\b305\"`Complete trim detail at top of stringer where  
basement\nstaircase meets first floor level. \"\a25\"\001\241The  
vertical joint between the two adjacent masonry wythes at\nthe ... do  
not appear to be bonded or tied together. Cracking of the\nmortar is  
resulting. \t\t\t\t\t\t\n\"\b728\"\001\242The mantle above factory  
built fireplace in the family room has\nbeen installed in an  
un-workmanlike manner. Gaps between the mantle and\nwall are too  
excessive. "], ["hello world"]]

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Ryan\_Davis1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/ryan_davis1/32/1848_2.png) [@Ryan\_Davis1](https://rubytalk.org/u/Ryan_Davis1)\
**Post date:** [7 October 2009 20:40 UTC](https://rubytalk.org/t/ruby-marshal-unexpected-results/55711/3 "2009-10-07T20:40:19Z")

</div>

1) this is a rails question, not a ruby question. You'd be better off asking over there.

2) Doesn't activerecord already support object serialization out of the box?

> **···**
>
> On Oct 7, 2009, at 08:13 , Aaron Vegh wrote:
> 
> > For a Rails project, I'm trying to serialize some data in a hash, using  
> > Marshal. However, this data is getting serialized in unexpected ways.  
> > I'm not sure if there's a problem with Marshal, my data, or me.

---

<div class="post-metadata">

**Author:** ![Aaron\_Vegh](https://avatars.discourse-cdn.com/v4/letter/a/45deac/32.png) [@Aaron\_Vegh](https://rubytalk.org/u/Aaron_Vegh)\
**Post date:** [7 October 2009 18:12 UTC](https://rubytalk.org/t/ruby-marshal-unexpected-results/55711/4 "2009-10-07T18:12:14Z")

</div>

7stud -- wrote:

[perfectly-functional code demonstrating Marshal and SQLite3 at work]

Nice looking code! But that doesn't solve my problem, I'm afraid. I've  
used Marshal before many times. My question isn't "how do I use it", but  
"why the heck is it not serializing my data properly?

Apologies if I don't spy the solution within your code, but you didn't  
provide any commentary with your code...

Thanks,  
Aaron.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![John\_W\_Higgins1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/john_w_higgins1/32/1922_2.png) [@John\_W\_Higgins1](https://rubytalk.org/u/John_W_Higgins1)\
**Post date:** [7 October 2009 18:28 UTC](https://rubytalk.org/t/ruby-marshal-unexpected-results/55711/5 "2009-10-07T18:28:34Z")

</div>

Aaron,

The short answer appears to be that you are not using a hash but rather the  
HashWithIndifferentAccess class which apparently in combination with Marshal  
spits out characters (I'm guessing the tick at the end of this section right  
here "\004\b{\b\"\b305\"`Complete) which are not conducive to storage within  
SQLite.

My quick and dirty suggestion would be to switch your data back to a raw  
hash prior to SQLite storage and then reverse the process on the way back  
out of the database.

For example,

data = Marshal.dump(p.to\_hash)

and

p = HashWithIndifferentAccess.new(data)

John

---

<div class="post-metadata">

**Author:** ![Aaron\_Vegh](https://avatars.discourse-cdn.com/v4/letter/a/45deac/32.png) [@Aaron\_Vegh](https://rubytalk.org/u/Aaron_Vegh)\
**Post date:** [7 October 2009 19:12 UTC](https://rubytalk.org/t/ruby-marshal-unexpected-results/55711/6 "2009-10-07T19:12:14Z")

</div>

John W Higgins wrote:

> My quick and dirty suggestion would be to switch your data back to a raw  
> hash prior to SQLite storage and then reverse the process on the way  
> back out of the database.

That was a good suggestion! It pushed me further along, that's for sure.  
Your suggestion that it may be related to bad characters really tipped  
me off. I'm now employing strip on the text that does into the form  
that's submitted, so no whitespace surrounds the content I'm processing.  
But there does appear to be a rogue character that is hanging up the  
SQLite database. Here's the current result of my serialization:

740"?The mantle...

The quote-question mark is what appears to be causing the problem, but I  
can't identify where that's coming from. I'm really scratching my head  
over this one.

Thanks,  
Aaron.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
