Casting Object to LinkedHashMap - java

I have parsed a json string using ObjectMapper into Map<String, Object>. Now while I am debugging this map, I see it's effectively a multi-level LinkedHashMap.
So, I would like to convert this Map<String, Object> to Map<String, LinkedHashMap> using ObjectMapper. I have tried the following:
Map<String, LinkedHashMap> tempJsonMap = mapper.readValue((JsonParser) originalJsonMap,
new TypeReference<Map<String, LinkedHashMap>>(){});
Here in this above code, originalJsonMap is of type: Map<String, Object>.
But this code snippet is not working, I am getting error. In this context, I found a few examples to convert LinkedHashMap into a custom object but not the other way round.
Could anyone please help here?

Related

How to skip a map of map when deserialize from Json String to Object using object mapper

I have a map of map in my POJO which is always null. When i convert a json string to java object, then i am getting a deserialization error because of the map. So how can i skip the map while converting the json string to object
one of those will probably solve the problem:
you may define the field transient, like:
private transient Map<String, String> mapOfSomething;
(or) you can add #JsonIgnore annotation, like:
#JsonIgnore
private Map<String, String> mapOfAnotherThing;

How do you serialise Map<Number, Object> or Map<LocalDate, Object> into XML with Jackson?

It appears as if Jackson (2.9, current version as of writing) cannot serialize Java data structures like
Map<Integer, Object>
Map<LocalData, Object>
Map<Double, Object>
into valid XML. The issue is that a Map<K,V> is represented as
<Map>
<key1>
value1
</key1>
<key2>
value2
</key2>
</Map>
and XML tags are not allowed to start with numbers (this also excludes 2019-04-27).
How do I have to configure Jackson to serialise a Map or any DTO containing a Map into a valid XML?
(Note: a similar problem is an option issue https://github.com/FasterXML/jackson-dataformat-xml/issues/244 on since 2017).

Spring Boot Map with Map Key

I am trying to create a
Map<String,Map<Map<String,String>,String>> properties2
as a configurable property with Spring Boot properties file.
I have been previously been able to get a :
Map<String, Map<String,String>> properties
populated with
properties.[A].B=C
where A is the first key and B the second key with a value of C.
I have already tried
properties2.[A].[B=C]=D
which doesn't allow the later components to start although it doesn't throw errors
Does anyone know how I can populate the properties2 map correctly?
you can nest properties as following
private final Map<String, Map<String, Map<String, String>>> namespace = new HashMap<>();
namespace.[foo].[bar].a=alpha
May be you have error here Map,String>> properties2
as you you're using Map<String,String> as as key try this instead Map<String, Map<String, Map<String, String>>>

Read Map entity in JAX-RS client

I have a webservice which returns a "Map", I am trying to read this object from the Response(javax.ws.rs.core).
something like this:
ex : Map<String, Object> temp = response.readEntity(Map.class)
but this doesn't seem to work.
My question is how do I read a Map entity from a response object?
Found a way to read the Map entity from response.I guess I needed to provide the implementation class for Map.
response.readEntity(new GenericType<HashMap<String, Object>>() { });

How to access List inside Map in Thymeleaf?

Consider the following map,
Map<String, List<String>> map=new HashMap<>();
I would like to put values into this map using thymeleaf. Now, how to insert list items into the map in thymeleaf.
For, Map<String,String> we would write something like map['key']=val;
How to do it for lists?
One way, I thought was keeping the List<String> in a class. For example,
class ListWrapper
{
public List<String> list=new ArrayList<>();
// setter and getter methods
}
and then write the map as
Map<String, ListWrapper> map=new HashMap<>();
and insert like this..
map['key'].list[0]='item 1';
map['key'].list[1]='item 2';
Is there any direct way, instead of writing a class unnecessarily?
Thanks in advance.
You don't need to write a class - you can use Guava's ListMulitMap. Check out these examples.

Categories

Resources