Merge pull request #6503 from eugenp/BAEL-12185-v2

new example for jackson article
This commit is contained in:
Loredana Crusoveanu 2019-03-10 09:44:23 +02:00 committed by GitHub
commit 53f4bbf651
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 2 deletions

View File

@ -0,0 +1,18 @@
package com.baeldung.jackson.exception;
import com.fasterxml.jackson.annotation.JsonRootName;
@JsonRootName(value = "user", namespace="users")
public class UserWithRootNamespace {
public int id;
public String name;
public UserWithRootNamespace() {
super();
}
public UserWithRootNamespace(final int id, final String name) {
this.id = id;
this.name = name;
}
}

View File

@ -37,6 +37,7 @@ import com.baeldung.jackson.date.EventWithSerializer;
import com.baeldung.jackson.dtos.MyMixInForIgnoreType;
import com.baeldung.jackson.dtos.withEnum.DistanceEnumWithValue;
import com.baeldung.jackson.exception.UserWithRoot;
import com.baeldung.jackson.exception.UserWithRootNamespace;
import com.baeldung.jackson.jsonview.Item;
import com.baeldung.jackson.jsonview.Views;
import com.fasterxml.jackson.core.JsonProcessingException;
@ -47,6 +48,7 @@ import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
public class JacksonAnnotationUnitTest {
@ -378,13 +380,40 @@ public class JacksonAnnotationUnitTest {
public void whenDeserializingUsingJsonAlias_thenCorrect() throws IOException {
// arrange
String json = "{\"fName\": \"Alex\", \"lastName\": \"Theedom\"}";
String json = "{\"fName\": \"John\", \"lastName\": \"Green\"}";
// act
AliasBean aliasBean = new ObjectMapper().readerFor(AliasBean.class).readValue(json);
// assert
assertThat(aliasBean.getFirstName(), is("Alex"));
assertThat(aliasBean.getFirstName(), is("John"));
}
@Test
public void whenSerializingUsingXMLRootNameWithNameSpace_thenCorrect() throws JsonProcessingException {
// arrange
UserWithRootNamespace author = new UserWithRootNamespace(1, "John");
// act
ObjectMapper mapper = new XmlMapper();
mapper = mapper.enable(SerializationFeature.WRAP_ROOT_VALUE).enable(SerializationFeature.INDENT_OUTPUT);
String result = mapper.writeValueAsString(author);
// assert
assertThat(result, containsString("<user xmlns=\"users\">"));
/*
<user xmlns="users">
<id xmlns="">3006b44a-cf62-4cfe-b3d8-30dc6c46ea96</id>
<id xmlns="">1</id>
<name xmlns="">John</name>
<items xmlns=""/>
</user>
*/
}
}