反序列化嵌套对象结构时的 Ruby YAML 问题

发布于 2024-08-09 02:40:16 字数 2765 浏览 2 评论 0原文

我有 3 个具有以下关系的类:

战场 有一系列团队。

BattleTeam 有一组成员,以及对战场的引用。

JaguarBattleTeam 的成员,并且有对其的引用。

如果我序列化/反序列化 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

软糖 2024-08-16 02:40:16

我真的不太了解 YAML 或序列化/反序列化..
但是看:

puts b.to_yaml
--- &id002 !ruby/object:战场
团队:
- &id001 !ruby/object:BattleTeam
战场:*id002
dead_members: []

成员:
- !ruby/对象:捷豹
战斗团队:*id001
日志:
姓名:
灵性:
活力:
=> nil

为什么战场和战队看起来像这样:

--- &id002 !ruby/object:Battlefield 
- &id001 !ruby/object:BattleTeam 

而捷豹看起来像这样:

  - !ruby/object:Jaguar 

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:

--- &id002 !ruby/object:Battlefield 
- &id001 !ruby/object:BattleTeam 

And Jaguar looks like this:

  - !ruby/object:Jaguar 

The id seems to be missing... ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文