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

Add TypeHierarchyAdapters? #3

Open
DanielGronau opened this issue Apr 12, 2018 · 2 comments
Open

Add TypeHierarchyAdapters? #3

DanielGronau opened this issue Apr 12, 2018 · 2 comments

Comments

@DanielGronau
Copy link

When a class contains a field with a vavr interface type, e.g. Map, Gson can't deserialize it, as it needs a certain implementation of Map. Often a particular implementation (like HashMap) would be a good default for the majority of classes, so one could just register a TypeHierarchyAdapter using a HashMap to deserialize a Map field. Such TypeHierarchyAdapters could be part of the library (but not registered in VavrGson, just as additional helpers).

@ruslansennov
Copy link
Member

PRs Welcome!

@mincong-h
Copy link
Member

Hi @DanielGronau and @ruslansennov , I would like to help on this issue. I created a simple test case trying to reproduce, but the test passed. So both serialization and deserialization works.

package io.vavr.gson.map;

import io.vavr.collection.HashMap;
import io.vavr.collection.Map;
import io.vavr.gson.AbstractTest;
import org.junit.Test;

public class MapInObjectTest extends AbstractTest {
    @Test
    public void testTypeHierarchyAdapters() {
        Foo foo = gson.fromJson("{\"config\":{\"k\":1}}", Foo.class);
        assert foo.config.equals(HashMap.of("k", 1));
        assert gson.toJson(foo).equals("{\"config\":{\"k\":1}}");
    }

    private static class Foo {
        private Map<String, Integer> config;

        Foo() {
            // no-args constructor
        }
    }
}

Am I doing something wrong?

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

No branches or pull requests

3 participants