Ruby Marshal unexpected results

Discussion in 'Ruby' started by Aaron Vegh, Oct 7, 2009.

  1. Aaron Vegh

    Aaron Vegh Guest

    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/.
     
    Aaron Vegh, Oct 7, 2009
    #1
    1. Advertising

  2. Aaron Vegh

    7stud -- Guest

    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/.
     
    7stud --, Oct 7, 2009
    #2
    1. Advertising

  3. Aaron Vegh

    Aaron Vegh Guest

    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/.
     
    Aaron Vegh, Oct 7, 2009
    #3
  4. [Note: parts of this message were removed to make it a legal post.]

    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
     
    John W Higgins, Oct 7, 2009
    #4
  5. Aaron Vegh

    Aaron Vegh Guest

    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/.
     
    Aaron Vegh, Oct 7, 2009
    #5
  6. Aaron Vegh

    Ryan Davis Guest

    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.


    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?
     
    Ryan Davis, Oct 7, 2009
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Scott Lander

    Re: unexpected results

    Scott Lander, Jul 7, 2003, in forum: Perl
    Replies:
    0
    Views:
    1,745
    Scott Lander
    Jul 7, 2003
  2. Dave
    Replies:
    1
    Views:
    343
    Leor Zolman
    Apr 8, 2004
  3. =?Utf-8?B?QXJ0?=

    Page inherting from .master - unexpected results

    =?Utf-8?B?QXJ0?=, May 26, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    364
    =?Utf-8?B?QXJ0?=
    May 26, 2006
  4. Replies:
    10
    Views:
    541
    Aaron Watters
    Jun 18, 2008
  5. Michael Davis

    Ruby 1.8 and Marshal.load/Marshal.dump

    Michael Davis, Oct 10, 2003, in forum: Ruby
    Replies:
    0
    Views:
    176
    Michael Davis
    Oct 10, 2003
Loading...

Share This Page