Determining if a String is marshalled

D

Daniel Berger

Hi all,

I was just wondering if there's a way to determine if a string has been
marshalled without visual inspection. Something along the lines of
"string.marshalled?" or "string.kind_of?(MarshaledString)". Is there a
regex I could use?

This had me thinking that it would be nice if Marshal.dump returned a
MarshaledString rather than just a String.

The scenario I'm hitting is a client/server setup where the server may
return a marshaled string or a plain string, depending on the plugin it
calls. At the moment I'm resorting to this, which has potential problems:

r = client.gets
begin
rv = Marshal.load(r)
rescue TypeError
p r
rescue ArgumentError => e
p "R: #{r}"
else
puts rv
end

What do you think?

Regards,

Dan
 
Y

Yukihiro Matsumoto

Hi,

In message "Determining if a String is marshalled"

|I was just wondering if there's a way to determine if a string has been
|marshalled without visual inspection. Something along the lines of
|"string.marshalled?" or "string.kind_of?(MarshaledString)". Is there a
|regex I could use?

Marshal string have its format version embedded at the top 2 bytes (no
magic though). So that

string[0,2] == Marshal.dump("")[0,2]

should work for most of the cases.

matz.
 
B

Brian Candler

I was just wondering if there's a way to determine if a string has been
marshalled without visual inspection. Something along the lines of
"string.marshalled?" or "string.kind_of?(MarshaledString)". Is there a
regex I could use?

Check for the first two bytes, which are the major/minor version number.
Rather than hard-code it, you could do

marshall_sig = Marshal.dump("")[0..1]
marshall_check = /\A#{marshall_sig}/

irb(main):004:0> "abc" =~ marshall_check
=> nil
irb(main):005:0> Marshal.dump(123) =~ marshall_check
=> 0

Of course, the string might actually start with those bytes, but they are
low ("\004\006" on my machine) so unlikely to occur in a normal string.

Regards,

Brian.
 
D

Daniel Berger

Brian said:
I was just wondering if there's a way to determine if a string has been
marshalled without visual inspection. Something along the lines of
"string.marshalled?" or "string.kind_of?(MarshaledString)". Is there a
regex I could use?


Check for the first two bytes, which are the major/minor version number.
Rather than hard-code it, you could do

marshall_sig = Marshal.dump("")[0..1]
marshall_check = /\A#{marshall_sig}/

irb(main):004:0> "abc" =~ marshall_check
=> nil
irb(main):005:0> Marshal.dump(123) =~ marshall_check
=> 0

Of course, the string might actually start with those bytes, but they are
low ("\004\006" on my machine) so unlikely to occur in a normal string.

Regards,

Brian.

Thanks Brian and Matz for the info. My only concern might be that a
pack'd string would inadvertantly pass this test. I guess that's what
begin/rescue is for, though. :)

Regards,

Dan
 
H

Hal E. Fulton

----- Original Message -----
From: "Daniel Berger" <[email protected]>
To: "ruby-talk ML" <[email protected]>
Sent: Tuesday, July 15, 2003 11:38 AM
Subject: Re: Determining if a String is marshalled

Thanks Brian and Matz for the info. My only concern might be that a
pack'd string would inadvertantly pass this test. I guess that's what
begin/rescue is for, though. :)

Hmm, assuming a packed string can have an arbitrary value,
this could easily happen (probability 1/65536).

Here's a silly idea. When you Marshal a
string, add a singleton to it (that does
nothing). Test for that later.

str.respond_to? :marshalled # it's a marshalled string

However, that only works for strings in
memory, of course. If you read it in from
a file... who knows?

Hal
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top