This PR is related to the article BAEL-6102 (#15878)

* Delete json-modules/json-conversion/src/main/java/com/baeldung/preventexpressingintasfloat directory

* Update PreventExpressingIntAsFloatUnitTest.java
This commit is contained in:
Mo Helmy 2024-02-14 23:55:33 +02:00 committed by GitHub
parent b44c9d2c32
commit 83e7736d7d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 27 deletions

View File

@ -1,22 +0,0 @@
package com.baeldung.preventexpressingintasfloat;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Hashtable;
public class Main {
public static String draft = "[{\"id\":4077395,\"field_id\":242566,\"body\":\"\"}, " +
"{\"id\":4077398,\"field_id\":242569,\"body\":[[273019,0],[273020,1],[273021,0]]}, " +
"{\"id\":4077399,\"field_id\":242570,\"body\":[[273022,0],[273023,1],[273024,0]]}]";
public static void main(String[] args) {
ArrayList<Hashtable<String, Object>> responses;
Type ResponseList = new TypeToken<ArrayList<Hashtable<String, Object>>>() {
}.getType();
responses = new Gson().fromJson(draft, ResponseList);
System.out.println(responses);
}
}

View File

@ -14,17 +14,24 @@ public class PreventExpressingIntAsFloatUnitTest {
public String jsonString = "[{\"id\":4077395,\"field_id\":242566,\"body\":\"\"}, " +
"{\"id\":4077398,\"field_id\":242569,\"body\":[[273019,0],[273020,1],[273021,0]]}, " +
"{\"id\":4077399,\"field_id\":242570,\"body\":[[273022,0],[273023,1],[273024,0]]}]";
public String expectedOutput = "[{body=, field_id=242566, id=4077395}, " +
"{body=[[273019, 0], [273020, 1], [273021, 0]], field_id=242569, id=4077398}, " +
"{body=[[273022, 0], [273023, 1], [273024, 0]], field_id=242570, id=4077399}]";
public String defaultOutput = "[{body=, field_id=242566.0, id=4077395.0}, " +
"{body=[[273019.0, 0.0], [273020.0, 1.0], [273021.0, 0.0]], field_id=242569.0, id=4077398.0}, " +
"{body=[[273022.0, 0.0], [273023.0, 1.0], [273024.0, 0.0]], field_id=242570.0, id=4077399.0}]";
@Test
public void givenJsonString_whenUsingsetObjectToNumberStrategyMethod_thenValidateOutput() {
Gson gson = new GsonBuilder().setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE).create();
ArrayList<Hashtable<String, Object>> responses = gson.fromJson(jsonString, new TypeToken<ArrayList<Hashtable<String, Object>>>() {
public void givenJsonString_whenUsingSetObjectToNumberStrategyMethod_thenValidateOutput() {
Gson defaultGson = new Gson();
ArrayList<Hashtable<String, Object>> defaultResponses = defaultGson.fromJson(jsonString, new TypeToken<ArrayList<Hashtable<String, Object>>>() {
}.getType());
assertEquals(expectedOutput, responses.toString());
Gson customGson = new GsonBuilder().setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE).create();
ArrayList<Hashtable<String, Object>> customResponses = customGson.fromJson(jsonString, new TypeToken<ArrayList<Hashtable<String, Object>>>() {
}.getType());
assertEquals(defaultOutput, defaultResponses.toString());
assertEquals(expectedOutput, customResponses.toString());
}
}