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

Nested maps are deserialized as java maps #109

Open
heldev opened this issue May 3, 2017 · 6 comments
Open

Nested maps are deserialized as java maps #109

heldev opened this issue May 3, 2017 · 6 comments
Labels

Comments

@heldev
Copy link

heldev commented May 3, 2017

Hi, It looks like that it's impossible right now to deserialize a JSON as vavr map tree, because only top level elements are deserialized as javaslang collections, deeper levels are java containers:

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import javaslang.collection.List;
import javaslang.collection.Map;
import javaslang.jackson.datatype.JavaslangModule;
import org.junit.Test;

import java.io.IOException;

import static org.assertj.core.api.Assertions.assertThat;


public class VavrJacksonTest {

    private static final String JSON = "[\n" +
            "   {\n" +
            "        \"field1\": \"val1\",\n" +
            "        \"complexField\": {\n" +
            "            \"subField1\": \"subValue1\",\n" +
            "            \"subField2\": \"subValue2\"\n" +
            "        }\n" +
            "    }\n" +
            "]";


    @Test
    public void nested_maps_should_be_javaslang_maps() throws IOException {
        List<Map<String, Object>> listOfMapTrees = new ObjectMapper()
                .registerModule(new JavaslangModule())
                .readValue(JSON, new TypeReference<List<Map<String, Object>>>() {});

        assertThat(listOfMapTrees.get().apply("complexField")).isInstanceOf(Map.class);
        //Expecting:
        // <{"subField1"="subValue1", "subField2"="subValue2"}>
        //to be an instance of:
        // <javaslang.collection.Map>
        //but was instance of:
        // <java.util.LinkedHashMap>
    }
}
@ruslansennov
Copy link
Member

ruslansennov commented May 3, 2017

This is expected behavior. By default, all JSON objects are deserialized to java.util.LinkedHashMap.

You can try using

new TypeReference<List<Map<String, Map<String,String>>>>() {}

instead of

new TypeReference<List<Map<String, Object>>>() {}

(unfortunately your example will fail because val1 is not a map)

@heldev
Copy link
Author

heldev commented May 3, 2017

I understand that there is, probably, some technical limitation from the jackson side, but this issue automatically breaks #75 deserialize(serialize(o)) equals o rule and gives a really hard time in some cases. Would it be possible to fix it ?

@ruslansennov
Copy link
Member

Would it be possible to fix it ?

Up to now I have no idea about how that issue should be fixed. Maybe tests will be produced without nested cases only...

@bduisenov
Copy link

Hi @ruslansennov, do you have any updates on this?
@heldev did you find a solution?

@heldev
Copy link
Author

heldev commented Feb 15, 2018

@bduisenov, no, not really :-/

@dbaltor
Copy link

dbaltor commented Feb 13, 2021

You might find this Stackoverflow answer useful.

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

No branches or pull requests

4 participants