Skip to content

Commit

Permalink
addressed review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Blazer-007 committed Sep 18, 2024
1 parent e2d921a commit 8ccdba4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,13 @@ private <S> S readValue(JsonObject jsonObject, TypeToken<S> defaultTypeToken) th

public static <T> Gson getGson(Class<T> clazz) {
Gson gson = new GsonBuilder()
.setObjectToNumberStrategy(INTEGERNUMBERPOLICY.INTEGER_OR_LONG_OR_DOUBLE)
.setObjectToNumberStrategy(CustomToNumberPolicy.INTEGER_OR_LONG_OR_DOUBLE)
.registerTypeAdapterFactory(new GsonInterfaceAdapter(clazz))
.create();
return gson;
}

public enum INTEGERNUMBERPOLICY implements ToNumberStrategy {
public enum CustomToNumberPolicy implements ToNumberStrategy {
INTEGER_OR_LONG_OR_DOUBLE {
@Override
public Number readNumber(JsonReader in) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,51 @@ public void testObjectToIntegerDeserialize() {
String ser = gson.toJson(test);
Object deser = gson.fromJson(ser, Object.class);
Assert.assertNotEquals(test, deser);
Assert.assertTrue(deser instanceof Double);

Gson customGson = new GsonBuilder()
.setObjectToNumberStrategy(GsonInterfaceAdapter.INTEGERNUMBERPOLICY.INTEGER_OR_LONG_OR_DOUBLE)
.setObjectToNumberStrategy(GsonInterfaceAdapter.CustomToNumberPolicy.INTEGER_OR_LONG_OR_DOUBLE)
.create();

Object deser2 = customGson.fromJson(ser, Object.class);
Assert.assertEquals(test, deser2);
Assert.assertTrue(deser2 instanceof Integer);
}

@Test
public void testObjectToLongDeserialize() {
Gson gson = new GsonBuilder().create();
Long test = 1234567890123456789L;
String ser = gson.toJson(test);
Object deser = gson.fromJson(ser, Object.class);
Assert.assertNotEquals(test, deser);
Assert.assertTrue(deser instanceof Double);

Gson customGson = new GsonBuilder()
.setObjectToNumberStrategy(GsonInterfaceAdapter.CustomToNumberPolicy.INTEGER_OR_LONG_OR_DOUBLE)
.create();

Object deser2 = customGson.fromJson(ser, Object.class);
Assert.assertEquals(test, deser2);
Assert.assertTrue(deser2 instanceof Long);
}

@Test
public void testObjectToDoubleDeserialize() {
Gson gson = new GsonBuilder().create();
Double test = 5.0;
String ser = gson.toJson(test);
Object deser = gson.fromJson(ser, Object.class);
Assert.assertEquals(test, deser);
Assert.assertTrue(deser instanceof Double);

Gson customGson = new GsonBuilder()
.setObjectToNumberStrategy(GsonInterfaceAdapter.CustomToNumberPolicy.INTEGER_OR_LONG_OR_DOUBLE)
.create();

Object deser2 = customGson.fromJson(ser, Object.class);
Assert.assertEquals(test, deser2);
Assert.assertTrue(deser2 instanceof Double);
}

}

0 comments on commit 8ccdba4

Please sign in to comment.