jackson custom serializer work

This commit is contained in:
eugenp 2014-01-11 20:56:10 +02:00
parent a47c533ec9
commit 1a217c46ea
12 changed files with 92 additions and 27 deletions

View File

@ -3,12 +3,12 @@ package org.baeldung.jackson.dtos;
public class Item { public class Item {
public final int id; public final int id;
public final String itemNr; public final String itemNr;
public final User createdBy; public final User owner;
public Item(final int id, final String itemNr, final User createdBy) { public Item(final int id, final String itemNr, final User owner) {
this.id = id; this.id = id;
this.itemNr = itemNr; this.itemNr = itemNr;
this.createdBy = createdBy; this.owner = owner;
} }
// API // API
@ -21,8 +21,8 @@ public class Item {
return itemNr; return itemNr;
} }
public User getCreatedBy() { public User getOwner() {
return createdBy; return owner;
} }
} }

View File

@ -0,0 +1,33 @@
package org.baeldung.jackson.dtos;
import org.baeldung.jackson.serialization.ItemSerializerOnClass;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
@JsonSerialize(using = ItemSerializerOnClass.class)
public class ItemWithSerializer {
public final int id;
public final String itemNr;
public final User owner;
public ItemWithSerializer(final int id, final String itemNr, final User owner) {
this.id = id;
this.itemNr = itemNr;
this.owner = owner;
}
// API
public int getId() {
return id;
}
public String getItemNr() {
return itemNr;
}
public User getOwner() {
return owner;
}
}

View File

@ -1,4 +1,4 @@
package org.baeldung.jackson.dtos; package org.baeldung.jackson.dtos.ignore;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;

View File

@ -1,4 +1,4 @@
package org.baeldung.jackson.dtos; package org.baeldung.jackson.dtos.ignore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

View File

@ -1,4 +1,4 @@
package org.baeldung.jackson.dtos; package org.baeldung.jackson.dtos.ignore;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.annotation.JsonInclude.Include;

View File

@ -1,4 +1,4 @@
package org.baeldung.jackson.dtos; package org.baeldung.jackson.dtos.ignore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

View File

@ -1,4 +1,4 @@
package org.baeldung.jackson.dtos; package org.baeldung.jackson.dtos.ignore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

View File

@ -1,4 +1,4 @@
package org.baeldung.jackson.try1; package org.baeldung.jackson.serialization;
import java.io.IOException; import java.io.IOException;
@ -16,7 +16,7 @@ public class ItemSerializer extends JsonSerializer<Item> {
jgen.writeStartObject(); jgen.writeStartObject();
jgen.writeNumberField("id", value.id); jgen.writeNumberField("id", value.id);
jgen.writeStringField("itemNr", value.itemNr); jgen.writeStringField("itemNr", value.itemNr);
jgen.writeNumberField("createdBy", value.createdBy.id); jgen.writeNumberField("createdBy", value.owner.id);
jgen.writeEndObject(); jgen.writeEndObject();
} }

View File

@ -0,0 +1,23 @@
package org.baeldung.jackson.serialization;
import java.io.IOException;
import org.baeldung.jackson.dtos.ItemWithSerializer;
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 ItemSerializerOnClass extends JsonSerializer<ItemWithSerializer> {
@Override
public final void serialize(final ItemWithSerializer 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.owner.id);
jgen.writeEndObject();
}
}

View File

@ -7,7 +7,7 @@ import static org.junit.Assert.assertThat;
import java.io.IOException; import java.io.IOException;
import org.baeldung.jackson.dtos.MyDto; import org.baeldung.jackson.dtos.MyDto;
import org.baeldung.jackson.dtos.MyDtoIgnoreUnknown; import org.baeldung.jackson.dtos.ignore.MyDtoIgnoreUnknown;
import org.junit.Test; import org.junit.Test;
import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonFactory;

View File

@ -7,12 +7,12 @@ import static org.junit.Assert.assertThat;
import java.io.IOException; import java.io.IOException;
import org.baeldung.jackson.dtos.MyDto; import org.baeldung.jackson.dtos.MyDto;
import org.baeldung.jackson.dtos.MyDtoIgnoreField;
import org.baeldung.jackson.dtos.MyDtoIgnoreFieldByName;
import org.baeldung.jackson.dtos.MyDtoIgnoreNull;
import org.baeldung.jackson.dtos.MyDtoIncludeNonDefault; import org.baeldung.jackson.dtos.MyDtoIncludeNonDefault;
import org.baeldung.jackson.dtos.MyDtoWithFilter; import org.baeldung.jackson.dtos.MyDtoWithFilter;
import org.baeldung.jackson.dtos.MyMixInForString; import org.baeldung.jackson.dtos.MyMixInForString;
import org.baeldung.jackson.dtos.ignore.MyDtoIgnoreField;
import org.baeldung.jackson.dtos.ignore.MyDtoIgnoreFieldByName;
import org.baeldung.jackson.dtos.ignore.MyDtoIgnoreNull;
import org.junit.Test; import org.junit.Test;
import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.annotation.JsonInclude.Include;

View File

@ -1,30 +1,27 @@
package org.baeldung.jackson.test; 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.containsString;
import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import java.io.IOException; import java.io.IOException;
import java.io.StringWriter;
import java.util.List; import java.util.List;
import org.baeldung.jackson.dtos.Item; import org.baeldung.jackson.dtos.Item;
import org.baeldung.jackson.dtos.ItemWithSerializer;
import org.baeldung.jackson.dtos.MyDto; import org.baeldung.jackson.dtos.MyDto;
import org.baeldung.jackson.dtos.MyDtoFieldNameChanged; import org.baeldung.jackson.dtos.MyDtoFieldNameChanged;
import org.baeldung.jackson.dtos.MyDtoNoAccessors; import org.baeldung.jackson.dtos.MyDtoNoAccessors;
import org.baeldung.jackson.dtos.MyDtoNoAccessorsAndFieldVisibility; import org.baeldung.jackson.dtos.MyDtoNoAccessorsAndFieldVisibility;
import org.baeldung.jackson.dtos.User; import org.baeldung.jackson.dtos.User;
import org.baeldung.jackson.try1.ItemSerializer; import org.baeldung.jackson.serialization.ItemSerializer;
import org.junit.Test; import org.junit.Test;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonGenerationException; import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.module.SimpleModule;
@ -101,20 +98,32 @@ public class JacksonSerializationUnitTest {
// tests - custom serializer // tests - custom serializer
@Test
public final void whenSerializing_thenNoExceptions() throws JsonGenerationException, JsonMappingException, IOException {
final Item myItem = new Item(1, "theItem", new User(2, "theUser"));
final String serialized = new ObjectMapper().writeValueAsString(myItem);
System.out.println(serialized);
}
@Test @Test
public final void whenSerializingWithCustomSerializer_thenNoExceptions() throws JsonGenerationException, JsonMappingException, IOException { 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 Item myItem = new Item(1, "theItem", new User(2, "theUser"));
final ObjectMapper mapper = new ObjectMapper(); final ObjectMapper mapper = new ObjectMapper();
final SimpleModule simpleModule = new SimpleModule("SimpleModule", new Version(1, 0, 0, null)); final SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(Item.class, new ItemSerializer()); simpleModule.addSerializer(Item.class, new ItemSerializer());
// simpleModule.addSerializer(User.class, new UserSerializer());
mapper.registerModule(simpleModule); mapper.registerModule(simpleModule);
final StringWriter writer = new StringWriter(); final String serialized = mapper.writeValueAsString(myItem);
mapper.writeValue(writer, myItem); System.out.println(serialized);
final String serialized = writer.toString(); }
@Test
public final void givenSerializerRegisteredOnClass_whenSerializingWithCustomSerializer_thenNoExceptions() throws JsonGenerationException, JsonMappingException, IOException {
final ItemWithSerializer myItem = new ItemWithSerializer(1, "theItem", new User(2, "theUser"));
final String serialized = new ObjectMapper().writeValueAsString(myItem);
System.out.println(serialized); System.out.println(serialized);
} }