diff --git a/parameter_tools/src/main/java/coppercore/parameter_tools/FieldNamingStrategy.java b/parameter_tools/src/main/java/coppercore/parameter_tools/FieldNamingStrategy.java new file mode 100644 index 0000000..07c18b8 --- /dev/null +++ b/parameter_tools/src/main/java/coppercore/parameter_tools/FieldNamingStrategy.java @@ -0,0 +1,3 @@ +package coppercore.parameter_tools; + +public class FieldNamingStrategy {} diff --git a/parameter_tools/src/main/java/coppercore/parameter_tools/JSONExcludeExclusionStrategy.java b/parameter_tools/src/main/java/coppercore/parameter_tools/JSONExcludeExclusionStrategy.java new file mode 100644 index 0000000..d5e9fdf --- /dev/null +++ b/parameter_tools/src/main/java/coppercore/parameter_tools/JSONExcludeExclusionStrategy.java @@ -0,0 +1,16 @@ +package coppercore.parameter_tools; + +import com.google.gson.ExclusionStrategy; +import com.google.gson.FieldAttributes; + +public class JSONExcludeExclusionStrategy implements ExclusionStrategy { + @Override + public boolean shouldSkipField(FieldAttributes field) { + return (field.getAnnotation(JSONExclude.class) != null); + } + + @Override + public boolean shouldSkipClass(Class clazz) { + return false; + } +} diff --git a/parameter_tools/src/main/java/coppercore/parameter_tools/JSONName.java b/parameter_tools/src/main/java/coppercore/parameter_tools/JSONName.java new file mode 100644 index 0000000..95238fc --- /dev/null +++ b/parameter_tools/src/main/java/coppercore/parameter_tools/JSONName.java @@ -0,0 +1,12 @@ +package coppercore.parameter_tools; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface JSONName { + String value() default "No name given"; +} diff --git a/parameter_tools/src/main/java/coppercore/parameter_tools/JSONNameNamingStrategy.java b/parameter_tools/src/main/java/coppercore/parameter_tools/JSONNameNamingStrategy.java new file mode 100644 index 0000000..e2c9468 --- /dev/null +++ b/parameter_tools/src/main/java/coppercore/parameter_tools/JSONNameNamingStrategy.java @@ -0,0 +1,25 @@ +package coppercore.parameter_tools; + +import com.google.gson.FieldNamingPolicy; +import com.google.gson.FieldNamingStrategy; +import java.lang.reflect.Field; + +public class JSONNameNamingStrategy implements FieldNamingStrategy { + + private FieldNamingPolicy policy = FieldNamingPolicy.IDENTITY; + + public JSONNameNamingStrategy() {} + + public JSONNameNamingStrategy(FieldNamingPolicy policy) { + this.policy = policy; + } + + @Override + public String translateName(Field field) { + JSONName annotation = field.getAnnotation(JSONName.class); + if (annotation != null) { + return annotation.value(); + } + return policy.translateName(field); + } +} diff --git a/parameter_tools/src/main/java/coppercore/parameter_tools/JSONSync.java b/parameter_tools/src/main/java/coppercore/parameter_tools/JSONSync.java index 7dbb70b..d8c4d8d 100644 --- a/parameter_tools/src/main/java/coppercore/parameter_tools/JSONSync.java +++ b/parameter_tools/src/main/java/coppercore/parameter_tools/JSONSync.java @@ -1,8 +1,8 @@ package coppercore.parameter_tools; import com.google.gson.ExclusionStrategy; -import com.google.gson.FieldAttributes; import com.google.gson.FieldNamingPolicy; +import com.google.gson.FieldNamingStrategy; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.LongSerializationPolicy; @@ -63,28 +63,17 @@ public void setFile(String newFilePath) { } private Gson generateGson() { - ExclusionStrategy strategy = - new ExclusionStrategy() { - @Override - public boolean shouldSkipField(FieldAttributes field) { - return (field.getAnnotation(JSONExclude.class) != null); - } - - @Override - public boolean shouldSkipClass(Class clazz) { - return false; - } - }; - + ExclusionStrategy jsonExcludeStrategy = new JSONExcludeExclusionStrategy(); + FieldNamingStrategy jsonNameStrategy = new JSONNameNamingStrategy(this.config.namingPolicy); GsonBuilder builder = new GsonBuilder(); if (this.config.serializeNulls) builder.serializeNulls(); if (this.config.prettyPrinting) builder.setPrettyPrinting(); if (this.config.excludeFieldsWithoutExposeAnnotation) builder.excludeFieldsWithoutExposeAnnotation(); - builder.setFieldNamingPolicy(this.config.namingPolicy) + builder.setFieldNamingStrategy(jsonNameStrategy) .setLongSerializationPolicy(this.config.longSerializationPolicy) - .addDeserializationExclusionStrategy(strategy) - .addSerializationExclusionStrategy(strategy); + .addDeserializationExclusionStrategy(jsonExcludeStrategy) + .addSerializationExclusionStrategy(jsonExcludeStrategy); return builder.create(); } diff --git a/parameter_tools/src/test/java/ExampleJsonSyncClass.java b/parameter_tools/src/test/java/coppercore/parameter_tools/test/ExampleJsonSyncClass.java similarity index 89% rename from parameter_tools/src/test/java/ExampleJsonSyncClass.java rename to parameter_tools/src/test/java/coppercore/parameter_tools/test/ExampleJsonSyncClass.java index f7f4655..05a4cdf 100644 --- a/parameter_tools/src/test/java/ExampleJsonSyncClass.java +++ b/parameter_tools/src/test/java/coppercore/parameter_tools/test/ExampleJsonSyncClass.java @@ -1,5 +1,6 @@ package coppercore.parameter_tools.test; +import coppercore.parameter_tools.JSONName; import coppercore.parameter_tools.JSONSync; public class ExampleJsonSyncClass { @@ -12,7 +13,10 @@ public class ExampleJsonSyncClass { public final String testText = ""; public final Double testDouble = 0.0; - public Integer testInt = 0; + + @JSONName("testInt") + public Integer testingIntField = 0; + public final BasicMotorDataHolder motorData = null; public class BasicMotorDataHolder { @@ -34,7 +38,7 @@ public String toString() { return "testText: " + testText + "\ntestInt: " - + testInt + + testingIntField + "\ntestDouble: " + testDouble + "\nmotorData: " diff --git a/parameter_tools/src/test/java/JSONSyncTests.java b/parameter_tools/src/test/java/coppercore/parameter_tools/test/JSONSyncTests.java similarity index 92% rename from parameter_tools/src/test/java/JSONSyncTests.java rename to parameter_tools/src/test/java/coppercore/parameter_tools/test/JSONSyncTests.java index fb328a2..14ff77e 100644 --- a/parameter_tools/src/test/java/JSONSyncTests.java +++ b/parameter_tools/src/test/java/coppercore/parameter_tools/test/JSONSyncTests.java @@ -27,7 +27,7 @@ public void JsonSyncLoadDataTest() { ExampleJsonSyncClass instance = ExampleJsonSyncClass.synced.getObject(); Assertions.assertEquals(10.0, instance.testDouble); - Assertions.assertEquals(2, instance.testInt); + Assertions.assertEquals(2, instance.testingIntField); Assertions.assertNull(instance.motorData); } @@ -38,7 +38,7 @@ public void JsonSyncSetFileTest() { ExampleJsonSyncClass instance = ExampleJsonSyncClass.synced.getObject(); Assertions.assertEquals(10.0, instance.testDouble); - Assertions.assertEquals(2, instance.testInt); + Assertions.assertEquals(2, instance.testingIntField); Assertions.assertNotNull(instance.motorData); Assertions.assertEquals(-12.3, instance.motorData.minVoltage); Assertions.assertEquals(16.4, instance.motorData.maxVoltage); @@ -49,7 +49,7 @@ public void JsonSyncSetFileTest() { public void JsonSyncSaveFileTest() { ExampleJsonSyncClass.synced.loadData(); ExampleJsonSyncClass instance = ExampleJsonSyncClass.synced.getObject(); - instance.testInt = 10; + instance.testingIntField = 10; ExampleJsonSyncClass.synced.setFile(RESOURCE_DIRECTORY + "/SaveFileTest.json"); ExampleJsonSyncClass.synced.saveData(); }