LocalDate being converted to LinkedHashMap

23 views
Skip to first unread message

gurpal2000

unread,
Jul 6, 2019, 4:20:14 AM7/6/19
to jackson-user
I have a pojo that includes a LocalDate (with a year, month, day populated only)

I then attempt to convert the member variables and object values to a Map<String, Object> using this:

Map<String, Object> map = objectMapper.convertValue(obj, new TypeReference<Map<String, Object>>() {});

In debug i'm seeing that my Map contains a LinkedHashMap for the LocalDate!

I need to pass this to a JDBC prepared statement and obviously LinkedHashMap makes no sense to it.

How do I get the Object Mapper to skip all instances of LocalDate or a specific field? I'm using spring boot 2.1.4?

If I use something like this function which uses reflection all is well. But i'd rather use ObjectMapper if possible.

public Map<String, Object> pojoToMap(Object obj) {
Map<String, Object> hashMap = new HashMap<String, Object>();
try {
Class<? extends Object> c = obj.getClass();
Method m[] = c.getMethods();
for (int i = 0; i < m.length; i++) {
if (m[i].getName().indexOf("get") == 0) {
String name = m[i].getName().toLowerCase().substring(3, 4) + m[i].getName().substring(4);
hashMap.put(name, m[i].invoke(obj, new Object[0]));
hashMap.remove("class"); // remove class
}
}
}
catch (Throwable e) {
// TODO: log error
}
return hashMap;
}

thanks

Johannes Paul

unread,
Jul 8, 2019, 1:19:58 AM7/8/19
to jackson-user
To answer your question directly:
To skip a value just annotate it with "@JsonIgnore".

But i guess you want good values, instead of skipping them.
To get a good LocalDate from Jackson, confige the ObjectMapper with the following feature:
"objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);"
and then annotate the LocalDate property with:
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone = "UTC")

This should give you a good LocalDate Value.

Tatu Saloranta

unread,
Jul 18, 2019, 1:32:59 AM7/18/19
to jackson-user
On Sat, Jul 6, 2019 at 1:20 AM gurpal2000 <gurpal...@gmail.com> wrote:
>
> I have a pojo that includes a LocalDate (with a year, month, day populated only)
>
> I then attempt to convert the member variables and object values to a Map<String, Object> using this:
>
> Map<String, Object> map = objectMapper.convertValue(obj, new TypeReference<Map<String, Object>>() {});
>
>
> In debug i'm seeing that my Map contains a LinkedHashMap for the LocalDate!

Yes, this occurs since `convertValue()` is similar to first
serializing contents into json (although without writing as json), and
then reading it back into target type.
And since type is `Map<String, Object>`, there is nothing to guide
second part to any other types than basic wrappers, so JSON Object
becomes a `Map`.

Now: aside from this, if you simply wanted to exclude value of type
`LocalDate`, you could use a relatively new feature, config overrides
to do equivalent of adding `@JsonIgnoreType` (you could actually apply
it via mix-ins to `LocalDate` too...):

ObjectMapper mapper = objectMapper();
mapper.configOverride(LocalDate.class).setIsIgnoredType(true);

and then any `LocalDate`-value properties should be ignored when serializing.

I hope this helps,

-+ Tatu +-

>
>
> I need to pass this to a JDBC prepared statement and obviously LinkedHashMap makes no sense to it.
>
>
> How do I get the Object Mapper to skip all instances of LocalDate or a specific field? I'm using spring boot 2.1.4?
>
>
> If I use something like this function which uses reflection all is well. But i'd rather use ObjectMapper if possible.
>
>
> public Map<String, Object> pojoToMap(Object obj) {
> Map<String, Object> hashMap = new HashMap<String, Object>();
> try {
> Class<? extends Object> c = obj.getClass();
> Method m[] = c.getMethods();
> for (int i = 0; i < m.length; i++) {
> if (m[i].getName().indexOf("get") == 0) {
> String name = m[i].getName().toLowerCase().substring(3, 4) + m[i].getName().substring(4);
> hashMap.put(name, m[i].invoke(obj, new Object[0]));
> hashMap.remove("class"); // remove class
> }
> }
> }
> catch (Throwable e) {
> // TODO: log error
> }
> return hashMap;
> }
>
>
> thanks
>
> --
> You received this message because you are subscribed to the Google Groups "jackson-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user...@googlegroups.com.
> To post to this group, send email to jackso...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/jackson-user/7cb74ef3-026e-4d34-bd17-1ea0ac11b298%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages