-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
1 parent
24de8a2
commit f2bb0df
Showing
6 changed files
with
192 additions
and
74 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
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
70 changes: 70 additions & 0 deletions
70
src/test/java/com/fasterxml/jackson/databind/jsontype/ExternalTypeId198Test.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,70 @@ | ||
package com.fasterxml.jackson.databind.jsontype; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonSubTypes; | ||
import com.fasterxml.jackson.annotation.JsonTypeInfo; | ||
|
||
import com.fasterxml.jackson.databind.*; | ||
|
||
public class ExternalTypeId198Test extends BaseMapTest | ||
{ | ||
public enum Attacks { KICK, PUNCH } | ||
|
||
static class Character { | ||
public String name; | ||
public Attacks preferredAttack; | ||
|
||
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, defaultImpl=Kick.class, | ||
include=JsonTypeInfo.As.EXTERNAL_PROPERTY, property="preferredAttack") | ||
@JsonSubTypes({ | ||
@JsonSubTypes.Type(value=Kick.class, name="KICK"), | ||
@JsonSubTypes.Type(value=Punch.class, name="PUNCH") | ||
}) | ||
public Attack attack; | ||
} | ||
|
||
public static abstract class Attack { | ||
public String side; | ||
|
||
@JsonCreator | ||
public Attack(String side) { | ||
this.side = side; | ||
} | ||
} | ||
|
||
public static class Kick extends Attack { | ||
@JsonCreator | ||
public Kick(String side) { | ||
super(side); | ||
} | ||
} | ||
|
||
public static class Punch extends Attack { | ||
@JsonCreator | ||
public Punch(String side) { | ||
super(side); | ||
} | ||
} | ||
|
||
final ObjectMapper MAPPER = new ObjectMapper(); | ||
|
||
public void testFails() throws Exception { | ||
String json = "{ \"name\": \"foo\", \"attack\":\"right\" } }"; | ||
|
||
Character character = MAPPER.readValue(json, Character.class); | ||
|
||
assertNotNull(character); | ||
assertNotNull(character.attack); | ||
assertEquals("foo", character.name); | ||
} | ||
|
||
public void testWorks() throws Exception { | ||
String json = "{ \"name\": \"foo\", \"preferredAttack\": \"KICK\", \"attack\":\"right\" } }"; | ||
|
||
Character character = MAPPER.readValue(json, Character.class); | ||
|
||
assertNotNull(character); | ||
assertNotNull(character.attack); | ||
assertEquals("foo", character.name); | ||
} | ||
} |
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
Oops, something went wrong.