switched from config to Gson for better json support as the validation config will be editable in the application directly

This commit is contained in:
Andreas Billmann
2016-02-02 03:20:14 +01:00
parent d9405eb536
commit 10c2592510
12 changed files with 312 additions and 229 deletions

View File

@@ -26,8 +26,7 @@
package ninja.javafx.smartcsv.preferences;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import com.google.gson.GsonBuilder;
import ninja.javafx.smartcsv.FileReader;
import org.springframework.stereotype.Service;
import org.supercsv.prefs.CsvPreference;
@@ -35,6 +34,8 @@ import org.supercsv.quote.AlwaysQuoteMode;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import static ninja.javafx.smartcsv.preferences.QuoteModeHelper.getQuoteMode;
@@ -44,7 +45,7 @@ import static ninja.javafx.smartcsv.preferences.QuoteModeHelper.getQuoteMode;
@Service
public class PreferencesFileReader implements FileReader {
private Config config;
private Map config;
private CsvPreference csvPreference;
public PreferencesFileReader() {
@@ -55,15 +56,15 @@ public class PreferencesFileReader implements FileReader {
@Override
public void read(File filename) throws IOException {
config = ConfigFactory.parseFile(filename);
config = new GsonBuilder().create().fromJson(new java.io.FileReader(filename), HashMap.class);
if (config != null) {
char quoteChar = config.getString("quoteChar").charAt(0);
char delimiterChar = config.getString("delimiterChar").charAt(0);
String endOfLineSymbols = config.getString("endOfLineSymbols");
boolean surroundingSpacesNeedQuotes = config.getBoolean("surroundingSpacesNeedQuotes");
boolean ignoreEmptyLines = config.getBoolean("ignoreEmptyLines");
String quoteMode = config.getString("quoteMode");
char quoteChar = config.get("quoteChar").toString().charAt(0);
char delimiterChar = config.get("delimiterChar").toString().charAt(0);
String endOfLineSymbols = config.get("endOfLineSymbols").toString();
boolean surroundingSpacesNeedQuotes = (Boolean)config.get("surroundingSpacesNeedQuotes");
boolean ignoreEmptyLines = (Boolean)config.get("ignoreEmptyLines");
String quoteMode = config.get("quoteMode").toString();
csvPreference = new CsvPreference.Builder(quoteChar, delimiterChar, endOfLineSymbols)
.useQuoteMode(getQuoteMode(quoteMode))

View File

@@ -26,9 +26,8 @@
package ninja.javafx.smartcsv.preferences;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import com.typesafe.config.ConfigRenderOptions;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import ninja.javafx.smartcsv.FileWriter;
import org.springframework.stereotype.Service;
import org.supercsv.prefs.CsvPreference;
@@ -60,8 +59,8 @@ public class PreferencesFileWriter implements FileWriter {
preferences.put("ignoreEmptyLines", csvPreference.isIgnoreEmptyLines());
preferences.put("quoteMode", QuoteModeHelper.getQuoteModeName(csvPreference.getQuoteMode()));
Config config = ConfigFactory.parseMap(preferences);
Files.write(file.toPath(), config.root().render(ConfigRenderOptions.concise()).getBytes());
Gson gson = new GsonBuilder().setPrettyPrinting().create();
Files.write(file.toPath(), gson.toJson(preferences).getBytes());
}
}