I am using Morphia (ver 0.99) for my JSON to Pojo mapping to my MongoDB (ver 2.0). Streaming data between web-clients and my server works fine. 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.
Here is my use-case;
Converting one Pojo is straight forward with Morphia, but how do I convert an array?
return morph.toDBObject(obj).toString();
Is there a notation like @JsonIgnore
in Morphia to ignore conversions to and from 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?
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. 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;
}