【问题标题】:java.lang.IllegalArgumentException: class java.util.concurrent.atomic.AtomicInteger declares multiple JSON fields named serialVersionUIDjava.lang.IllegalArgumentException:类 java.util.concurrent.atomic.AtomicInteger 声明了多个名为 serialVersionUID 的 JSON 字段
【发布时间】: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;
}

}

编辑: 如果你们还需要什么,请告诉我!

【问题讨论】:

标签: java json gson


【解决方案1】:

问题在于 GSON 无法识别库中某个类的 Serial id,因此我刚刚创建了自己的包装器,用于实现 Serializable 并使用新的序列号。

【讨论】:

    猜你喜欢
    • 2017-12-11
    • 1970-01-01
    • 1970-01-01
    • 2014-09-09
    • 2019-08-23
    • 1970-01-01
    • 2021-02-13
    • 1970-01-01
    • 2013-05-13
    相关资源
    最近更新 更多