问题描述
我有一个应用程序使用Jackson将一些数据存储在DynamoDB中,以将我的复杂对象转换为JSON。
例如,我编组的对象可能如下所示:
private String aString;
private List< SomeObject> someObjectList;
WhereObject可能如下所示:
private int anInteger;
private SomeOtherObject;
,SomeOtherObject可能如下所示:
private long aLong;
private float aFloat;
这是一个很好的对象获取编组没有问题,并作为JSON字符串存储在DB。 / p>
当到达从DynamoDB检索数据的时间Jackson自动检索JSON并将其转换回来... EXCEPT'someObjectList'作为 List< LinkedHashMap>
不是 List< SomeObject>
!这是杰克逊的标准行为,它不是一个错误,这是发生。
现在这导致一个问题。我的代码库认为它处理一个 List< SomeObject>
但现实是它处理一个 List< LinkedHashMap>
!我的问题是如何让我的LinkedHashMap回一个SomeObject。显然这是一个手动过程,但是我的意思是我不能提取值。
如果我这样做:
for(LinkedHashMap lhm:someObjectList){
pre>
//将值转换回
}
我收到一个编译错误,告诉我someObjectList的类型是SomeObject,不是LinkedHashMap。
我这样做:
for(someObject lhm:someObjectList){
//将值转换回
}
我得到一个运行时错误告诉我LinkedHashMap不能转换为SomeObject。解决方案您可以使用
ObjectMapper.convertValue()
为整个列表。但你需要知道类型转换为:POJO pojo = mapper.convertValue(singleObject,POJO.class);
//或:
List< POJO> pojos = mapper.convertValue(listOfObjects,new TypeReference< List< POJO>(){});
这与功能相同:
byte [] json = mapper.writeValueAsBytes(singleObject);
POJO pojo = mapper.readValue(json,POJO.class);
但是避免将数据实际序列化为JSON,而是使用内存中事件序列作为中间步骤。
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 aList<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 aList<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'.
解决方案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.
这篇关于将LinkedHashMap转换为复杂对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!