0

I am getting the gson error: java.lang.IllegalArgumentException: class java.util.concurrent.atomic.AtomicInteger declares multiple JSON fields named serialVersionUID

Here are my classes:

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();

}

Class extending Points

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();

}

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

}

Maybe there is a better way to accomplish this?

Points.getInstance().load() is called when i want to load the json file into memory. And Points.getInstance().forceSave() when saving it to the file again.

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

}

EDIT: Tell me if there's anything more u guys need!

ExpDev
  • 65
  • 1
  • 10
  • 3
    I wonder why it has a problem with `AtomicInteger`, when you don't have a single `AtomicInteger` in your code. Did you perhaps show us a lot of irrelevant code, and omitted that relevant code, i.e. the code that actually uses an `AtomicInteger`? Please read: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) – Andreas Apr 30 '17 at 15:46
  • 1
    Possible duplicate of [Object of class containing AtomicInteger's fails to be converted to JSON using GSON](http://stackoverflow.com/q/40243712/5221149) – Andreas Apr 30 '17 at 15:51
  • I am not using any AtomicInteger. All relevant code should be shown. Maybe my GLocation class is relevant? See edited post. – ExpDev Apr 30 '17 at 15:51
  • Doing what that post suggested left me with an StackOverFlowException. @Andreas – ExpDev Apr 30 '17 at 15:59

1 Answers1

0

Issue was with GSON not recognizing the Serial id for a class in the library, so I just created my own wrapper for it implementing Serializable and with a new serial id.

ExpDev
  • 65
  • 1
  • 10