Changes for BAEL-2176 (#5305)

* Evaluation Article - Spring web-flux

* Evaluation Article - Spring web-flux

* Evaluation Article - Spring web-flux

* Evaluation Article - Spring web-flux

* core-scala: initial commit

* adding core-scala to pom

* Revert "core-scala: initial commit"

This reverts commit d46873405a67addfaa69aa7855b37af9c4a3df14.

* BAEL-2176

* inserting new lines between given, when and then parts

* Formatted using formatter

* indentation changed further

* Suggested changes in coding
This commit is contained in:
Swapan Pramanick 2018-10-02 07:58:53 +05:30 committed by KevinGilmore
parent 5aa8a5c985
commit 59a5313d96
6 changed files with 276 additions and 69 deletions

View File

@ -0,0 +1,36 @@
package org.baeldung.gson.entities;
public class Employee {
private int id;
private String name;
private String address;
public Employee(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}

View File

@ -0,0 +1,66 @@
package org.baeldung.gson.serialization;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import org.baeldung.gson.entities.Employee;
import com.google.gson.*;
public class HashMapDeserializer implements JsonDeserializer<HashMap<String, Object>> {
@Override
public HashMap<String, Object> deserialize(JsonElement elem, Type type, JsonDeserializationContext context) throws JsonParseException {
HashMap<String, Object> map = new HashMap<>();
for (Map.Entry<String, JsonElement> entry : elem.getAsJsonObject().entrySet()) {
JsonElement jsonValue = entry.getValue();
Object value = null;
if (jsonValue.isJsonPrimitive()) {
value = toPrimitive(jsonValue.getAsJsonPrimitive(), context);
} else {
value = context.deserialize(jsonValue, Employee.class);
}
map.put(entry.getKey(), value);
}
return map;
}
private Object toPrimitive(JsonPrimitive jsonValue, JsonDeserializationContext context) {
if (jsonValue.isBoolean())
return jsonValue.getAsBoolean();
else if (jsonValue.isString())
return jsonValue.getAsString();
else {
BigDecimal bigDec = jsonValue.getAsBigDecimal();
Long l;
Integer i;
if ((i = toInteger(bigDec)) != null) {
return i;
} else if ((l = toLong(bigDec)) != null) {
return l;
} else {
return bigDec.doubleValue();
}
}
}
private Long toLong(BigDecimal val) {
try {
return val.toBigIntegerExact().longValue();
} catch (ArithmeticException e) {
return null;
}
}
private Integer toInteger(BigDecimal val) {
try {
return val.intValueExact();
} catch (ArithmeticException e) {
return null;
}
}
}

View File

@ -7,13 +7,13 @@
</encoder>
</appender>
<logger name="org.springframework" level="WARN" />
<logger name="org.springframework.transaction" level="WARN" />
<logger name="org.springframework" level="WARN"/>
<logger name="org.springframework.transaction" level="WARN"/>
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
<logger name="org.springframework.web.servlet.mvc" level="WARN"/>
<root level="INFO">
<appender-ref ref="STDOUT" />
<appender-ref ref="STDOUT"/>
</root>
</configuration>

View File

@ -0,0 +1,86 @@
package org.baeldung.gson.deserialization;
import java.lang.reflect.Type;
import java.util.HashMap;
import org.baeldung.gson.entities.Employee;
import org.baeldung.gson.serialization.HashMapDeserializer;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import com.google.gson.internal.LinkedTreeMap;
import com.google.gson.reflect.TypeToken;
public class HashMapDeserializationUnitTest {
private static final Logger logger = LoggerFactory.getLogger(HashMapDeserializationUnitTest.class);
@Test
public void whenUsingHashMapClass_thenShouldReturnMapWithDefaultClasses() {
String jsonString = "{'employee.name':'Bob','employee.salary':10000, 'employee.active':true, "
+ "'employee':{'id':10, 'name': 'Bob Willis', 'address':'London'}}";
Gson gson = new Gson();
HashMap map = gson.fromJson(jsonString, HashMap.class);
logger.info("The converted map: {}", map);
Assert.assertEquals(4, map.size());
Assert.assertEquals(Double.class, map.get("employee.salary").getClass());
Assert.assertEquals(LinkedTreeMap.class, map.get("employee").getClass());
}
@Test(expected = JsonSyntaxException.class)
public void whenUsingJsonStringWithDuplicateKey_thenShouldThrowJsonSyntaxException() {
String jsonString = "{'employee.name':'Bob', 'employee.name':'Jenny','employee.salary':10000, "
+ "'employee.active':true, " + "'employee':{'id':10, 'name': 'Bob Willis', 'address':'London'}}";
Gson gson = new Gson();
HashMap map = gson.fromJson(jsonString, HashMap.class);
logger.info("The converted map: {}", map);
}
@Test
public void whenUsingTypeToken_thenShouldReturnMapWithProperClass() {
String jsonString = "{'Bob':{'id':10, 'name': 'Bob Willis', 'address':'UK'},"
+ "'Jenny':{'id':10, 'name': 'Jenny McCarthy', 'address':'USA'}, "
+ "'Steve':{'id':10, 'name': 'Steven Waugh', 'address':'Australia'}}";
Gson gson = new Gson();
Type empMapType = new TypeToken<HashMap<String, Employee>>(){}.getType();
HashMap<String, Employee> nameEmployeeMap = gson.fromJson(jsonString, empMapType);
logger.info("The converted map: {}", nameEmployeeMap);
Assert.assertEquals(3, nameEmployeeMap.size());
Assert.assertEquals(Employee.class, nameEmployeeMap.get("Bob").getClass());
}
@Test
public void whenUsingCustomDeserializer_thenShouldReturnMapWithProperClass() {
String jsonString = "{'employee.name':'Bob','employee.salary':10000, 'employee.active':true, "
+ "'employee':{'id':10, 'name': 'Bob Willis', 'address':'London'}}";
Type type = new TypeToken<HashMap<String, Object>>(){}.getType();
Gson gson = new GsonBuilder()
.registerTypeAdapter(type, new HashMapDeserializer())
.create();
HashMap<String, Object> blendedMap = gson.fromJson(jsonString, type);
logger.info("The converted map: {}", blendedMap);
Assert.assertEquals(4, blendedMap.size());
Assert.assertEquals(Integer.class, blendedMap.get("employee.salary").getClass());
Assert.assertEquals(Employee.class, blendedMap.get("employee").getClass());
}
}

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<logger name="org.springframework" level="WARN"/>
<logger name="org.springframework.transaction" level="WARN"/>
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
<logger name="org.springframework.web.servlet.mvc" level="WARN"/>
<root level="INFO">
<appender-ref ref="STDOUT"/>
</root>
</configuration>