On Sat, Jul 6, 2019 at 1:20 AM gurpal2000 <[email protected]> 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 [email protected].
> To post to this group, send email to [email protected].
> 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.
--
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/jackson-user/CAL4a10iZO4WCSD0ZC7GARqBd58ErPwbxucZZMhkwBJ1cO%3DSLJw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.