开发者

Which Java JSON libraries make good reuse of JAXB annotations? [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations 开发者_运维问答for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 1 year ago.

Improve this question

I am developing a project that already uses XML serialization, so I need an elegant solution to support JSON, by reusing the JAXB annotations.

Can anyone recommend some Java JSON libraries that makes a good reuse of JAXB annotations? Lightweight libraries are preferred.


Note: I'm the EclipseLink JAXB (MOXy) lead, and a member of the JAXB (JSR-222) expert group.

Check out the JSON binding being added to EclipseLink JAXB (MOXy). Not only do we leverage the JAXB annotations we also leverage the runtime APIs:

package blog.json.twitter;

import java.util.Date;    
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(SearchResults.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        unmarshaller.setProperty("eclipselink.media.type", "application/json");
        StreamSource source = new StreamSource("http://search.twitter.com/search.json?q=jaxb");
        JAXBElement<SearchResults> jaxbElement = unmarshaller.unmarshal(source, SearchResults.class);

        Result result = new Result();
        result.setCreatedAt(new Date());
        result.setFromUser("bdoughan");
        result.setText("You can now use EclipseLink JAXB (MOXy) with JSON :)");
        jaxbElement.getValue().getResults().add(result);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty("eclipselink.media.type", "application/json");
        marshaller.marshal(jaxbElement, System.out);
    }

}
  • http://blog.bdoughan.com/2011/08/json-binding-with-eclipselink-moxy.html

In addition to the JAXB annotations MOXy extensions (such as @XmlPath) are supported making it even easier to have one annotated model that can be used for both XML and JSON:

  • http://blog.bdoughan.com/2011/08/binding-to-json-xml-geocode-example.html?m=0


I'd Use Jackson. It seems to have good JAXB support out of the box.

Reference:

  • Using JAXB annotations with Jackson
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜