Java Genson能否处理对象上的通用嵌套字段?

Java Genson能否处理对象上的通用嵌套字段?,java,json,jersey,genson,Java,Json,Jersey,Genson,基本上,我有一些Java对象,我希望将它们序列化为JSON,并且尽可能省事。现在我用的是Tomcat、Jersey和Genson 我发现像这样的东西对Genson不起作用(当然,这是一个玩具示例): public class Main { public static void main(String[] args) { MyClass mc = new MyClass(); mc.mapOfSets = new HashMap<>();

基本上,我有一些Java对象,我希望将它们序列化为JSON,并且尽可能省事。现在我用的是Tomcat、Jersey和Genson

我发现像这样的东西对Genson不起作用(当然,这是一个玩具示例):

public class Main {
    public static void main(String[] args) {
        MyClass mc = new MyClass();
        mc.mapOfSets = new HashMap<>();
        Set<String> set0 = new HashSet<>();
        set0.add("0");
        Set<String> set1 = new HashSet<>();
        set1.add("1");
        set1.add("11");
        Set<String> set2 = new HashSet<>();
        set2.add("2");
        set2.add("22");
        set2.add("222");
        mc.mapOfSets.put("set0", set0);
        mc.mapOfSets.put("set1", set1);
        mc.mapOfSets.put("set2", set2);
        try {
            String json1 = new Genson().serialize(mc.mapOfSets);
            System.out.println(json1);
            String json = new Genson().serialize(mc);
            System.out.println(json);
        } catch (TransformationException | IOException e) {
            e.printStackTrace();
        }
    }
}

class MyClass {
    public Map<String, Set<String>> mapOfSets;
}
Genson的优点是我把它放在我的WebContent文件夹中,它被用来代替与Jersey捆绑在一起的任何东西——不需要额外的配置。如果有一种非常直接的方法可以让像上面这样的对象序列化为JSON,而不必为每个模式编写某种转换器,我很乐意将其用于Jersey而不是Genson,但是Genson到目前为止并没有让我失望

那么-我如何让Genson正确地序列化-或者什么是一个无痛处理这类事情的库


谢谢

我正在使用Guice来处理依赖注入需求,这就是为什么我很难将Jackson与我的Jersey项目整合在一起。由于Genson没有做我想做的事,我决定再次尝试Jackson。我试着改变一些事情,直到它起作用,试着对SO和Google提出不同的建议

现在,下面给出了我的沙箱项目中预期的输出:

ObjectMapper mapper = new ObjectMapper();
AnnotationIntrospector introspector = new JaxbAnnotationIntrospector();
mapper.getSerializationConfig().setAnnotationIntrospector(introspector);
String jsonData = mapper.writeValueAsString(mc);
System.out.println(jsonData);


我是詹森的作家。我刚刚检查过,这是一个bug,泛型在Genson中可以正常工作,除了这个特殊情况。。。 如果你能等到明天,我今晚将推出一个新版本,包含修复程序和一些小的新功能。我会更新我的答案,当它完成


编辑修正错误并将0.94版推送至公共maven repo,最长应在几个小时内提供。以下是一些示例。请尝试并确认它是否解决了您的问题。谢谢:)

发布完成,请参阅我的编辑
ObjectMapper mapper = new ObjectMapper();
AnnotationIntrospector introspector = new JaxbAnnotationIntrospector();
mapper.getSerializationConfig().setAnnotationIntrospector(introspector);
String jsonData = mapper.writeValueAsString(mc);
System.out.println(jsonData);
{"mapOfSets":{"set1":["1","11"],"set0":["0"],"set2":["2","222","22"]}}