/
NikolayIvkin
/
tutorials1
Обзор
Документация
Войти
/
NikolayIvkin
/
tutorials1
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
json-modules/json-conversion/src/test/java/com/baeldung/preventexpressingintasfloat/PreventExpressingIntAsFloatUnitTest.java
37 строк
2 KB
Mo Helmy
This PR is related to the article BAEL-6102 (#15878)
15 фев 2024, 00:55
Не верифицирован
15 фев 2024, 00:55
83e7736
Код
Авторство
О чём код?
package com.baeldung.preventexpressingintasfloat; import com.google.gson.*; import com.google.gson.reflect.TypeToken; import org.junit.jupiter.api.Test; import java.util.ArrayList; import java.util.Hashtable; import static org.junit.jupiter.api.Assertions.assertEquals; 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 defaultGson = new Gson(); ArrayList<Hashtable<String, Object>> defaultResponses = defaultGson.fromJson(jsonString, new TypeToken<ArrayList<Hashtable<String, Object>>>() { }.getType()); 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()); } }