Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
421 views
in Technique[技术] by (71.8m points)

java - Casting LinkedHashMap to Complex Object

I've got an application that stores some data in DynamoDB using Jackson to marshall my complex object into a JSON.

For example the object I'm marshalling might look like this:

private String aString;
private List<SomeObject> someObjectList;

Where SomeObject might look like this:

private int anInteger;
private SomeOtherObject;

and SomeOtherObject might look like this:

private long aLong;
private float aFloat; 

This is fine an the object gets marshalled no problem and stored in the DB as a JSON string.

When it comes time to retrieve the data from DynamoDB Jackson automatically retrieves the JSON and converts it back... EXCEPT that 'someObjectList' is returned as a List<LinkedHashMap> not as a List<SomeObject>! This is standard behaviour for Jackson, its not a mistake that this is happening.

So now this leads to a problem. My code base thinks its dealing with a List<SomeObject> but the reality is that its handling a List<LinkedHashMap>! My question is how do I get my LinkedHashMap back into a 'SomeObject'. Obviously this is a manual process but what I mean is I can't even extract the values.

If I do this:

for (LinkedHashMap lhm : someObjectList) {
    // Convert the values back
}

I get a compile error telling me that someObjectList is of type 'SomeObject' not LinkedHashMap.

If I do this:

for (SomeObject lhm : someObjectList) {
    // Convert the values back
}

I get a runtime error telling me that LinkedHashMap cannot be cast to 'SomeObject'.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use ObjectMapper.convertValue(), either value by value or even for the whole list. But you need to know the type to convert to:

POJO pojo = mapper.convertValue(singleObject, POJO.class);
// or:
List<POJO> pojos = mapper.convertValue(listOfObjects, new TypeReference<List<POJO>>() { });

this is functionally same as if you did:

byte[] json = mapper.writeValueAsBytes(singleObject);
POJO pojo = mapper.readValue(json, POJO.class);

but avoids actual serialization of data as JSON, instead using an in-memory event sequence as the intermediate step.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...