How to write a Deserializer which only works for List<String>, but not for other Lists?
See original GitHub issueI want to write a Deserializer which removes blank strings from arrays. I am not sure why, but my Deserializer is never called. (I don’t see the Hello World in the console). Additionally, the array with the blank string is written to the database with the blank strings. So, Jackson just skips my Deserializer for any reason. My other Deserializers work without a problem. I am not sure if this is a bug or if I forgot something in my code. Thanks in advance.
Version information Spring Boot 2.5.4 Jackson 2.12.4
To Reproduce ArrayTrimDeserializer.java
public class ArrayTrimDeserializer extends StdDeserializer<String[]> {
protected ArrayTrimDeserializer() {
super(String[].class);
}
@Override
public String[] deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
System.out.println("Hello World!");
JsonNode array = deserializationContext.readTree(jsonParser);
List<String> filtered = new ArrayList<>();
for(JsonNode node : array){
String str = node.textValue();
if(!str.isBlank()){
filtered.add(str);
}
}
return filtered.toArray(new String[0]);
}
}
RegisterModule.java
@Component
public class RegisterModule extends SimpleModule {
public RegisterModule() {
// ...
addDeserializer(String[].class, new ArrayTrimDeserializer());
}
}
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Java JSON Deserialize when type is String or List<String>
After deserializing, I always want to access it as a list for simplicity in the rest of my code. I've seen suggestions for...
Read more >Deserializing a list of JSON String in an Apex Method
deserialize () method has different signature and takes a variable of String type as a first param, not List<String> . Share.
Read more >How to JSON.deserialize() a List<String>?
I am new to REST API and am getting error on JSON deserialization :I tried the above solution, but NOT think i am...
Read more >Jackson Tips: custom List serialization | by @cowtowncoder
Serializer itself would be relatively easy if we know that: It only needs to work for String s (that is, List<String> ); We...
Read more >Deserialize list of array failed with enableDefaultTyping #2095
I think the problem is Java Type Erasure with root values: you are serializing something only seen as List<?> (due type erasure), and...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Excellent @Alwinator, thank you for sharing the solution!
@cowtowncoder Thanks! That was exactly the hint I needed. I have implemented the solution here: https://github.com/Alwinator/StringListJsonTrimmer