YAML Parsing, ignoring objects?

T

Travis Smith

I have a class which when YAMLized produces...

--- !ruby/object:player
name: dude
password: dude
session: !ruby/object:TCPSocket {}
thread: !ruby/object:Thread {}

I'd like to figure out a way for it to just ignore the Thread and/or
TCPSocket type. It won't choke on the socket, but it will loading up
the Thread. Can I add a domain type that will just produce nil not
matter what happens?

irb(main):019:0> YAML::add_domain_type("legomaster.net,2005-03",
/Thread/) { | type, val | nil }
=> nil
irb(main):020:0> temp = YAML.load_file('data/players/dude.o')
TypeError: allocator undefined for Thread
from /usr/local/lib/ruby/1.8/yaml.rb:347:in `allocate'
from /usr/local/lib/ruby/1.8/yaml.rb:347:in `object_maker'
from /usr/local/lib/ruby/1.8/yaml/rubytypes.rb:36
from /usr/local/lib/ruby/1.8/yaml/rubytypes.rb:34:in `call'
from (irb):20:in `transfer'
from /usr/local/lib/ruby/1.8/yaml.rb:119:in `load'
from /usr/local/lib/ruby/1.8/yaml.rb:119:in `load'
from /usr/local/lib/ruby/1.8/yaml.rb:130:in `load_file'
from /usr/local/lib/ruby/1.8/yaml.rb:129:in `open'
from /usr/local/lib/ruby/1.8/yaml.rb:129:in `load_file'
from (irb):20
irb(main):021:0>

Thanks, it would much easier if I didn't have to end up writing my own parser.

~Travis Smith
 
F

Florian Gross

Travis said:
I have a class which when YAMLized produces...

--- !ruby/object:player
name: dude
password: dude
session: !ruby/object:TCPSocket {}
thread: !ruby/object:Thread {}

I'd like to figure out a way for it to just ignore the Thread and/or
TCPSocket type. It won't choke on the socket, but it will loading up
the Thread. Can I add a domain type that will just produce nil not
matter what happens?

Perhaps you can just redefine Player#to_yaml to exclude those?
 
T

Travis Smith

That did it for now. We'll see if I end up screwing things up in the
future because I don't to_yaml something right.

Thanks
 
W

why the lucky stiff

Travis said:
I have a class which when YAMLized produces...

--- !ruby/object:player
name: dude
password: dude
session: !ruby/object:TCPSocket {}
thread: !ruby/object:Thread {}

I'd like to figure out a way for it to just ignore the Thread and/or
TCPSocket type. It won't choke on the socket, but it will loading up
the Thread. Can I add a domain type that will just produce nil not
matter what happens?
Hey, Travis. You can define to_yaml_properties for any object, which is
simply an method which returns an array of properties you want in your
YAML doc, in the order you like.

class Player
def to_yaml_properties; ['@name', '@password']; end
end

You can read more in the docs[1]. See the section "Type Families".

_why

[1] http://yaml4r.sf.net/doc/
 
T

Travis Smith

I missed that last part on that page and it turns out that's exactly
what I want. Thanks for pointing that out. Minimal maintenance with
leaving most of the work to YAML parser.

Travis said:
I have a class which when YAMLized produces...

--- !ruby/object:player
name: dude
password: dude
session: !ruby/object:TCPSocket {}
thread: !ruby/object:Thread {}

I'd like to figure out a way for it to just ignore the Thread and/or
TCPSocket type. It won't choke on the socket, but it will loading up
the Thread. Can I add a domain type that will just produce nil not
matter what happens?
Hey, Travis. You can define to_yaml_properties for any object, which is
simply an method which returns an array of properties you want in your
YAML doc, in the order you like.

class Player
def to_yaml_properties; ['@name', '@password']; end
end

You can read more in the docs[1]. See the section "Type Families".

_why

[1] http://yaml4r.sf.net/doc/
 

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,774
Messages
2,569,598
Members
45,158
Latest member
Vinay_Kumar Nevatia
Top