Created
June 25, 2020 13:07
-
-
Save tsathis/765b4747337f090087f8a43575e36a21 to your computer and use it in GitHub Desktop.
temporary deserializer ##spring ##java ##deserializer
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
public class AnimalDeserializer extends StdDeserializer<Animal> | |
{ | |
@Autowired | |
ObjectMapper mapper; | |
public AnimalDeserializer( Class<?> vc, ObjectMapper mapper ) | |
{ | |
super( vc ); | |
this.mapper = mapper; | |
} | |
public AnimalDeserializer() | |
{ | |
this( null ); | |
} | |
public AnimalDeserializer( Class<?> vc ) | |
{ | |
super( vc ); | |
} | |
@Override | |
public Animal deserialize( JsonParser jsonParser, DeserializationContext deserializationContext ) throws IOException | |
{ | |
// JsonNode requestNode = mapper.readValue( jsonParser, JsonNode.class ); | |
JsonNode requestNode = jsonParser.getCodec().readTree(jsonParser); | |
if( requestNode.has( "type" ) ) | |
{ | |
String key = requestNode.get( "type" ).asText(); | |
if( !StringUtils.isEmpty( key ) ) | |
{ | |
if( key.equals( "DOG" ) ) | |
{ | |
String dogId = ""; | |
if (requestNode.has( "dogId" ) && !StringUtils.isEmpty( requestNode.get( "dogId" ).asText())) | |
dogId = requestNode.get( "dogId" ).asText(); | |
return new Dog(dogId); | |
} | |
else if( key.equals( "CAT" ) ) | |
{ | |
// return mapper.treeToValue( requestNode, Tiger.class ); | |
String catCode = ""; | |
if (requestNode.has( "catCode" ) && !StringUtils.isEmpty( requestNode.get( "catCode" ).asText())) | |
catCode = requestNode.get( "catCode" ).asText(); | |
return new Cat(catCode); | |
} | |
} | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment