Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change programmatically the default JAXB date serialization?

Tags:

java

xml

jaxb

Is there a way to change the default way jaxb serialize/deserialize types, dates in my case, without specifying it through annotation and/or through xml jaxb binding as mentioned here http://jaxb.java.net/guide/Using_different_datatypes.html

I'd basically like to do something like:

    JAXBContext jaxbContext = ...;
    Marshaller marshaller = jaxbContext.createMarshaller().setAdapter(new DateAdapter(dateFormat));

To have a preconfigured JaxBContext or Marshaller/Unmarshaller that serialize/deserialize dates in a customized way..

Couldn't find any resource that shows how to do expect through annotations or statically with the xml binding file.. Thanks!

like image 376
zhk Avatar asked Jun 16 '11 20:06

zhk


People also ask

How does JAXB marshalling work?

In JAXB, marshalling involves parsing an XML content object tree and writing out an XML document that is an accurate representation of the original XML document, and is valid with respect the source schema. JAXB can marshal XML data to XML documents, SAX content handlers, and DOM nodes.

What is JAXB marshalling?

JAXB stands for Java Architecture for XML Binding. It provides mechanism to marshal (write) java objects into XML and unmarshal (read) XML into object. Simply, you can say it is used to convert java object into xml and vice-versa.

Is JAXB Unmarshaller thread safe?

All other objects, including Marshaller and Unmarshaller, are not thread-safe and must not be shared. The static helper methods in the JAXB class can be used from several threads, of course. In practice, this means that if you need a JAXBContext instance, you should probably store in a static member.

What is Marshaller in Java?

The Marshaller class is responsible for governing the process of serializing Java content trees back into XML data.


2 Answers

This isn't exactly what you're looking for but it beats annotating every Date field individually. You can set a XmlJavaTypeAdapter at the package level so that every reference to Date within your package will use it. If your objects are in the com.example package, you should add a package-info.java file to it with the following contents:

@XmlJavaTypeAdapter(value=MyCustomDateAdapter.class,type=Date.class)
package com.example;
like image 105
Patrick Marchwiak Avatar answered Oct 21 '22 11:10

Patrick Marchwiak


Try this:

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlRootElement(name = "event")
public class Event {

    private Date date;
    private String description;

    @XmlJavaTypeAdapter(DateFormatterAdapter.class)
    public Date getDate() {
        return date;
    }

    public void setDate(final Date date) {
        this.date = date;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(final String description) {
        this.description = description;
    }

    private static class DateFormatterAdapter extends XmlAdapter<String, Date> {
        private final SimpleDateFormat dateFormat = new SimpleDateFormat("dd_mm_yyyy");

        @Override
        public Date unmarshal(final String v) throws Exception {
            return dateFormat.parse(v);
        }

        @Override
        public String marshal(final Date v) throws Exception {
            return dateFormat.format(v);
        }
    }

    public static void main(final String[] args) throws Exception {
        final JAXBContext context = JAXBContext.newInstance(Event.class);
        final Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        final Event event = new Event();
        event.setDate(new Date());
        event.setDescription("im rick james");

        marshaller.marshal(event, System.out);
    }
}

This produces:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<event>
    <date>16_05_2011</date>
    <description>im rick james</description>
</event>
like image 32
chahuistle Avatar answered Oct 21 '22 09:10

chahuistle