Search in sources :

Example 1 with ISoliniaNPCMerchantEntryTypeAdapterFactory

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

the class JsonNPCMerchantRepository method commit.

@Override
public void commit() {
    // TODO Auto-generated method stub
    GsonBuilder gsonbuilder = new GsonBuilder();
    // gsonbuilder.setPrettyPrinting();
    gsonbuilder.registerTypeAdapterFactory(new ISoliniaNPCMerchantEntryTypeAdapterFactory(SoliniaNPCMerchantEntry.class));
    Gson gson = gsonbuilder.create();
    String jsonOutput = gson.toJson(npcmerchants.values(), new TypeToken<List<SoliniaNPCMerchant>>() {
    }.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 " + npcmerchants.size() + " npcmerchants");
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : ISoliniaNPCMerchant(com.solinia.solinia.Interfaces.ISoliniaNPCMerchant) SoliniaNPCMerchant(com.solinia.solinia.Models.SoliniaNPCMerchant) GsonBuilder(com.google.gson.GsonBuilder) ISoliniaNPCMerchantEntryTypeAdapterFactory(com.solinia.solinia.Factories.ISoliniaNPCMerchantEntryTypeAdapterFactory) TypeToken(com.google.gson.reflect.TypeToken) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) Gson(com.google.gson.Gson) SoliniaNPCMerchantEntry(com.solinia.solinia.Models.SoliniaNPCMerchantEntry) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) File(java.io.File)

Example 2 with ISoliniaNPCMerchantEntryTypeAdapterFactory

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

the class JsonNPCMerchantRepository method reload.

@Override
public void reload() {
    List<ISoliniaNPCMerchant> file = new ArrayList<ISoliniaNPCMerchant>();
    try {
        GsonBuilder gsonbuilder = new GsonBuilder();
        gsonbuilder.registerTypeAdapterFactory(new ISoliniaNPCMerchantEntryTypeAdapterFactory(SoliniaNPCMerchantEntry.class));
        Gson gson = gsonbuilder.create();
        BufferedReader br = new BufferedReader(new FileReader(filePath));
        file = gson.fromJson(br, new TypeToken<List<SoliniaNPCMerchant>>() {
        }.getType());
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    npcmerchants.clear();
    for (ISoliniaNPCMerchant i : file) {
        npcmerchants.put(i.getId(), i);
    }
    System.out.println("Reloaded " + npcmerchants.size() + " npcmerchants");
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) ISoliniaNPCMerchantEntryTypeAdapterFactory(com.solinia.solinia.Factories.ISoliniaNPCMerchantEntryTypeAdapterFactory) ISoliniaNPCMerchant(com.solinia.solinia.Interfaces.ISoliniaNPCMerchant) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) FileNotFoundException(java.io.FileNotFoundException) Gson(com.google.gson.Gson) SoliniaNPCMerchantEntry(com.solinia.solinia.Models.SoliniaNPCMerchantEntry) 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 ISoliniaNPCMerchantEntryTypeAdapterFactory (com.solinia.solinia.Factories.ISoliniaNPCMerchantEntryTypeAdapterFactory)2 ISoliniaNPCMerchant (com.solinia.solinia.Interfaces.ISoliniaNPCMerchant)2 SoliniaNPCMerchantEntry (com.solinia.solinia.Models.SoliniaNPCMerchantEntry)2 FileNotFoundException (java.io.FileNotFoundException)2 TypeToken (com.google.gson.reflect.TypeToken)1 SoliniaNPCMerchant (com.solinia.solinia.Models.SoliniaNPCMerchant)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