GSON返回StackOverflowError:null

时间:2019-06-20 19:44:10

标签: java gson

我正尝试使用Google的lib将HashMap转换为JSON字符串。 GSON。

我已经尝试使用Jackson的ObjectMapper,但是它也不起作用。我尝试将一些瞬态事件和一些我发现在其他主题上发现的东西都没有成功。

当前正在返回错误(未指定任何行):

//ERROR 
java.lang.StackOverflowError: null
    at sun.reflect.misc.ReflectUtil.checkPackageAccess(Unknown Source) ~[?:1.8.0_201]
    at sun.reflect.misc.ReflectUtil.checkPackageAccess(Unknown Source) ~[?:1.8.0_201]
    at sun.reflect.generics.reflectiveObjects.TypeVariableImpl.getGenericDeclaration(Unknown Source) ~[?:1.8.0_201]
    at com.google.gson.internal.$Gson$Types.declaringClassOf($Gson$Types.java:427) ~[$Gson$Types.class:?]
    at com.google.gson.internal.$Gson$Types.resolveTypeVariable($Gson$Types.java:397) ~[$Gson$Types.class:?]
    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:329) ~[$Gson$Types.class:?]
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:158) ~[ReflectiveTypeAdapterFactory.class:?]
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100) ~[ReflectiveTypeAdapterFactory.class:?]
    at com.google.gson.Gson.getAdapter(Gson.java:423) ~[Gson.class:?]
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:115) ~[ReflectiveTypeAdapterFactory.class:?]

// HashMap I need in JSON
public class Storage {

    static @Getter @Setter HashMap<World, HashMap<Chunk, List<Particle>>> storage = new HashMap<>();

}
// Class to load/save the data file
public class StorageManager {

    public static File folder, config;
    private static final Gson GSON = new Gson();

    public static void setup(File _folder){
        folder = _folder;
    }

    public static void load() throws IOException {
        if(!folder.exists())
            folder.mkdir();
        config = new File(folder, "particles.json");
        if(!config.exists()) {
            config.createNewFile();
            FileWriter writer = new FileWriter(config, false);
            writer.write(GSON.toJson(Storage.getStorage()));
            writer.close();
        }
        FileReader content = new FileReader(config);
        Type type = new TypeToken<HashMap<World, HashMap<Chunk, List<Particle>>>>(){}.getType();
        Storage.setStorage(GSON.fromJson(content, type));
    }

    public static void save() throws IOException {
        String json = GSON.toJson(Storage.getStorage());
        FileWriter writer = new FileWriter(config, false);
        writer.write(json);
        writer.close();
    }

}

我需要能够加载文件,获取JSON并将我当前为空的Hashmap替换为文件中的数据。我还需要使用新的Hashmap数据保存文件。

0 个答案:

没有答案