intermediary json work
This commit is contained in:
parent
3d7ca2e81c
commit
8a13a58c04
@ -0,0 +1,37 @@
|
|||||||
|
package org.baeldung.jackson.dtos.withEnum;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonValue;
|
||||||
|
|
||||||
|
|
||||||
|
public enum TypeEnumWithValue {
|
||||||
|
TYPE1(1, "Type A"), TYPE2(2, "Type 2");
|
||||||
|
|
||||||
|
private Integer id;
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
private TypeEnumWithValue(final Integer id, final String name) {
|
||||||
|
this.id = id;
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
// API
|
||||||
|
|
||||||
|
public Integer getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(final Integer id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonValue
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(final String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -10,6 +10,8 @@ import org.baeldung.jackson.dtos.withEnum.MyDtoWithEnumCustom;
|
|||||||
import org.baeldung.jackson.dtos.withEnum.TypeEnum;
|
import org.baeldung.jackson.dtos.withEnum.TypeEnum;
|
||||||
import org.baeldung.jackson.dtos.withEnum.TypeEnumSimple;
|
import org.baeldung.jackson.dtos.withEnum.TypeEnumSimple;
|
||||||
import org.baeldung.jackson.dtos.withEnum.TypeEnumWithCustomSerializer;
|
import org.baeldung.jackson.dtos.withEnum.TypeEnumWithCustomSerializer;
|
||||||
|
import org.baeldung.jackson.dtos.withEnum.TypeEnumWithValue;
|
||||||
|
import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonParseException;
|
import com.fasterxml.jackson.core.JsonParseException;
|
||||||
@ -17,7 +19,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
|
|
||||||
public class JacksonSerializationEnumsUnitTest {
|
public class JacksonSerializationEnumsUnitTest {
|
||||||
|
|
||||||
// tests - enums
|
// tests - simple enum
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenSerializingASimpleEnum_thenCorrect() throws JsonParseException, IOException {
|
public final void whenSerializingASimpleEnum_thenCorrect() throws JsonParseException, IOException {
|
||||||
@ -28,6 +30,20 @@ public class JacksonSerializationEnumsUnitTest {
|
|||||||
assertThat(dtoAsString, containsString("TYPE1"));
|
assertThat(dtoAsString, containsString("TYPE1"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// tests - enum with main value
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Ignore("https://github.com/FasterXML/jackson-databind/issues/47")
|
||||||
|
public final void whenSerializingAEnumWithValue_thenCorrect() throws JsonParseException, IOException {
|
||||||
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
|
final String dtoAsString = mapper.writeValueAsString(TypeEnumWithValue.TYPE1.name());
|
||||||
|
System.out.println(dtoAsString);
|
||||||
|
|
||||||
|
assertThat(dtoAsString, containsString("Type A"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// tests - enum
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenSerializingAnEnum_thenCorrect() throws JsonParseException, IOException {
|
public final void whenSerializingAnEnum_thenCorrect() throws JsonParseException, IOException {
|
||||||
final ObjectMapper mapper = new ObjectMapper();
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
@ -46,15 +62,6 @@ public class JacksonSerializationEnumsUnitTest {
|
|||||||
assertThat(dtoAsString, containsString("\"name\":\"Type A\""));
|
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
|
@Test
|
||||||
public final void whenSerializingArrayOfEnums_thenCorrect() throws JsonParseException, IOException {
|
public final void whenSerializingArrayOfEnums_thenCorrect() throws JsonParseException, IOException {
|
||||||
final ObjectMapper mapper = new ObjectMapper();
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
@ -64,4 +71,15 @@ public class JacksonSerializationEnumsUnitTest {
|
|||||||
assertThat(json, containsString("\"name\":\"Type A\""));
|
assertThat(json, containsString("\"name\":\"Type A\""));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// tests - enum with custom serializer
|
||||||
|
|
||||||
|
@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\""));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,20 @@
|
|||||||
|
package org.baeldung.jackson.test;
|
||||||
|
|
||||||
|
import org.baeldung.jackson.sandbox.JacksonPrettyPrintUnitTest;
|
||||||
|
import org.baeldung.jackson.sandbox.SandboxTest;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.junit.runners.Suite;
|
||||||
|
|
||||||
|
@RunWith(Suite.class)
|
||||||
|
@Suite.SuiteClasses({// @formatter:off
|
||||||
|
JacksonCollectionDeserializationUnitTest.class
|
||||||
|
,JacksonSerializationEnumsUnitTest.class
|
||||||
|
,JacksonDeserializationUnitTest.class
|
||||||
|
,JacksonDeserializationUnitTest.class
|
||||||
|
,JacksonPrettyPrintUnitTest.class
|
||||||
|
,JacksonSerializationIgnoreUnitTest.class
|
||||||
|
,JacksonSerializationUnitTest.class
|
||||||
|
,SandboxTest.class
|
||||||
|
}) // @formatter:on
|
||||||
|
public class UnitTestSuite {
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user