Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Naming of Serialized Fields #61

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package coppercore.parameter_tools;

public class FieldNamingStrategy {}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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";
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package coppercore.parameter_tools.test;

import coppercore.parameter_tools.JSONName;
import coppercore.parameter_tools.JSONSync;

public class ExampleJsonSyncClass {
Expand All @@ -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 {
Expand All @@ -34,7 +38,7 @@ public String toString() {
return "testText: "
+ testText
+ "\ntestInt: "
+ testInt
+ testingIntField
+ "\ntestDouble: "
+ testDouble
+ "\nmotorData: "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"testText": "Hello World",
"testcText": "Hello World",
avidraccoon marked this conversation as resolved.
Show resolved Hide resolved
"testDouble": 10.0,
"testInt": 2
}
Loading