Skip to content

Commit

Permalink
Make null serialization and lenient parsing in the JSONFileParser con…
Browse files Browse the repository at this point in the history
…figurable.
  • Loading branch information
jordin committed Jan 26, 2019
1 parent 80b15b1 commit 0a0d3e2
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 60 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>net.b0at</groupId>
<artifactId>torsion</artifactId>
<version>0.2.0-SNAPSHOT</version>
<version>0.3.0</version>

<dependencies>
<dependency>
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/net/b0at/torsion/FileStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

/**
* Created by Jordin on 8/4/2017.
* Jordin is still best hacker.
*/
public class FileStorage<T> implements Storage<T> {
private static File baseDirectory = new File(".");
private final File file;
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/net/b0at/torsion/Storage.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package net.b0at.torsion;

/**
* Created by Jordin on 8/4/2017.
* Jordin is still best hacker.
*/
public interface Storage<T> {
T load();

Expand Down
4 changes: 0 additions & 4 deletions src/main/java/net/b0at/torsion/parser/StorageParser.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package net.b0at.torsion.parser;

/**
* Created by Jordin on 8/4/2017.
* Jordin is still best hacker.
*/
public interface StorageParser<T> {
T load(Class<T> clazz);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
* Created by Jordin on 8/4/2017.
* Jordin is still best hacker.
*/
public class FileParserFactory {
private static final Map<String, Class<? extends FileStorageParser>> PARSER_MAP = ImmutableMap.<String, Class<? extends FileStorageParser>>builder()
.put("json", JSONFileParser.class)
Expand All @@ -29,6 +25,6 @@ public static <T> StorageParser from(File file, String extension) throws Instant

Class<? extends FileStorageParser> parserClass = PARSER_MAP.getOrDefault(extension, NullFileParser.class);

return parserClass.getDeclaredConstructor(File.class).<T>newInstance(file);
return parserClass.getDeclaredConstructor(File.class).newInstance(file);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@

import java.io.File;

/**
* Created by Jordin on 8/5/2017.
* Jordin is still best hacker.
*/
public abstract class FileStorageParser<T> implements StorageParser<T> {
private File file;

Expand Down
27 changes: 22 additions & 5 deletions src/main/java/net/b0at/torsion/parser/file/JSONFileParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@

import java.io.*;

/**
* Created by Jordin on 8/4/2017.
* Jordin is still best hacker.
*/
public class JSONFileParser<T> extends FileStorageParser<T> {
private static boolean serializeNulls;
private static boolean lenient;

public JSONFileParser(File file) {
super(file);
}
Expand Down Expand Up @@ -51,6 +50,24 @@ public void save(T object) {
}

private static Gson constructGsonBuilder() {
return new GsonBuilder().setPrettyPrinting().serializeNulls().create();
GsonBuilder builder = new GsonBuilder();
builder.setPrettyPrinting();
if (JSONFileParser.serializeNulls) {
builder.serializeNulls();
}

if (JSONFileParser.lenient) {
builder.setLenient();
}

return builder.create();
}

public static void setSerializeNulls(boolean serializeNulls) {
JSONFileParser.serializeNulls = serializeNulls;
}

public static void setLenient(boolean lenient) {
JSONFileParser.lenient = lenient;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

import java.io.File;

/**
* Created by Jordin on 8/4/2017.
* Jordin is still best hacker.
*/
public class NullFileParser<T> extends FileStorageParser<T> {
public NullFileParser(File file) {
super(file);
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/net/b0at/torsion/parser/file/YMLFileParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@

import java.io.*;

/**
* Created by Jordin on 8/4/2017.
* Jordin is still best hacker.
*/
public class YMLFileParser<T> extends FileStorageParser<T> {
public YMLFileParser(File file) {
super(file);
Expand Down
28 changes: 3 additions & 25 deletions src/test/java/net/b0at/torsion/test/StorageTest.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
package net.b0at.torsion.test;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.gson.annotations.SerializedName;
import net.b0at.torsion.FileStorage;
import net.b0at.torsion.Storage;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
* Created by Jordin on 8/4/2017.
Expand All @@ -18,34 +13,17 @@
public class StorageTest {

public static void main(String[] args) throws IOException {
FileStorage.setBaseDirectory(new File("./testing/test/123/dir"));
Storage<TestClass> storage = FileStorage.of(TestClass.class, "fuke.invalid-extension");

Storage<TestClass> storage = FileStorage.of(TestClass.class, "test.yml");

TestClass testClass = storage.load();
testClass.testDouble = 5;
testClass.testInt = 5;
storage.save(testClass);
}

public static class TestClass {
@SerializedName("test-int")
public int testInt = -1;

@SerializedName("test-double")
public double testDouble = 251.125156;

@SerializedName("test-string")
public String testString = "";

@SerializedName("test-list")
public List<String> testList = ImmutableList.of("Testing", "Arrays", "Of", "Strings", "To", "And", "From", "Configurations");

@SerializedName("map-test")
public Map<String, Integer> testMap = ImmutableMap.<String, Integer>builder()
.put("test", 5)
.put("testing", 2)
.put("11", 11)
.build();

public int testInt2 = -7;
}
}

0 comments on commit 0a0d3e2

Please sign in to comment.