custom serializer work

This commit is contained in:
eugenp 2014-01-11 18:23:45 +02:00
parent c905b21ffc
commit a47c533ec9
6 changed files with 164 additions and 42 deletions

View File

@ -27,6 +27,12 @@
<version>4.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<!-- web -->
<!-- marshalling -->

View File

@ -0,0 +1,28 @@
package org.baeldung.jackson.dtos;
public class Item {
public final int id;
public final String itemNr;
public final User createdBy;
public Item(final int id, final String itemNr, final User createdBy) {
this.id = id;
this.itemNr = itemNr;
this.createdBy = createdBy;
}
// API
public int getId() {
return id;
}
public String getItemNr() {
return itemNr;
}
public User getCreatedBy() {
return createdBy;
}
}

View File

@ -0,0 +1,22 @@
package org.baeldung.jackson.dtos;
public class User {
public final int id;
public final String name;
public User(final int id, final String name) {
this.id = id;
this.name = name;
}
// API
public int getId() {
return id;
}
public String getName() {
return name;
}
}

View File

@ -0,0 +1,57 @@
package org.baeldung.jackson.test;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertThat;
import java.io.IOException;
import org.baeldung.jackson.dtos.withEnum.MyDtoWithEnum;
import org.baeldung.jackson.dtos.withEnum.MyDtoWithEnumCustom;
import org.baeldung.jackson.dtos.withEnum.TypeEnum;
import org.baeldung.jackson.dtos.withEnum.TypeEnumWithCustomSerializer;
import org.junit.Test;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonSerializationEnumsUnitTest {
// tests - enums
@Test
public final void whenSerializingSimpleEnum_thenCorrect() throws JsonParseException, IOException {
final ObjectMapper mapper = new ObjectMapper();
final String dtoAsString = mapper.writeValueAsString(TypeEnum.TYPE1);
System.out.println(dtoAsString);
assertThat(dtoAsString, containsString("\"name\":\"Type A\""));
}
@Test
public final void whenSerializingEntityWithEnum_thenCorrect() throws JsonParseException, IOException {
final ObjectMapper mapper = new ObjectMapper();
final String dtoAsString = mapper.writeValueAsString(new MyDtoWithEnum("a", 1, true, TypeEnum.TYPE1));
System.out.println(dtoAsString);
assertThat(dtoAsString, containsString("\"name\":\"Type A\""));
}
@Test
public final void givenCustomSerializer_whenSerializingEntityWithEnum_thenCorrect() throws JsonParseException, IOException {
final ObjectMapper mapper = new ObjectMapper();
final String dtoAsString = mapper.writeValueAsString(new MyDtoWithEnumCustom("a", 1, true, TypeEnumWithCustomSerializer.TYPE1));
System.out.println(dtoAsString);
assertThat(dtoAsString, containsString("\"name\":\"Type A\""));
}
@Test
public final void whenSerializingArrayOfEnums_thenCorrect() throws JsonParseException, IOException {
final ObjectMapper mapper = new ObjectMapper();
final String json = mapper.writeValueAsString(new TypeEnum[] { TypeEnum.TYPE1, TypeEnum.TYPE2 });
System.out.println(json);
assertThat(json, containsString("\"name\":\"Type A\""));
}
}

View File

@ -1,28 +1,33 @@
package org.baeldung.jackson.test;
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
import static org.apache.commons.lang3.RandomStringUtils.randomNumeric;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;
import java.io.IOException;
import java.io.StringWriter;
import java.util.List;
import org.baeldung.jackson.dtos.Item;
import org.baeldung.jackson.dtos.MyDto;
import org.baeldung.jackson.dtos.MyDtoFieldNameChanged;
import org.baeldung.jackson.dtos.MyDtoNoAccessors;
import org.baeldung.jackson.dtos.MyDtoNoAccessorsAndFieldVisibility;
import org.baeldung.jackson.dtos.withEnum.MyDtoWithEnum;
import org.baeldung.jackson.dtos.withEnum.MyDtoWithEnumCustom;
import org.baeldung.jackson.dtos.withEnum.TypeEnum;
import org.baeldung.jackson.dtos.withEnum.TypeEnumWithCustomSerializer;
import org.baeldung.jackson.dtos.User;
import org.baeldung.jackson.try1.ItemSerializer;
import org.junit.Test;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.google.common.collect.Lists;
public class JacksonSerializationUnitTest {
@ -82,44 +87,6 @@ public class JacksonSerializationUnitTest {
assertThat(dtoAsString, containsString("booleanValue"));
}
// tests - enums
@Test
public final void whenSerializingSimpleEnum_thenCorrect() throws JsonParseException, IOException {
final ObjectMapper mapper = new ObjectMapper();
final String dtoAsString = mapper.writeValueAsString(TypeEnum.TYPE1);
System.out.println(dtoAsString);
assertThat(dtoAsString, containsString("\"name\":\"Type A\""));
}
@Test
public final void whenSerializingEntityWithEnum_thenCorrect() throws JsonParseException, IOException {
final ObjectMapper mapper = new ObjectMapper();
final String dtoAsString = mapper.writeValueAsString(new MyDtoWithEnum("a", 1, true, TypeEnum.TYPE1));
System.out.println(dtoAsString);
assertThat(dtoAsString, containsString("\"name\":\"Type A\""));
}
@Test
public final void givenCustomSerializer_whenSerializingEntityWithEnum_thenCorrect() throws JsonParseException, IOException {
final ObjectMapper mapper = new ObjectMapper();
final String dtoAsString = mapper.writeValueAsString(new MyDtoWithEnumCustom("a", 1, true, TypeEnumWithCustomSerializer.TYPE1));
System.out.println(dtoAsString);
assertThat(dtoAsString, containsString("\"name\":\"Type A\""));
}
@Test
public final void whenSerializingArrayOfEnums_thenCorrect() throws JsonParseException, IOException {
final ObjectMapper mapper = new ObjectMapper();
final String json = mapper.writeValueAsString(new TypeEnum[] { TypeEnum.TYPE1, TypeEnum.TYPE2 });
System.out.println(json);
assertThat(json, containsString("\"name\":\"Type A\""));
}
// tests - multiple entities to json
@Test
@ -132,4 +99,23 @@ public class JacksonSerializationUnitTest {
System.out.println(dtosAsString);
}
// tests - custom serializer
@Test
public final void whenSerializingWithCustomSerializer_thenNoExceptions() throws JsonGenerationException, JsonMappingException, IOException {
final Item myItem = new Item(Integer.parseInt(randomNumeric(2)), randomAlphabetic(8), new User(Integer.parseInt(randomNumeric(2)), randomAlphabetic(8)));
final ObjectMapper mapper = new ObjectMapper();
final SimpleModule simpleModule = new SimpleModule("SimpleModule", new Version(1, 0, 0, null));
simpleModule.addSerializer(Item.class, new ItemSerializer());
// simpleModule.addSerializer(User.class, new UserSerializer());
mapper.registerModule(simpleModule);
final StringWriter writer = new StringWriter();
mapper.writeValue(writer, myItem);
final String serialized = writer.toString();
System.out.println(serialized);
}
}

View File

@ -0,0 +1,23 @@
package org.baeldung.jackson.try1;
import java.io.IOException;
import org.baeldung.jackson.dtos.Item;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
public class ItemSerializer extends JsonSerializer<Item> {
@Override
public final void serialize(final Item value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException, JsonProcessingException {
jgen.writeStartObject();
jgen.writeNumberField("id", value.id);
jgen.writeStringField("itemNr", value.itemNr);
jgen.writeNumberField("createdBy", value.createdBy.id);
jgen.writeEndObject();
}
}