如何封送/取消封送Map< Integer,List< Integer>&gt ;? [英] How to marshal/unmarshal a Map<Integer, List<Integer>>?

查看:102
本文介绍了如何封送/取消封送Map< Integer,List< Integer>&gt ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下程序将封送和取消封送包含Map<Integer, List<Integer>>字段的类.

The following program marshals and unmarshals a class containing a Map<Integer, List<Integer>> field.

解组地图中的列表后,该列表包含字符串,而不是整数.

After unmarshaling the list in the map contains strings and not integers.

是否有一种简单的方法来确保列表中将填充整数而不是 在解组期间输入字符串?

Is there an easy way to ensure that the list will be filled with integers instead of strings during unmarshalling?

import java.io.StringReader;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlRootElement;
import org.eclipse.persistence.jaxb.JAXBContextFactory;
import org.eclipse.persistence.jaxb.MarshallerProperties;
import org.eclipse.persistence.oxm.MediaType;

public class MapApp {

    @XmlRootElement
    public static class Publication {

        private String name;

        private Map<Integer, List<Integer>> yearToIssues;

        public void setName(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public void setYearToIssues(Map<Integer, List<Integer>> yearToIssues) {
            this.yearToIssues = yearToIssues;
        }

        public Map<Integer, List<Integer>> getYearToIssues() {
            return yearToIssues;
        }

    }

    public static void main(String[] args) throws JAXBException {
        Publication publication = new Publication();
        publication.setName("JAXB miracles");
        Map<Integer, List<Integer>> yearToIssues = new HashMap<>();
        yearToIssues.put(2013, Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12));
        yearToIssues.put(2014, Arrays.asList(1, 2));
        publication.setYearToIssues(yearToIssues);
        String marshalled = marshal(publication);
        Publication uPublication = unmarshal(Publication.class, marshalled);
        List<Integer> issues = uPublication.getYearToIssues().get(2013);
        if (((Object) issues.get(0)) instanceof String) {
            System.out.println("issue is instance of String!");
        }

    }

    static String marshal(Object toMarshal) throws JAXBException {
        JAXBContext jc = JAXBContextFactory.createContext(new Class[] {toMarshal.getClass()}, null);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_XML);
        StringWriter sw = new StringWriter();
        marshaller.marshal(toMarshal, sw);
        System.out.println(sw);
        return sw.toString();
    }

    static <T> T unmarshal(Class<T> entityClass, String str) throws JAXBException {
        JAXBContext jc = JAXBContextFactory.createContext(new Class[] {entityClass}, null);
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        unmarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_XML);
        return (T) unmarshaller.unmarshal(new StringReader(str));
    }

}

推荐答案

您将需要为Map<Integer, List<Integer>>编写XmlAdapter才能正确处理此用例.

You are going to need to write an XmlAdapter for Map<Integer, List<Integer>> to properly handle this use case.

这篇关于如何封送/取消封送Map&lt; Integer,List&lt; Integer&gt;&gt ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆