尝试使用GSON将对象序列化为json - >得到巨大的递归错误

时间:2017-04-23 12:53:39

标签: java serialization gson

我的代码:

public class Faction implements Serializable {

private static Set<Faction> factions = new HashSet<>();

private transient static Set<UUID> factionOperator = new HashSet<>();

private transient static Map<UUID, Faction> onlineFPlayers = new HashMap<>();

private String announcement;

private String name;

private UUID leader;

private Map<UUID, Boolean> players = new HashMap<>();

private transient Set<UUID> factionChatPlayers = new HashSet<>();

private transient Set<UUID> invitedPlayers = new HashSet<>();

private Power power = new Power(this);

private Region region;

private double credit = 0;


public static void save() {
    Gson gson = new GsonBuilder().create();
    try {
        Writer writer = new FileWriter(Krene.get().getDataFolder() + "\\factions.json");
        gson.toJson(factions, writer);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

在课堂上有一些方法可以做其他事情。但基本上我想将这些序列化为一个json文件(序列化代码在方法save()中) - 但是我收到了很大的错误。

错误:

  

java.lang.StackOverflowError的           at sun.nio.cs.SingleByte.withResult(Unknown Source)〜[?:1.8.0_121]           在sun.nio.cs.SingleByte.access $ 000(未知来源)〜[?:1.8.0_121]           at sun.nio.cs.SingleByte $ Encoder.encodeArrayLoop(Unknown Source)〜[?:1.8.0_121]           at sun.nio.cs.SingleByte $ Encoder.encodeLoop(Unknown Source)〜[?:1.8.0_121]           at java.nio.charset.CharsetEncoder.encode(Unknown Source)〜[?:1.8.0_121]           at sun.nio.cs.StreamEncoder.implWrite(Unknown Source)〜[?:1.8.0_121]           at sun.nio.cs.StreamEncoder.write(Unknown Source)〜[?:1.8.0_121]           at sun.nio.cs.StreamEncoder.write(Unknown Source)〜[?:1.8.0_121]           at java.io.OutputStreamWriter.write(Unknown Source)〜[?:1.8.0_121]           在java.io.Writer.write(未知来源)〜[?:1.8.0_121]           在com.google.gson.stream.JsonWriter.string(JsonWriter.java:551)〜[?:?]           在com.google.gson.stream.JsonWriter.writeDeferredName(JsonWriter.java:402)   〜[:?]           在com.google.gson.stream.JsonWriter.beginObject(JsonWriter.java:307)   〜[:?]           在com.google.gson.internal.bind.MapTypeAdapterFactory $ Adapter.write(MapTypeAdapterFactory.java:205)   〜[:?]           在com.google.gson.internal.bind.MapTypeAdapterFactory $ Adapter.write(MapTypeAdapterFactory.java:145)   〜[:?]           在com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)   〜[:?]           在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ 1.write(ReflectiveTypeAdapterFactory.java:112)   〜[:?]           在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.write(ReflectiveTypeAdapterFactory.java:239)   〜[:?]           在com.google.gson.Gson $ FutureTypeAdapter.write(Gson.java:968)〜[?:?]           在com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)   〜[:?]           在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ 1.write(ReflectiveTypeAdapterFactory.java:112)   〜[:?]           在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.write(ReflectiveTypeAdapterFactory.java:239)   〜[:?]           在com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)   〜[:?]           在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ 1.write(ReflectiveTypeAdapterFactory.java:112)   〜[:?]           在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.write(ReflectiveTypeAdapterFactory.java:239)   〜[:?]           在com.google.gson.Gson $ FutureTypeAdapter.write(Gson.java:968)〜[?:?]           在com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)   〜[:?]           在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ 1.write(ReflectiveTypeAdapterFactory.java:112)   〜[:?]           在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.write(ReflectiveTypeAdapterFactory.java:239)   〜[:?]           在com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)   〜[:?]           在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ 1.write(ReflectiveTypeAdapterFactory.java:112)   〜[:?]           在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.write(ReflectiveTypeAdapterFactory.java:239)   〜[:?]           在com.google.gson.Gson $ FutureTypeAdapter.write(Gson.java:968)〜[?:?]           在com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)   〜[:?]           在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ 1.write(ReflectiveTypeAdapterFactory.java:112)   〜[:?]           在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.write(ReflectiveTypeAdapterFactory.java:239)   〜[:?]           在com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)   〜[:?]           在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ 1.write(ReflectiveTypeAdapterFactory.java:112)   〜[:?]           在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.write(ReflectiveTypeAdapterFactory.java:239)   〜[:?]           在com.google.gson.Gson $ FutureTypeAdapter.write(Gson.java:968)〜[?:?]           在com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)   〜[:?]           在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ 1.write(ReflectiveTypeAdapterFactory.java:112)   〜[:?]           在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.write(ReflectiveTypeAdapterFactory.java:239)   〜[:?]           在com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)   〜[:?]           在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ 1.write(ReflectiveTypeAdapterFactory.java:112)   〜[:?]           在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.write(ReflectiveTypeAdapterFactory.java:239)   〜[:?]           在com.google.gson.Gson $ FutureTypeAdapter.write(Gson.java:968)〜[?:?]           在com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)   〜[:?]           在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ 1.write(ReflectiveTypeAdapterFactory.java:112)   〜[:?]           在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.write(ReflectiveTypeAdapterFactory.java:239)   〜[:?]           在com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)   〜[:?]           在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ 1.write(ReflectiveTypeAdapterFactory.java:112)   〜[:?]           在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.write(ReflectiveTypeAdapterFactory.java:239)   〜[:?]           在com.google.gson.Gson $ FutureTypeAdapter.write(Gson.java:968)〜[?:?]           在com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)   〜[:?]           在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ 1.write(ReflectiveTypeAdapterFactory.java:112)   〜[:?]           在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.write(ReflectiveTypeAdapterFactory.java:239)   〜[:?]           在com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)   〜[:?]           在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ 1.write(ReflectiveTypeAdapterFactory.java:112)   〜[:?]           在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.write(ReflectiveTypeAdapterFactory.java:239)   〜[:?]           在com.google.gson.Gson $ FutureTypeAdapter.write(Gson.java:968)〜[?:?]

为什么我收到此错误?请记住,Region和Power这两个类都实现了Serializable(我认为这是必要的,如果我错了,请纠正我?)。

是UUID的序列化吗?我应该将它们序列化为字符串吗?

0 个答案:

没有答案