有引用的JSON序列化是否有Java支持?

时间:2012-03-25 16:59:11

标签: java json jsonserializer

我正在寻找一个可以根据JSPON specification.

处理引用的Java JSPON序列化程序

目前有没有可以做到的?或者有没有办法修改现有的序列化程序来处理带有$ ref表示法的对象引用?

2 个答案:

答案 0 :(得分:0)

我会使用众多Object to JSon序列化库中的一个。许多库都是可扩展的,但我怀疑添加引用可能会变得复杂,除非你在何时使用它们做出一些实用的选择。

答案 1 :(得分:0)

注意:我是EclipseLink JAXB (MOXy)主管,是JAXB 2 (JSR-222)专家组的成员。

如果您对object-JSON绑定方法感兴趣,下面是使用MOXy完成的方法。以下示例基于JSPON Core Spec中的示例:

<强>父

Parent类是与JSON消息的根对应的域对象。它有两个类型为Child的字段。

package forum9862100;

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Parent {

    protected Child field1;
    protected Child field2;

}

儿童

Child类可以通过其键引用。我们将使用XmlAdapter处理此用例。我们通过XmlAdapter注释链接到@XmlJavaTypeAdapter

package forum9862100;

import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlJavaTypeAdapter(ChildAdapter.class)
@XmlAccessorType(XmlAccessType.FIELD)
public class Child {

    protected String id;
    protected String foo;
    protected Integer bar;

}

<强> ChildAdapter

以下是XmlAdapter的实施。此XmlAdapter是有状态的,这意味着我们需要在MarshallerUnmarshaller上设置实例。

package forum9862100;

import java.util.*;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.adapters.XmlAdapter;

public class ChildAdapter extends XmlAdapter<ChildAdapter.AdaptedChild, Child>{

    private List<Child> childList = new ArrayList<Child>();
    private Map<String, Child> childMap = new HashMap<String, Child>();

    public static class AdaptedChild extends Child {
        @XmlElement(name="$ref")
        public String reference;
    }

    @Override
    public AdaptedChild marshal(Child child) throws Exception {
        AdaptedChild adaptedChild = new AdaptedChild();
        if(childList.contains(child)) {
            adaptedChild.reference = child.id;
        } else {
            adaptedChild.id = child.id;
            adaptedChild.foo = child.foo;
            adaptedChild.bar = child.bar;
            childList.add(child);
        }
        return adaptedChild;
    }

    @Override
    public Child unmarshal(AdaptedChild adaptedChild) throws Exception {
        Child child = childMap.get(adaptedChild.reference);
        if(null == child) {
            child = new Child();
            child.id = adaptedChild.id;
            child.foo = adaptedChild.foo;
            child.bar = adaptedChild.bar;
            childMap.put(child.id, child);
        }
        return child;
    }

}

<强>演示

以下代码演示了如何在XmlAdapterMarshaller上指定有状态Unmarshaller

package forum9862100;

import java.io.File;
import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Parent.class);

        StreamSource json = new StreamSource(new File("src/forum9862100/input.json"));
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        unmarshaller.setProperty("eclipselink.media-type", "application/json");
        unmarshaller.setProperty("eclipselink.json.include-root", false);
        unmarshaller.setAdapter(new ChildAdapter());
        Parent parent = (Parent) unmarshaller.unmarshal(json, Parent.class).getValue();

        System.out.println(parent.field1 == parent.field2);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty("eclipselink.media-type", "application/json");
        marshaller.setProperty("eclipselink.json.include-root", false);
        marshaller.setAdapter(new ChildAdapter());
        marshaller.marshal(parent, System.out);
    }

}

<强>输出

以下是运行演示代码的输出。请注意Child的两个实例如何通过身份测试。

true
{
   "field1" : {
      "id" : "2",
      "foo" : "val",
      "bar" : 4
   },
   "field2" : {
      "$ref" : "2"
   }}

了解更多信息