gpt4 book ai didi

java - 从 GSON 到 Json 的大规模 StackOverflowError

转载 作者:行者123 更新时间:2023-12-01 09:33:23 25 4
gpt4 key购买 nike

所以我尝试使用 GSON 通过 toJson() 函数将类转换为 JSON 文件,每次调用代码时都会出现 1000 多行的 StackOverflowError。这是错误:http://pastebin.com/dBhYUFva

这是我正在执行 gson.toJson() 的类(class):

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import mc.kwit.arena.players.ArenaPlayer;
import mc.kwit.arena.util.PlayerUtil;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;

import java.util.ArrayList;
import java.util.List;

/**
* Created by Zach on 8/3/16.
*/
public class Match {

List<ArenaPlayer> arenaPlayers = new ArrayList<ArenaPlayer>();
String[] uuidArray = null;
Player winner;
String matchId = "123456";
int killLimit = 5;
int timeLimit = -1;


public Match() {

}

public Match(List<ArenaPlayer> arenaPlayerList, String id, int seconds, int kills) {
this.arenaPlayers = arenaPlayerList;
this.matchId = id;
this.timeLimit = seconds;
this.killLimit = kills;
}

public Match(String[] players, String id, int seconds, int kills) {
this.uuidArray = players;
this.matchId = id;
this.timeLimit = seconds;
this.killLimit = kills;
}

public void start() {

//Create our ArenaPlayer objects
this.arenaPlayers = PlayerUtil.uuidToArenaPlayerList(uuidArray);

Bukkit.getLogger().info("Match \" " + this.matchId + "\" has been started!");
//Whitelist all participants
for(ArenaPlayer p : arenaPlayers) {
KwitArena.pc.addArenaPlayer(p.getPlayer());
p.getPlayer().setWhitelisted(true);
Bukkit.getLogger().info(p.getName() + " has been whitelisted!");
p.setMatch(this);
}
}

public void finish() throws Exception {

//Remove all players from whitelist and game
for(ArenaPlayer p : arenaPlayers) {
p.getPlayer().setWhitelisted(false);

if(p.isWinner()){
p.kick(ChatColor.GREEN + "You have won the game!");
this.winner = p.getPlayer();
} else {
p.kick(ChatColor.RED + winner.getName() + "has won the game :(");
}
}

Gson gson = new GsonBuilder().setPrettyPrinting().create();
String stats = gson.toJson(this);
KwitArena.postStats(stats);

//Remove this arena instance from the plugin
KwitArena.match = null;
}

public String getId(){
return(this.matchId);
}

public void setWinner(ArenaPlayer p) {
this.winner = p.getPlayer();
}
}

ArenaPlayer.class:

import mc.kwit.arena.Match;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;

/**
* Created by Zach on 7/31/16.
*/
public class ArenaPlayer {

ArenaPlayer ap;
Player player;
OfflinePlayer offlinePlayer;
int kills = 0;
int deaths = 0;
boolean isInGame = false;
Match match = null;
boolean isWinner = false;

public ArenaPlayer(Player player){
this.player = player;
}

public ArenaPlayer(Player player, boolean playing, int numKills, int numDeaths) {
this.player = player;
kills = numKills;
deaths = numDeaths;
isInGame = playing;
}


//Getters
public String getMatchId() { return(this.match.getId());}

public int getKills() { return(this.kills);}

public int getDeaths() { return(this.deaths);}

public Match getMatch() { return(this.match);}

public Player getPlayer() { return(this.player);}

public String getName() { return(player.getName());}

public OfflinePlayer getOfflinePlayer() { return(this.offlinePlayer);}

//Setters
public void setMatch(Match match) { this.match = match;}

public void setIsWinner(boolean b) { this.isWinner = b;}

public void setOfflinePlayer(OfflinePlayer off) { this.offlinePlayer = off; }


//Extras
public void addDeath() { this.deaths++;}

public void addKill() { this.kills++;}

public boolean isPlaying() { return(this.isInGame);}

public boolean isWinner() { return(this.isWinner);}

public void kick(String message) { player.kickPlayer(message);}


}

我不太确定从哪里开始堆栈跟踪,有人有一些指示吗?

最佳答案

我认为问题在于 Match 有一个 ArenaPlayer 字段,而 ArenaPlayer 有一个 Match 字段,如果两个实例相互引用,则会导致 Gson 由于其实现而进入无限循环。

您必须整理您的类、实例,或使用不同的库,或从序列化中排除某些字段 - 请参阅 Gson doc .

<小时/>

顺便说一句,1000 是默认的调用堆栈深度 - 如果您在堆栈中看到这么多行,则可能遇到了无限递归。

关于java - 从 GSON 到 Json 的大规模 StackOverflowError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39216806/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com