Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a basic example on how to use Jackson to serialize deserialize a date from an object </p> <p>public class JacksonSetup {</p> <pre><code>private static class JacksonSerializer { private static JacksonSerializer instance; private JacksonSerializer() { } public static JacksonSerializer getInstance() { if (instance == null) instance = new JacksonSerializer(); return instance; } public &lt;E extends ModelObject&gt; void writeTo(E object, Class&lt;E&gt; type, OutputStream out) throws IOException { ObjectMapper mapper = getMapper(); mapper.writeValue(out, object); } public &lt;E extends ModelObject&gt; void writeTo(E object, Class&lt;E&gt; type, Writer out) throws IOException { ObjectMapper mapper = getMapper(); mapper.writeValue(out, object); } public &lt;E extends ModelObject&gt; E read(String input, Class&lt;E&gt; type) throws IOException { ObjectMapper mapper = getMapper(); E result = (E) mapper.readValue(input, type); return result; } private ObjectMapper getMapper() { ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(mapper.getTypeFactory()); mapper.setAnnotationIntrospector(introspector); return mapper; } } private static class JaxbDateSerializer extends XmlAdapter&lt;String, Date&gt; { private SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy"); @Override public String marshal(Date date) throws Exception { return dateFormat.format(date); } @Override public Date unmarshal(String date) throws Exception { return dateFormat.parse(date); } } private static abstract class ModelObject { } private static class Person extends ModelObject { private String name; private Date bday; public String getName() { return name; } public void setName(String name) { this.name = name; } @XmlElement(name = "birth-day") @XmlJavaTypeAdapter(JaxbDateSerializer.class) public Date getBday() { return bday; } public void setBday(Date bday) { this.bday = bday; } } public static void main(String[] args) { try { Person person = new Person(); person.setName("Jhon Doe"); person.setBday(new Date()); Writer writer = new StringWriter(); JacksonSerializer.getInstance().writeTo(person, Person.class, writer); System.out.println(writer.toString()); } catch (Exception e) { e.printStackTrace(); } } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload