Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot deserialize JSON array to vavr List, using JavaType ObjectMapper API #189

Open
skapral opened this issue Nov 15, 2021 · 2 comments
Open
Milestone

Comments

@skapral
Copy link

skapral commented Nov 15, 2021

The version is 0.10.3

Minimal example for reproducing the issue is:

package com.skapral.test;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import io.vavr.collection.List;
import io.vavr.jackson.datatype.VavrModule;

import java.lang.reflect.Method;

class XY {
    private int x, y;

    public XY() {
    }

    public XY(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }
}


public class Main2 {
    private static final Method sample;

    static {
        try {
            sample = Main2.class.getDeclaredMethod("sample", List.class);
        } catch(Exception ex) {
            throw new RuntimeException(ex);
        }
    }


    public static void sample(List<XY> arg) {}

    public static void main(String... args) {
        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(new VavrModule());

        String s = "[{\"x\":1,\"y\":2},{\"x\":3,\"y\":4}]";

        JavaType a = TypeFactory.defaultInstance().constructType(sample.getGenericParameterTypes()[0]);
        try {
            var list = mapper.readValue(s, a);
        } catch(Exception ex) {
            throw new RuntimeException(ex);
        }
    }
}

Attempt to run this code will cause

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `byte` from Object value (token `JsonToken.START_OBJECT`)

Some context on how the issue was bumped into, can be found here. I was using vavr + vavr-jackson + spring AMQP for messaging, and faced the issue when made attempt to deserialize AMQP message with JSON array payload. Spring AMQP does deserialization the same way, like it is done in example above.

@pivovarit
Copy link
Member

pivovarit commented Oct 14, 2024

I have a simpler reproducer here: #225

The core issue is that Jackson has no clue that Vavr's List is a collection type. This will be fixed soon in 0.10.5

@pivovarit pivovarit added this to the 0.10.5 milestone Oct 14, 2024
@pivovarit
Copy link
Member

pivovarit commented Oct 14, 2024

It's not a bug in vavr-jackson, but the reproducer is faulty - you should not be using TypeFactory.defaultInstance() but mapper instead which has all required mappings. If this is how Spring AMQP does things internally, then it's a bug there

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants