[英]De-serialize Array of objects to JSON in MongoDB
I am using Morphia (ver 0.99) for my JSON to Pojo mapping to my MongoDB (ver 2.0). 我将Morphia(版本0.99)用于JSON到Pojo的映射到我的MongoDB(版本2.0)。 Streaming data between web-clients and my server works fine.
Web客户端和我的服务器之间的数据流工作正常。 But now I have a use-case where I don't know what pattern that is most appropriate.
但是现在我有一个用例,其中我不知道哪种模式最合适。 Can I use Morphia or MongoDB Java driver to achieve my requirements or do I need to use Jackson and JPA 2.2 notation.
我可以使用Morphia或MongoDB Java驱动程序来满足我的要求,还是需要使用Jackson和JPA 2.2表示法。
Here is my use-case; 这是我的用例;
Converting one Pojo is straight forward with Morphia, but how do I convert an array? 使用Morphia可以直接转换一个Pojo,但是如何转换数组?
return morph.toDBObject(obj).toString();
Is there a notation like @JsonIgnore
in Morphia to ignore conversions to and from JSON ? 在Morphia中是否有类似
@JsonIgnore
的符号来忽略与JSON之间的转换?
How can I most efficiently (without using more libraries if possible) to solve step three in in my use-case. 如何最有效地(如果可能,不使用更多库)解决用例中的第三步。 Convert ArrayList to JSON and ignore conversion of some of the Pojo properties?
将ArrayList转换为JSON并忽略某些Pojo属性的转换?
I've come up with a solution to my problem. 我想出了解决我问题的方法。 It's maybe not the most elegant but it works the way I want and I don't have to include other libraries (like Gson and Jackson) to de-serialize my array list of Pojo's to Json, I only used classes from the MongoDB Java driver and the Morphia API.
它可能不是最优雅的,但是它可以按照我想要的方式工作,我不必包括其他库(例如Gson和Jackson)来将我的Pojo数组列表反序列化为Json,我只使用了MongoDB Java驱动程序中的类和Morphia API。 I also added a simple parameter list to strip away unnecessary property value to be pushed to the client.
我还添加了一个简单的参数列表,以去除不必要的属性值以推送给客户端。
public static String deserializeToJSON(List<?> objList, String... removeAttributes) {
List<DBObject> dbObjList = new ArrayList<>(objList.size());
DBObject dbObj;
for(Object obj :objList){
dbObj = morph.toDBObject(obj);
for(int i=0; i < removeAttributes.length; i++){
debug("Removed DBObject filed: " +dbObj.removeField(removeAttributes[i]));
}
dbObjList.add(dbObj);
}
String json = JSON.serialize(dbObjList);
return json;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.