Search in sources :

Example 1 with ISoliniaSpawnGroupTypeAdapterFactory

use of com.solinia.solinia.Factories.ISoliniaSpawnGroupTypeAdapterFactory in project solinia3-core by mixxit.

the class JsonSpawnGroupRepository method commit.

@Override
public void commit() {
    // TODO Auto-generated method stub
    GsonBuilder gsonbuilder = new GsonBuilder();
    // gsonbuilder.setPrettyPrinting();
    gsonbuilder.registerTypeAdapterFactory(new ISoliniaSpawnGroupTypeAdapterFactory(SoliniaSpawnGroup.class));
    Gson gson = gsonbuilder.create();
    String jsonOutput = gson.toJson(spawngroups.values(), new TypeToken<List<SoliniaSpawnGroup>>() {
    }.getType());
    try {
        File file = new File(filePath);
        if (!file.exists())
            file.createNewFile();
        FileOutputStream fileOut = new FileOutputStream(file);
        OutputStreamWriter outWriter = new OutputStreamWriter(fileOut);
        outWriter.append(jsonOutput);
        outWriter.close();
        fileOut.close();
        System.out.println("Commited " + spawngroups.size() + " spawngroups");
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : ISoliniaSpawnGroupTypeAdapterFactory(com.solinia.solinia.Factories.ISoliniaSpawnGroupTypeAdapterFactory) GsonBuilder(com.google.gson.GsonBuilder) TypeToken(com.google.gson.reflect.TypeToken) SoliniaSpawnGroup(com.solinia.solinia.Models.SoliniaSpawnGroup) ISoliniaSpawnGroup(com.solinia.solinia.Interfaces.ISoliniaSpawnGroup) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) Gson(com.google.gson.Gson) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) File(java.io.File)

Example 2 with ISoliniaSpawnGroupTypeAdapterFactory

use of com.solinia.solinia.Factories.ISoliniaSpawnGroupTypeAdapterFactory in project solinia3-core by mixxit.

the class JsonSpawnGroupRepository method reload.

@Override
public void reload() {
    List<ISoliniaSpawnGroup> file = new ArrayList<ISoliniaSpawnGroup>();
    try {
        GsonBuilder gsonbuilder = new GsonBuilder();
        gsonbuilder.registerTypeAdapterFactory(new ISoliniaSpawnGroupTypeAdapterFactory(SoliniaSpawnGroup.class));
        Gson gson = gsonbuilder.create();
        BufferedReader br = new BufferedReader(new FileReader(filePath));
        file = gson.fromJson(br, new TypeToken<List<SoliniaSpawnGroup>>() {
        }.getType());
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    spawngroups.clear();
    for (ISoliniaSpawnGroup i : file) {
        spawngroups.put(i.getId(), i);
    }
    System.out.println("Reloaded " + spawngroups.size() + " spawngroups");
}
Also used : ISoliniaSpawnGroupTypeAdapterFactory(com.solinia.solinia.Factories.ISoliniaSpawnGroupTypeAdapterFactory) GsonBuilder(com.google.gson.GsonBuilder) SoliniaSpawnGroup(com.solinia.solinia.Models.SoliniaSpawnGroup) ISoliniaSpawnGroup(com.solinia.solinia.Interfaces.ISoliniaSpawnGroup) ISoliniaSpawnGroup(com.solinia.solinia.Interfaces.ISoliniaSpawnGroup) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) FileNotFoundException(java.io.FileNotFoundException) Gson(com.google.gson.Gson) FileReader(java.io.FileReader) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

Gson (com.google.gson.Gson)2 GsonBuilder (com.google.gson.GsonBuilder)2 ISoliniaSpawnGroupTypeAdapterFactory (com.solinia.solinia.Factories.ISoliniaSpawnGroupTypeAdapterFactory)2 ISoliniaSpawnGroup (com.solinia.solinia.Interfaces.ISoliniaSpawnGroup)2 SoliniaSpawnGroup (com.solinia.solinia.Models.SoliniaSpawnGroup)2 FileNotFoundException (java.io.FileNotFoundException)2 TypeToken (com.google.gson.reflect.TypeToken)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1