所以我尝试使用GSON通过toJson()函数将一个类转换为JSON文件,每次调用代码时都会出现一个1000多行的StackOverflowError。http://pastebin.com/dBhYUFva
下面是我正在执行gson.toJson()
的类:
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();
}
}
竞技场玩家. 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);}
}
我真的不知道从哪里开始,甚至在堆栈跟踪,谁有一些指针?
1条答案
按热度按时间x7yiwoj41#
我认为问题是
Match
有一个ArenaPlayer
字段,ArenaPlayer
有一个Match
字段,如果这两个示例相互引用,就会导致Gson由于其实现而进入无限循环。你必须解开你的类,你的示例,或者使用一个不同的库,或者从序列化中排除某些字段--参见Gson文档。
顺便说一句,1000是默认的调用栈深度--如果你在栈中看到这么多行,你可能遇到了无限递归。