反序列化嵌套对象结构时的 Ruby YAML 问题
我有 3 个具有以下关系的类:
战场 有一系列团队。
BattleTeam 有一组成员,以及对战场的引用。
Jaguar 是BattleTeam 的成员,并且有对其的引用。
如果我序列化/反序列化 Jaguar 直至 BattleTeam,就没有问题。序列化/反序列化 Battlefield 时会出现此问题。
序列化它会产生适当的结果,但反序列化它会将 BattleTeam 中的成员变成零。
让我告诉你:
>> j = Jaguar.new
=> #<Jaguar:0xa2221f8 @vitality=nil, @spirituality=nil, @log=nil, @name=nil>
#A battleteam might have many members:
>> t = BattleTeam.new
=> #<BattleTeam:0xa21fafc @members=[], @dead_members=[]>
#A battlefield might have many teams:
>> b = Battlefield.new
=> #<Battlefield:0xa2075d8 @teams=[]>
#Add a member to the team
>> t.add_member(j)
=> #<BattleTeam:0xa21fafc @members=[#<Jaguar:0xa2221f8 @vitality=nil, @spirituality=nil, @log=nil, @name=nil, @battle_team=#<BattleTeam:0xa21fafc ...>], dead_members[]
#Add a team to the battlefield
>> b.add_team(t)
=> #<Battlefield:0xa2075d8 @teams=[#<BattleTeam:0xa21fafc @members=[#<Jaguar:0xa2221f8 @vitality=nil, @spirituality=nil, @log=nil, @name=nil, @battle_team=#<BattleTeam:0xa21fafc ...>>], @dead_members=[], @battlefield=#<Battlefield:0xa2075d8 ...>>]>
#Serialize and deserialize the jaguar object (it works as expected):
>> YAML::load j.to_yaml
=> #<Jaguar:0xa1e3cdc @vitality=nil, @spirituality=nil, @log=nil, @name=nil, @battle_team=#<BattleTeam:0xa1e4204 @members=[#<Jaguar:0xa1e3cdc ...>], dead_members[], battlefield#<Battlefield:0xa1e4038 @teams=[#<BattleTeam:0xa1e4204 ...>]
#Serialize and deserialize the battle team object (it words as expected):
>> YAML::load t.to_yaml
=> #<BattleTeam:0xa1d0664 @members=[#<Jaguar:0xa1cfee4 @vitality=nil, @spirituality=nil, @log=nil, @name=nil, @battle_team=#<BattleTeam:0xa1d0664 ...>], dead_members[], battlefield#<Battlefield:0xa1d0470 @teams=[#<BattleTeam:0xa1d0664 ...>]
#Serialize and deserialize the battle team object
#(here the @members array is deserialized as nil, this is not expected!):
>> YAML::load b.to_yaml
=> #<Battlefield:0xa1c370c @teams=[#<BattleTeam:0xa1c33b0 @members=nil, @dead_members=[], @battlefield=#<Battlefield:0xa1c370c ...>]
>> YAML::load(b.to_yaml).teams[0].members
=> nil
#Here is the generated yaml, it looks right, the member is there:
>> puts b.to_yaml
--- &id002 !ruby/object:Battlefield
teams:
- &id001 !ruby/object:BattleTeam
battlefield: *id002
dead_members: []
members:
- !ruby/object:Jaguar
battle_team: *id001
log:
name:
spirituality:
vitality:
=> nil
有人知道这是怎么回事吗?
I have 3 classes that have the following relationship:
Battlefield have an array of teams.
BattleTeam have an array of members, and a reference to the Battlefield.
Jaguar is a member of a BattleTeam and has a reference to it.
If I serialize/deserialize Jaguar and up to BattleTeam, there is no problem. The problem happens when serializing/deserializing a Battlefield.
Serializing it yields the appropiate result, but deserializing it turns the members in BattleTeam to nil.
Let me show you:
>> j = Jaguar.new
=> #<Jaguar:0xa2221f8 @vitality=nil, @spirituality=nil, @log=nil, @name=nil>
#A battleteam might have many members:
>> t = BattleTeam.new
=> #<BattleTeam:0xa21fafc @members=[], @dead_members=[]>
#A battlefield might have many teams:
>> b = Battlefield.new
=> #<Battlefield:0xa2075d8 @teams=[]>
#Add a member to the team
>> t.add_member(j)
=> #<BattleTeam:0xa21fafc @members=[#<Jaguar:0xa2221f8 @vitality=nil, @spirituality=nil, @log=nil, @name=nil, @battle_team=#<BattleTeam:0xa21fafc ...>], dead_members[]
#Add a team to the battlefield
>> b.add_team(t)
=> #<Battlefield:0xa2075d8 @teams=[#<BattleTeam:0xa21fafc @members=[#<Jaguar:0xa2221f8 @vitality=nil, @spirituality=nil, @log=nil, @name=nil, @battle_team=#<BattleTeam:0xa21fafc ...>>], @dead_members=[], @battlefield=#<Battlefield:0xa2075d8 ...>>]>
#Serialize and deserialize the jaguar object (it works as expected):
>> YAML::load j.to_yaml
=> #<Jaguar:0xa1e3cdc @vitality=nil, @spirituality=nil, @log=nil, @name=nil, @battle_team=#<BattleTeam:0xa1e4204 @members=[#<Jaguar:0xa1e3cdc ...>], dead_members[], battlefield#<Battlefield:0xa1e4038 @teams=[#<BattleTeam:0xa1e4204 ...>]
#Serialize and deserialize the battle team object (it words as expected):
>> YAML::load t.to_yaml
=> #<BattleTeam:0xa1d0664 @members=[#<Jaguar:0xa1cfee4 @vitality=nil, @spirituality=nil, @log=nil, @name=nil, @battle_team=#<BattleTeam:0xa1d0664 ...>], dead_members[], battlefield#<Battlefield:0xa1d0470 @teams=[#<BattleTeam:0xa1d0664 ...>]
#Serialize and deserialize the battle team object
#(here the @members array is deserialized as nil, this is not expected!):
>> YAML::load b.to_yaml
=> #<Battlefield:0xa1c370c @teams=[#<BattleTeam:0xa1c33b0 @members=nil, @dead_members=[], @battlefield=#<Battlefield:0xa1c370c ...>]
>> YAML::load(b.to_yaml).teams[0].members
=> nil
#Here is the generated yaml, it looks right, the member is there:
>> puts b.to_yaml
--- &id002 !ruby/object:Battlefield
teams:
- &id001 !ruby/object:BattleTeam
battlefield: *id002
dead_members: []
members:
- !ruby/object:Jaguar
battle_team: *id001
log:
name:
spirituality:
vitality:
=> nil
Anyone knows what's wrong with this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我真的不太了解 YAML 或序列化/反序列化..
但是看:
puts b.to_yaml
--- &id002 !ruby/object:战场
团队:
- &id001 !ruby/object:BattleTeam
战场:*id002
dead_members: []
成员:
- !ruby/对象:捷豹
战斗团队:*id001
日志:
姓名:
灵性:
活力:
=> nil
为什么战场和战队看起来像这样:
而捷豹看起来像这样:
id 似乎丢失了......?
I really don't know much about YAML or serializing / deserializing..
But look :
puts b.to_yaml
--- &id002 !ruby/object:Battlefield
teams:
- &id001 !ruby/object:BattleTeam
battlefield: *id002
dead_members: []
members:
- !ruby/object:Jaguar
battle_team: *id001
log:
name:
spirituality:
vitality:
=> nil
How come battlefield and battleteam look like this:
And Jaguar looks like this:
The id seems to be missing... ?