【发布时间】:2017-04-30 15:41:34
【问题描述】:
我收到 gson 错误:java.lang.IllegalArgumentException: class java.util.concurrent.atomic.AtomicInteger 声明了多个名为 serialVersionUID 的 JSON 字段
这是我的课程:
public abstract class Points {
private static Points instance = getBPointsImpl();
public static Points getInstance() {
return instance;
}
private static Points getBPointsImpl() {
return new JSONPoints();
}
public abstract void clean();
public abstract void forceSave();
public abstract void forceSave(boolean sync);
public abstract GLocation getSpawn();
public abstract void setSpawn(GLocation loc);
public abstract GLocation getPoint(String id);
public abstract void addPoint(String id, GLocation loc);
public abstract void removePoint(String id);
public abstract void load();
}
类扩展点
public abstract class MemoryPoints extends Points {
public GLocation spawn;
public Map<String, GLocation> points = new ConcurrentSkipListMap<String, GLocation>();
@Override
public void clean() {
}
@Override
public GLocation getSpawn() {
return spawn;
}
@Override
public void setSpawn(GLocation loc) {
this.spawn = loc;
}
@Override
public GLocation getPoint(String id) {
return points.get(id);
}
@Override
public void addPoint(String id, GLocation loc) {
points.put(id, loc);
}
@Override
public void removePoint(String id) {
points.remove(id);
}
@Override
public abstract void forceSave();
@Override
public abstract void load();
}
类扩展 MemoryPoints:
public class JSONPoints extends MemoryPoints {
// Info on how to persist
private Gson gson;
private File file;
public JSONPoints() {
file = new File("directory", "points.json");
gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().excludeFieldsWithModifiers(Modifier.TRANSIENT, Modifier.VOLATILE).create();
}
public Gson getGson() {
return gson;
}
public void setGson(Gson gson) {
this.gson = gson;
}
public void forceSave() {
forceSave(true);
}
public void forceSave(boolean sync) {
saveCore(file, this, sync);
}
private boolean saveCore(File target, MemoryPoints data, boolean sync) {
return DiscUtil.writeCatch(target, this.gson.toJson(data), sync);
}
public void load() {
MemoryPoints memoryPoints = this.loadCore();
if (memoryPoints == null) {
return;
}
this.points.clear();
this.points.putAll(memoryPoints.points);
}
private MemoryPoints loadCore() {
if (!this.file.exists()) {
return this;
}
String content = DiscUtil.readCatch(this.file);
if (content == null) {
return null;
}
MemoryPoints data = this.gson.fromJson(
content,
new TypeToken<MemoryPoints>() {
}.getType());
saveCore(this.file, data, true); // Update the flatfile
return data;
}
}
也许有更好的方法来实现这一点?
Points.getInstance().load() 在我想将 json 文件加载到内存时被调用。和 Points.getInstance().forceSave() 再次保存到文件时。
编辑:GLocation 类:
public class GLocation implements Serializable {
private transient static final long serialVersionUID = -6049901271320963314L;
private transient Location location = null;
private String worldName;
private double x;
private double y;
private double z;
private float pitch;
private float yaw;
public GLocation(Location loc) {
setLocation(loc);
}
public GLocation(String worldName, double x, double y, double z, float yaw, float pitch) {
this.worldName = worldName;
this.x = x;
this.y = y;
this.z = z;
this.yaw = yaw;
this.pitch = pitch;
}
public GLocation(String worldName, double x, double y, double z) {
this(worldName, x, y, z, 0, 0);
}
// This returns the actual Location
public final Location getLocation() {
// Make sure Location is initialized before returning it
initLocation();
return location;
}
// Change the Location
public void setLocation(Location loc) {
this.location = loc;
this.worldName = loc.getWorld().getName();
this.x = loc.getX();
this.y = loc.getY();
this.z = loc.getZ();
this.yaw = loc.getYaw();
this.pitch = loc.getPitch();
}
// This initializes the Location
private void initLocation() {
// if location is already initialized, simply return
if (location != null) {
return;
}
// get World; hopefully it's initialized at this point
World world = Bukkit.getWorld(worldName);
if (world == null) {
return;
}
// store the Location for future calls, and pass it on
location = new Location(world, x, y, z, yaw, pitch);
}
public String getWorldName() {
return worldName;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public double getZ() {
return z;
}
public double getPitch() {
return pitch;
}
public double getYaw() {
return yaw;
}
}
编辑: 如果你们还需要什么,请告诉我!
【问题讨论】:
-
我想知道为什么
AtomicInteger有问题,而你的代码中没有一个AtomicInteger。您是否向我们展示了很多不相关的代码,并省略了相关代码,即实际使用AtomicInteger的代码?请阅读:How to create a Minimal, Complete, and Verifiable example -
我没有使用任何 AtomicInteger。应显示所有相关代码。也许我的 GLocation 课程是相关的?查看编辑后的帖子。
-
按照那篇文章的建议去做,给我留下了 StackOverFlowException。 @安德烈亚斯