search
HomeJavajavaTutorialHow to Call Jackson's Default Deserializer from a Custom Deserializer?

How to Call Jackson's Default Deserializer from a Custom Deserializer?

Calling the Default Deserializer from a Custom Deserializer in Jackson

Problem:

Creating a custom deserializer in Jackson that allows access to the default deserializer for pre-populating objects before applying custom logic.

Code:

public class UserEventDeserializer extends StdDeserializer<user> {

    public UserEventDeserializer() {
        super(User.class);
    }

    @Override
    public User deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
        
        // Line causing UnsupportedOperationException:
        User deserializedUser = super.deserialize(jp, ctxt, new User()); 
    }
}</user>

Solution:

Implement a BeanDeserializerModifier to register a custom deserializer for a specific class.

Code:

public class UserEventDeserializer extends StdDeserializer<user> implements ResolvableDeserializer {

    private final JsonDeserializer> defaultDeserializer;

    public UserEventDeserializer(JsonDeserializer> defaultDeserializer) {
        super(User.class);
        this.defaultDeserializer = defaultDeserializer;
    }

    @Override
    public User deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
        
        User deserializedUser = (User) defaultDeserializer.deserialize(jp, ctxt);

        // Special logic

        return deserializedUser;
    }

    // Required when modifying BeanDeserializer
    @Override
    public void resolve(DeserializationContext ctxt) throws JsonMappingException {
        ((ResolvableDeserializer) defaultDeserializer).resolve(ctxt);
    }

    public static void main(String[] args) throws Exception {
        SimpleModule module = new SimpleModule();
        module.setDeserializerModifier(new BeanDeserializerModifier() {
            @Override
            public JsonDeserializer> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc, JsonDeserializer> deserializer) {
                if (beanDesc.getBeanClass() == User.class)
                    return new UserEventDeserializer(deserializer);
                return deserializer;
            }
        });

        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(module);
        User user = mapper.readValue(new File("test.json"), User.class);
    }
}</user>

The above is the detailed content of How to Call Jackson's Default Deserializer from a Custom Deserializer?. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.