簡體   English   中英

如何封送/解封地圖 <Integer, List<Integer> &gt;?

[英]How to marshal/unmarshal a Map<Integer, List<Integer>>?

下面的程序將編組和解組包含Map<Integer, List<Integer>>字段的類。

解組地圖后,列表中包含字符串而不是整數。

有沒有一種簡單的方法來確保在解組期間列表將用整數而不是字符串填充?

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以正確處理此用例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM