question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to write a Deserializer which only works for List<String>, but not for other Lists?

See original GitHub issue

I 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:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
cowtowncodercommented, Aug 25, 2021

Excellent @Alwinator, thank you for sharing the solution!

0reactions
Alwinatorcommented, Aug 25, 2021

@cowtowncoder Thanks! That was exactly the hint I needed. I have implemented the solution here: https://github.com/Alwinator/StringListJsonTrimmer

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found