Note that there are some explanatory texts on larger screens.

plurals
  1. POJackson De/Serializing Date-to-String-to-Date in generic Maps
    primarykey
    data
    text
    <p>There are many examples of Jackson to/from java.util.Date code but they all seem to leverage POJO annotation. I have generic Maps of scalars that I wish to de/serialize to JSON. This is the current deserializer setup; very simple:</p> <pre><code>public class JSONUtils { static { DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"); mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS, true); mapper.setDateFormat(df); // this works for outbounds but has no effect on inbounds mapper.getDeserializationConfig().with(df); // Gave this a shot but still does not sniff strings for a format that we declare should be treated as java.util.Date } public static Map&lt;String,Object&gt; parseJSON(InputStream is) { Map&lt;String,Object&gt; data = null; try { data = mapper.readValue(is, Map.class); } catch(Exception e) { // ... } return data; } </code></pre> <p>I grok that a dateserializer can turn java.util.Date into a ISO 8601-ish string. It's going the other way that puzzles me. Clearly, in a JSON doc with no context, a string is a string so I cannot know if it was once a date. So I am prepared to duck type this and examine all strings being deserialized and if they smell like YYYY-MM-DDTHH:MM:SS.sss datetimes, then I will make a java.util.Date instead of just passing back a String. So given:</p> <pre><code>{ "name": "buzz", "theDate": "2013-09-10T12:00:00.000" } </code></pre> <p>will yield</p> <pre><code>Map&lt;String,Object&gt; m = mapper.readValue(is, Map.class); Object o1 = m.get("name"); // o1 is instanceof String Object o2 = m.get("theDate"); // o2 is instanceof Date </code></pre> <p>But this means that the deserializer has to return two different types and I have not been able to figure out how to do this in Jackson. Does anyone know of a good, compact example that will sniff for date-like strings and turn them into Dates, leaving others as Strings?</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.
 

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