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:
parent
b44c9d2c32
commit
83e7736d7d
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
@ -14,17 +14,24 @@ public class PreventExpressingIntAsFloatUnitTest {
|
|||||||
public String jsonString = "[{\"id\":4077395,\"field_id\":242566,\"body\":\"\"}, " +
|
public String jsonString = "[{\"id\":4077395,\"field_id\":242566,\"body\":\"\"}, " +
|
||||||
"{\"id\":4077398,\"field_id\":242569,\"body\":[[273019,0],[273020,1],[273021,0]]}, " +
|
"{\"id\":4077398,\"field_id\":242569,\"body\":[[273019,0],[273020,1],[273021,0]]}, " +
|
||||||
"{\"id\":4077399,\"field_id\":242570,\"body\":[[273022,0],[273023,1],[273024,0]]}]";
|
"{\"id\":4077399,\"field_id\":242570,\"body\":[[273022,0],[273023,1],[273024,0]]}]";
|
||||||
|
|
||||||
public String expectedOutput = "[{body=, field_id=242566, id=4077395}, " +
|
public String expectedOutput = "[{body=, field_id=242566, id=4077395}, " +
|
||||||
"{body=[[273019, 0], [273020, 1], [273021, 0]], field_id=242569, id=4077398}, " +
|
"{body=[[273019, 0], [273020, 1], [273021, 0]], field_id=242569, id=4077398}, " +
|
||||||
"{body=[[273022, 0], [273023, 1], [273024, 0]], field_id=242570, id=4077399}]";
|
"{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
|
@Test
|
||||||
public void givenJsonString_whenUsingsetObjectToNumberStrategyMethod_thenValidateOutput() {
|
public void givenJsonString_whenUsingSetObjectToNumberStrategyMethod_thenValidateOutput() {
|
||||||
Gson gson = new GsonBuilder().setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE).create();
|
Gson defaultGson = new Gson();
|
||||||
ArrayList<Hashtable<String, Object>> responses = gson.fromJson(jsonString, new TypeToken<ArrayList<Hashtable<String, Object>>>() {
|
ArrayList<Hashtable<String, Object>> defaultResponses = defaultGson.fromJson(jsonString, new TypeToken<ArrayList<Hashtable<String, Object>>>() {
|
||||||
}.getType());
|
}.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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user