forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
eugenp
committed
Jan 11, 2014
1 parent
c905b21
commit a47c533
Showing
6 changed files
with
162 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
jackson/src/test/java/org/baeldung/jackson/test/JacksonSerializationEnumsUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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\"")); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
jackson/src/test/java/org/baeldung/jackson/try1/ItemSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
|
||
} |