0

I am using a Custom deserializer to parse the json node to appropriate subclass. When some of properties have umlaut characters , code fails at parser.readValueAsTree(). I tried using JsonDeserializer over property as well but it is never been called. I want to strip these accent characters from parser and then deserialize it. Is there a way to modify some of the key value properties to remove these characters and then read the json node?

Below is the code :

import java.io.IOException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.TreeNode;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.node.TextNode;


public class CustomAddressDeserializer extends StdDeserializer<Address> {

    
    protected CustomAddressDeserializer() {
        super(Address.class);
    }

    @Override
    public Address deserialize(JsonParser parser, DeserializationContext ctxt)
            throws IOException, JsonProcessingException {
        TreeNode node = parser.readValueAsTree();
        //code fails at this point
    
    }
}

json node is a list of addresses which contains city with accent characters

5
  • Can you edit your question and add example JSON payload and code which reproduces this bug? Commented Sep 2, 2022 at 18:35
  • sure, so I have a list object which contains key as city e.g; as shown below "listObject": [ { "city": "Óerlin", "country": "CA", "postalCode": "90001", "territory": "ON" } ], the city has accent character in it Commented Sep 2, 2022 at 18:41
  • Is there a way to validate the json before deserailization from JsonParser and replace these accent characters. All the code examples which I see validates on json string Commented Sep 2, 2022 at 19:07
  • I think this is not a problem with Jackson. Try to create a simple test where you provide JSON payload as a String. How do you read receive this JSON? Do you read it from file? Try to read it is as UTF-8. Jackson ObjectMapper with UTF-8 encoding? Commented Sep 2, 2022 at 22:21
  • 1
    Thank you so much @MichałZiober for the suggestion. I figured this out right after posting my question. Commented Sep 4, 2022 at 0:34

1 Answer 1

1

So, the problem was actually with the encoding. I have to explicity set the encoding to UTF-8 in my VM args. -Dfile.encoding=UTF-8

This solved the problem.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.