使用MOXy </object>编组列表<object>

时间:2014-08-14 17:36:14

标签: java json eclipselink marshalling moxy

我已经阅读了很多Blaise Doughan的StackOverflow答案和博客文章,我认为我理解他使用@XmlAnyElement和Object []的例子。

但是相同的原则似乎不适用于List - 如下面的示例所示(部分从他的一个示例复制,部分从xjc输出复制)。

我相信下面的代码应该创建JSON:

{"method":"test","status":["value":"500"]}

但它正在创建JASON:

{"method":"test","value":["com.mdsh.test.JsonRequestTest$Status@64dbfe37"]}

对我来说没用多大。

有人可以指导我正确编组这个小物件吗?

package com.mdsh.test;

import static org.junit.Assert.assertEquals;

import java.io.StringWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.XmlRootElement;

import org.eclipse.persistence.jaxb.MarshallerProperties;
import org.junit.Test;
import org.jvnet.jaxb2_commons.lang.JAXBToStringStrategy;
import org.jvnet.jaxb2_commons.lang.ToStringStrategy;
import org.jvnet.jaxb2_commons.locator.ObjectLocator;

public class JsonRequestTest
{
    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    public static class Request
    {
        String method;

        @XmlAnyElement(lax = true)
        protected List<Object> any = new Vector<Object>();

        public String getMethod()
        {
            return this.method;
        }

        public void setMethod(final String value)
        {
            this.method = value;
        }

        public List<Object> getAny()
        {
            if (this.any == null) {
                this.any = new Vector<Object>();
            }
            return this.any;
        }

        @Override
        public String toString()
        {
            final ToStringStrategy strategy = JAXBToStringStrategy.INSTANCE;
            final StringBuilder buffer = new StringBuilder();
            append(null, buffer, strategy);
            return buffer.toString();
        }

        public StringBuilder append(final ObjectLocator locator, final StringBuilder buffer, final ToStringStrategy strategy)
        {
            strategy.appendStart(locator, this, buffer);
            appendFields(locator, buffer, strategy);
            strategy.appendEnd(locator, this, buffer);
            return buffer;
        }

        public StringBuilder appendFields(final ObjectLocator locator, final StringBuilder buffer, final ToStringStrategy strategy)
        {
            {
                List<Object> theAny;
                theAny = (((this.any!= null)&&(!this.any.isEmpty()))?getAny():null);
                strategy.appendField(locator, this, "any", buffer, theAny);
            }
            return buffer;
        }
    }

    public static class Status
    {
        int value;

        public int getValue()
        {
            return this.value;
        }

        public void setValue(final int value)
        {
            this.value = value;
        }
    }

    @Test
    public void testListOfObjects() throws JAXBException
    {
        System.setProperty(JAXBContext.class.getName(), "org.eclipse.persistence.jaxb.JAXBContextFactory");
        final Map<String, Object> props = new HashMap<String, Object>();
        props.put(MarshallerProperties.MEDIA_TYPE, "application/json");
        props.put(MarshallerProperties.JSON_INCLUDE_ROOT, false);

        final JAXBContext ctx = JAXBContext.newInstance(
                new Class<?>[] { Request.class },
                props);

        final Marshaller m = ctx.createMarshaller();
        final StringWriter writer = new StringWriter();
        final Request req = new Request();
        req.setMethod("test");
        final Status stat = new Status();
        stat.setValue(500);
        req.getAny().add(stat);
        m.marshal(req, writer);

        assertEquals("{\"method\":\"test\",\"status\":[\"value\":\"500\"]}",
                writer.toString());
    }

}

0 个答案:

没有答案