java.lang.IllegalArgumentException:class java.util.concurrent.atomic.AtomicInteger声明了多个名为serialVersionUID的JSON字段

时间:2017-04-30 15:41:35

标签: java json gson

我收到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;
}

}

也许有更好的方法来实现这一目标?

当我想将json文件加载到内存中时,会调用Points.getInstance()。load()。和Points.getInstance()。forceSave()再次将其保存到文件中。

编辑:GLocation class:

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;
}

}

编辑: 告诉我你们还有什么需要的东西!

1 个答案:

答案 0 :(得分:0)

问题是GSON没有识别库中类的Serial id,所以我创建了自己的包装器来实现Serializable和新的序列号。