custom deserialization work

This commit is contained in:
eugenp 2014-01-12 11:58:07 +02:00
parent 1a217c46ea
commit ff141b5afb
8 changed files with 125 additions and 19 deletions

View File

@ -0,0 +1,30 @@
package org.baeldung.jackson.deserialization;
import java.io.IOException;
import org.baeldung.jackson.dtos.Item;
import org.baeldung.jackson.dtos.User;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.IntNode;
public class ItemDeserializer extends JsonDeserializer<Item> {
/**
* {"id":1,"itemNr":"theItem","owner":2}
*/
@Override
public Item deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException, JsonProcessingException {
final JsonNode node = jp.getCodec().readTree(jp);
final int id = (Integer) ((IntNode) node.get("id")).numberValue();
final String itemName = node.get("itemName").asText();
final int userId = (Integer) ((IntNode) node.get("owner")).numberValue();
return new Item(id, itemName, new User(userId, null));
}
}

View File

@ -0,0 +1,30 @@
package org.baeldung.jackson.deserialization;
import java.io.IOException;
import org.baeldung.jackson.dtos.ItemWithSerializer;
import org.baeldung.jackson.dtos.User;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.IntNode;
public class ItemDeserializerOnClass extends JsonDeserializer<ItemWithSerializer> {
/**
* {"id":1,"itemNr":"theItem","owner":2}
*/
@Override
public ItemWithSerializer deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException, JsonProcessingException {
final JsonNode node = jp.getCodec().readTree(jp);
final int id = (Integer) ((IntNode) node.get("id")).numberValue();
final String itemName = node.get("itemName").asText();
final int userId = (Integer) ((IntNode) node.get("owner")).numberValue();
return new ItemWithSerializer(id, itemName, new User(userId, null));
}
}

View File

@ -1,13 +1,17 @@
package org.baeldung.jackson.dtos;
public class Item {
public final int id;
public final String itemNr;
public final User owner;
public int id;
public String itemName;
public User owner;
public Item(final int id, final String itemNr, final User owner) {
public Item() {
super();
}
public Item(final int id, final String itemName, final User owner) {
this.id = id;
this.itemNr = itemNr;
this.itemName = itemName;
this.owner = owner;
}
@ -17,8 +21,8 @@ public class Item {
return id;
}
public String getItemNr() {
return itemNr;
public String getItemName() {
return itemName;
}
public User getOwner() {

View File

@ -1,18 +1,21 @@
package org.baeldung.jackson.dtos;
import org.baeldung.jackson.deserialization.ItemDeserializerOnClass;
import org.baeldung.jackson.serialization.ItemSerializerOnClass;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
@JsonSerialize(using = ItemSerializerOnClass.class)
@JsonDeserialize(using = ItemDeserializerOnClass.class)
public class ItemWithSerializer {
public final int id;
public final String itemNr;
public final String itemName;
public final User owner;
public ItemWithSerializer(final int id, final String itemNr, final User owner) {
public ItemWithSerializer(final int id, final String itemName, final User owner) {
this.id = id;
this.itemNr = itemNr;
this.itemName = itemName;
this.owner = owner;
}
@ -22,8 +25,8 @@ public class ItemWithSerializer {
return id;
}
public String getItemNr() {
return itemNr;
public String getItemName() {
return itemName;
}
public User getOwner() {

View File

@ -1,8 +1,12 @@
package org.baeldung.jackson.dtos;
public class User {
public final int id;
public final String name;
public int id;
public String name;
public User() {
super();
}
public User(final int id, final String name) {
this.id = id;

View File

@ -15,8 +15,8 @@ public class ItemSerializer extends JsonSerializer<Item> {
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.owner.id);
jgen.writeStringField("itemName", value.itemName);
jgen.writeNumberField("owner", value.owner.id);
jgen.writeEndObject();
}

View File

@ -15,8 +15,8 @@ public class ItemSerializerOnClass extends JsonSerializer<ItemWithSerializer> {
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.writeStringField("itemName", value.itemName);
jgen.writeNumberField("owner", value.owner.id);
jgen.writeEndObject();
}

View File

@ -1,11 +1,15 @@
package org.baeldung.jackson.test;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import java.io.IOException;
import org.baeldung.jackson.deserialization.ItemDeserializer;
import org.baeldung.jackson.dtos.Item;
import org.baeldung.jackson.dtos.ItemWithSerializer;
import org.baeldung.jackson.dtos.MyDto;
import org.baeldung.jackson.dtos.ignore.MyDtoIgnoreUnknown;
import org.junit.Test;
@ -18,7 +22,7 @@ import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
import com.fasterxml.jackson.databind.module.SimpleModule;
public class JacksonDeserializationUnitTest {
@ -130,4 +134,35 @@ public class JacksonDeserializationUnitTest {
assertThat(jsonNode1.textValue(), equalTo("v1"));
}
// custom deserialization
@Test
public final void whenDeserializingTheStandardRepresentation_thenCorrect() throws JsonParseException, JsonMappingException, IOException {
final String json = "{\"id\":1,\"itemName\":\"theItem\",\"owner\":{\"id\":2,\"name\":\"theUser\"}}";
final Item readValue = new ObjectMapper().readValue(json, Item.class);
assertThat(readValue, notNullValue());
}
@Test
public final void whenDeserializingANonStandardRepresentation_thenCorrect() throws JsonParseException, JsonMappingException, IOException {
final String json = "{\"id\":1,\"itemName\":\"theItem\",\"owner\":2}";
final ObjectMapper mapper = new ObjectMapper();
final SimpleModule module = new SimpleModule();
module.addDeserializer(Item.class, new ItemDeserializer());
mapper.registerModule(module);
final Item readValue = mapper.readValue(json, Item.class);
assertThat(readValue, notNullValue());
}
@Test
public final void givenDeserializerIsOnClass_whenDeserializingCustomRepresentation_thenCorrect() throws JsonParseException, JsonMappingException, IOException {
final String json = "{\"id\":1,\"itemName\":\"theItem\",\"owner\":2}";
final ItemWithSerializer readValue = new ObjectMapper().readValue(json, ItemWithSerializer.class);
assertThat(readValue, notNullValue());
}
}