Skip to content

Commit 15528c9

Browse files
committed
Bug fix: Date and DecimalFormat serializers put any specific name for the data.
VTypeToGsonTest passes data through Gson object to serialize VType.
1 parent 56ff852 commit 15528c9

3 files changed

Lines changed: 7 additions & 11 deletions

File tree

epics-vtype/vtype-gson/src/main/java/org/epics/vtype/gson/GsonDateHandler.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.google.gson.JsonElement;
2727
import com.google.gson.JsonObject;
2828
import com.google.gson.JsonParseException;
29+
import com.google.gson.JsonPrimitive;
2930
import com.google.gson.JsonSerializationContext;
3031
import com.google.gson.JsonSerializer;
3132

@@ -41,10 +42,7 @@
4142
public class GsonDateHandler implements JsonSerializer<Date>, JsonDeserializer<Date> {
4243
@Override
4344
public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
44-
JsonObject jsonObject = new JsonObject();
45-
jsonObject.addProperty("Date", src.toInstant().toEpochMilli());
46-
47-
return jsonObject;
45+
return new JsonPrimitive(src.toInstant().toEpochMilli());
4846
}
4947

5048
@Override

epics-vtype/vtype-gson/src/main/java/org/epics/vtype/gson/GsonDecimalFormatHandler.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.google.gson.JsonElement;
2727
import com.google.gson.JsonObject;
2828
import com.google.gson.JsonParseException;
29+
import com.google.gson.JsonPrimitive;
2930
import com.google.gson.JsonSerializationContext;
3031
import com.google.gson.JsonSerializer;
3132

@@ -41,10 +42,7 @@
4142
public class GsonDecimalFormatHandler implements JsonSerializer<DecimalFormat>, JsonDeserializer<DecimalFormat> {
4243
@Override
4344
public JsonElement serialize(DecimalFormat src, Type typeOfSrc, JsonSerializationContext context) {
44-
JsonObject jsonObject = new JsonObject();
45-
jsonObject.addProperty("DecimalFormat", src.toPattern());
46-
47-
return jsonObject;
45+
return new JsonPrimitive(src.toPattern());
4846
}
4947

5048
@Override

epics-vtype/vtype-gson/src/test/java/org/epics/vtype/gson/VTypeToGsonTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,14 @@ public class VTypeToGsonTest {
8181
* @param expectedJsonFileName the filename to compare
8282
*/
8383
public static void testSerialization(VType value, String expectedJsonFileName) {
84-
JsonElement json = VTypeToGson.toJson(value);
84+
JsonElement json = JsonParser.parseString(CustomGson.getGson().toJson(value, VType.class));
8585

8686
boolean success = false;
8787
try {
8888
JsonElement reference = loadJson(expectedJsonFileName + ".json");
8989
assertThat(json, equalTo(reference));
9090
success = true;
91-
} finally {
91+
} finally{
9292
File failedJsonFile = new File("src/test/resources/org/epics/vtype/gson/" + expectedJsonFileName + ".failed.json");
9393
if (!success) {
9494
saveErrorJson(json, failedJsonFile);
@@ -107,7 +107,7 @@ public static void testSerialization(VType value, String expectedJsonFileName) {
107107
* @param expected the object to compare to
108108
*/
109109
public static void testDeserialization(String jsonFileName, VType expected) {
110-
VType actual = VTypeToGson.toVType(loadJson(jsonFileName + ".json"));
110+
VType actual = CustomGson.getGson().fromJson(loadJson(jsonFileName + ".json"), VType.class);
111111
assertThat(actual, equalTo(expected));
112112
}
113113

0 commit comments

Comments
 (0)