Skip to content

Instantly share code, notes, and snippets.

@Anticom
Created December 15, 2016 15:10
Show Gist options
  • Save Anticom/6d773a4045e0559e4280bab68e358ab0 to your computer and use it in GitHub Desktop.
Save Anticom/6d773a4045e0559e4280bab68e358ab0 to your computer and use it in GitHub Desktop.
package eu.anticom.model.opensearch;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import eu.anticom.serializer.ContactDeserializer;
import eu.anticom.serializer.ContactSerializer;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import java.io.UnsupportedEncodingException;
@JsonSerialize(using = ContactSerializer.class)
@JsonDeserialize(using = ContactDeserializer.class)
public class Contact extends InternetAddress {
public Contact() {
super();
}
public Contact(String address) throws AddressException {
super(address);
}
public Contact(String address, boolean strict) throws AddressException {
super(address, strict);
}
public Contact(String address, String personal) throws UnsupportedEncodingException {
super(address, personal);
}
public Contact(String address, String personal, String charset) throws UnsupportedEncodingException {
super(address, personal, charset);
}
}
package eu.anticom.serializer;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import javax.mail.internet.InternetAddress;
import java.io.IOException;
public class ContactSerializer extends StdSerializer<InternetAddress> {
public ContactSerializer() {
this(null);
}
public ContactSerializer(Class<InternetAddress> t) {
super(t);
}
@Override
public void serialize(InternetAddress internetAddress, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField("foo", internetAddress.toString());
jsonGenerator.writeEndObject();
}
}
<!-- expected -->
<MyRootNode>
<Contact>[email protected]</Contact>
</MyRootNode>
<!-- actual -->
<MyRootNode>
<Contact>
<foo>[email protected]</foo>
</Contact>
</MyRootNode>
@JacksonXmlRootElement(localName = "MyRootNode", namespace = NS)
public class MySchema {
public static final String NS = "http://a9.com/-/spec/opensearch/1.1/";
@JacksonXmlProperty(localName = "Contact", namespace = NS)
private Contact contact;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment