How to retrieve a POJO from MongoDB via a Generic Class - java

I'm new to Mongo, and am trying to use it to implement a cache internally in our application. We have a defined cache interface (public Cache(K, V) ) with several alternative implementations (HashTable, JCS etc). I need to create a MongoDB implementation for some hard (i.e. expensive) to calculate data. The cache data will either be a POJO or a List of POJO's.
My problem is getting the Mongo response back into a POJO or (the bit that's eluded me so far), into a List of POJOs.
Code so far:
public class MongoDBCache<K, V> implements Cache<K, V>
{
private String name = null;
public MongoDBCache(String name)
{
this.name = name;
}
public V get(K key)
{
V result = null;
try
{
DB mdb = getMongoDB();
DBCollection mcol = mdb.getCollection(name);
BasicDBObject query = new BasicDBObject("_id", key.toString());
DBCursor cursor = mcol.find(query);
if (cursor.hasNext())
{
Gson gson = new Gson();
DBObject dbobj = cursor.next();
Class type = ????;
result = (V) gson.fromJson(dbobj.get("obj").toString(), type);
}
}
catch (Exception e)
{
e.printStackTrace();
}
return result;
}
}
I can kind of make this work if the value is just a POJO (can get the type on the put method, not ideal but works), but I can't figure out how to make it work for a List. As an example, a ArrayList<Long> ends up as ArrayList<Double>.
Any suggestions? Something I can do in GSON? Something I can do with reflection? (I'm not tied into GSON, or any other library, this is just my current attempt).
Thanks,
Alan

If you could save also your class type into the database along with the object, you could infer the right class using something like:
if (cursor.hasNext())
{
Gson gson = new Gson();
DBObject dbobj = cursor.next();
Class type = Class.forName(dbobj.get("class").toString());
result = (V) gson.fromJson(dbobj.get("obj").toString(), type);
}
you can get the proper string with code like this:
SomeClass object = ...
Class c = object.getClass();
String cn = c.toString();
Edit
Pay attention that when you want to store on database the class type and you are dealing with generics, due to type erasure, you cannot do something like this:
ArrayList<Long> l = new ArrayList<Long>();
String clazz = l.getClass
since clazz will store:
java.util.ArrayList
instead you need to do something like this
Type listType = new TypeToken<ArrayList<Long>>() {}.getType();
String clazz = l.getClass();
that will return you:
java.util.ArrayList<java.lang.Long>

Related

GSON raises exception if object type is different

I am using following approach to parse feed into Java Objects.
val gsonBuilder = GsonBuilder()
val gson = gsonBuilder.create()
var homeModel: DataModel?=null
try {
homeModel = gson.fromJson(response, DataModel::class.java)
}catch (ex:java.lang.Exception){
}
This works fine if feed comes in the same format, but it type of some object changes it lands into exception block.
For example, feed some time provides "integers" instead of Object in "data"
#SerializedName("data")
#Expose
private List<MoreData> data = null;
I want to know if there is any possibility in GSON to set specific data to "null" if type does not match.
you need to change your data type of "data" with List<Object> for java or List<Any> for kotlin. probably you will get rid of exception.
#SerializedName("data")
#Expose
private List<Object> data = null;
but you will need to cast items to appropriate types while you are using.
For example:
val item:Int = homeModel[i] as Int //as yourDesiredType
However, if you want to set "data" null when data type is different, you can try:
val model = DataModel()
val json = Gson().toJson(model)
homeModel = Gson().fromJson(json, DataModel::class.java)
try {
if(!homeModel.data.isNullOrEmpty()){
homeModel.data.first() as String //as yourDesiredType
}
}
catch (ex:java.lang.Exception){
homeModel.data = null
}

Get jsonArray Using Native query entity manager

I have an service class as below:
public class RulesService {
#PersistenceContext
private EntityManager em;
public JSONArray getReportingTableData(String Query) {
List<Object> list = em.createNativeQuery(Query).getResultList();
return /*convert the above list as json array**/;
}
}
So, if the query is "select col1 as name,col2 as agefrom table1" then my jsonArray should be
[{"name":"test","age":"24"},{"name":"test1","age":"26"}]
I don't want to use pojo or entity class here, because the query will get change dynamically and there are many number of tables here, so I don't want to create seperate java class for each table.That is the reason am trying to make it as a JSONArray.
Can anyone please give me the right solution do it.
You could do that with Jackson's ObjectMapper. This tutorial is very interesting.
List<Object> list = em.createNativeQuery(Query).getResultList();
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(out, list);
final byte[] data = out.toByteArray();
System.out.println(new String(data));
you can use ObjectMapper to do dynamically.
public JSONArray getReportingTableData(String Query) {
List<Object> list = em.createNativeQuery(Query).getResultList();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
String arrayToJson = objectMapper.writeValueAsString(list);
JSONArray array = new JSONArray(arrayToJson);
return /*convert the above list as json array**/;
}
I guess you want to do like bellow. your query result is list of array. [["test","24"],["test2","26"]] and you want to convert it to key-value
List<Map<String,String>> result = listOfarray.stream()
.map(arr->{
Map<String,String> map = new HashMap<>();
map.put("name",arr[0]);
map.put("age",arr[1]);
return map;
})
.collect(Collectors.toList());
Sorry guys, I might be late but someone might find this interesting and save his/her day.
Reference
[https://gist.github.com/baxtheman/44fd1601380d415eeec53d9e6d5587dc][1]
public List<ObjectNode> getQuery(
Integer anno,
Integer settimana) {
Query q = em.createNativeQuery(
"NATIVE SQL....",Tuple.class);
List<Tuple> results = q.getResultList();
List<ObjectNode> json = _toJson(results);
return json;
}
private List<ObjectNode> _toJson(List<Tuple> results) {
List<ObjectNode> json = new ArrayList<ObjectNode>();
ObjectMapper mapper = new ObjectMapper();
for (Tuple t : results)
{
List<TupleElement<?>> cols = t.getElements();
ObjectNode one = mapper.createObjectNode();
for (TupleElement col : cols)
{
one.put(col.getAlias(), t.get(col.getAlias()).toString());
}
json.add(one);
}
return json;
}
Its a late answer but got it when i needed it.
The pretty easy and simple thing worked for me is
String[] columns = {"id","name","salary","phone","address", "dob"};
String query = "SELECT id,name,salary,phone,address,dob from users ";
List<Object[]> queryResp = em.createNativeQuery(query).getResultList();
List<Map<String,String>> dataList = new ArrayList<>();
for(Object[] obj : queryResp) {
Map<String,String> row = new HashMap<>(columns.length);
for(int i=0; i<columns.length; i++) {
if(obj[i]!=null)
row.put(columns[i], obj[i].toString());
else
row.put(columns[i], "");
}
dataList.add(row);
}
//Creating the ObjectMapper object
ObjectMapper mapper = new ObjectMapper();
//Converting the Object to JSONString
String jsonString = mapper.writeValueAsString(dataList);

org.JSONObject vs Map in Gson serialization [duplicate]

I have a POJO class as:
public class D{
private JSONObject profileData;
public JSONObject getProfileData ()
{
return profileData;
}
public void setProfileData (JSONObject profileData)
{
this.profileData = profileData;
}
}
Now I populate this class like:
for (int i =0; i<identities.size();i++){
D d = new D();
d.setProfileData(profileData);
dList.add(d);
}
I create JSON object for profileData from GSON using a HashMap:
profileDataInJson = new JSONObject(gson.toJson(map1));
Where the signature of profileDataInJson is: JSONObject profileDataInJson = null;
Now the resultant JSON is like:
"profileData":{"map":{"ioCinema":"firstValue","ioSIMAvailable":"firstKey","Name":"onePair"}}
Wherein I get an unwanted object called map inserted in my main profileData object.
However when I print this inside the loop I get
{`"ioCinema":"firstValue","ioSIMAvailable":"firstKey","Name":"onePair"}`
Whish is exactly what I want inside profileData object, without nesting the map object.
How do I solve this?
"I am already aware that I can achieve this by converting the type of profileData in D class from JSONObject to String, which will induce escape characters - However I am looking for a generic solution"
EDIT:
map1 is constructed in two ways, depending on user input and both ways are as follows:
if (args.length >= 4 && args[1].equalsIgnoreCase("onePair")) {
map1 = new HashMap<>();
String key1 = args[2];
String value1 = args[3];
map1.put(key1, value1);
profileDataInJson = new JSONObject(gson.toJson(map1));
}
And:
if (args.length >= 1 && args[0].equalsIgnoreCase("update")) {
if (args.length >= 2)
profileData.setName(args[1] != null ? args[1] : "");
if (args.length >= 3)
profileData.setSIMAvailable(args[2] != null ? args[2] : "");
profileDataInJson = new JSONObject(profileData);
}
Signature: ProfileData profileData = new ProfileData();
The thing which puzzles me is when I try to traverse profileData and try to fetch the json object by name "map" I get a nullPointer exception
You don't need to use Gson to convert hashmap to a json object.
Simply use:
profileDataInJson = new JSONObject(map);
Add custom serializer to Gson, so that Gson serialize the org JSON as expected by you.
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(JSONObject.class, new JsonSerializer<JSONObject>() {
#Override
public JsonElement serialize(final JSONObject src, final Type typeOfSrc,
final JsonSerializationContext context) {
return new JsonParser().parse(src.toString()).getAsJsonObject();
}
});
gsonBuilder.create().toJson(map1);
This will return {"ioCinema":"firstValue","ioSIMAvailable":"firstKey","Name":"onePair"}

Convertig Json to List

I am trying to read and write a Json object to my database and I'm not getting how to convert the string you get from the database to a string and how I can use it later tp write back the changed list into my database again.
So when I'm asking the database for the field I want, I get this string back:
["[\"pw1\",\"pw2\",\"pw3\"]"]
Then I go and create an object of my class LastPasswords
List<String> passwordList = (List<String>) controllerServlet.getMacroDatabaseManager().executeNativeQuery(queryGet);
LastPasswords lastPasswords = new LastPasswords(passwordList);
Gson gson = new Gson();
String Json = gson.toJson(lastPasswords);
Here is the LastPasswords class
public class LastPasswords {
private List<String> passwords;
public LastPasswords(List<String> passwords) {
this.passwords = passwords;
}
public List<String> getPasswords(){
return passwords;
}
public void setPasswords(List<String> passwords){
this.passwords = passwords;
}
}
Then when I have this json string I try to get it as a list but I don't get the list.
lastPasswords.setPasswords((List<String>) gson.fromJson(banana, LastPasswords.class));
passwordList = lastPasswords.getPasswords();
Thanks for helping.
You can use com.google.gson.reflect.TypeToken.TypeToken to define return type of fromJson method.
Then, àter you have the list, you can create a new LastPasswords to use.
String json = "[\"pw1\",\"pw2\",\"pw3\"]";
Gson gson = new Gson();
List<String> list = gson.fromJson(json, new TypeToken<List<String>>() {}.getType());
System.out.println(list.size());
System.out.println(list);
Output:
3
[pw1, pw2, pw3]
You may use TypeToken to load the json string into a custom object.
String json = "[\"pw1\",\"pw2\",\"pw3\"]";
List<String> list = gson.fromJson(json, new TypeToken<List<String>>(){}.getType());
Or for an list of list of String
String json = "[\"[\"pw1\",\"pw2\",\"pw3\"]\"]";
List<List<String>> list = gson.fromJson(json, new TypeToken<List<List<String>>>(){}.getType());
list.get(0).get(0) == "pw1"
I would like to recommend to keep a Password class instead of keeping a ListPassword class.
Let us assume, you've a Password class like this.
public class Password {
public String password;
// Getter and setter
}
Now when you read the json string using gson, you might have to do this.
Password[] passwordArray = gson.fromJson(json, Password[].class);
This will map the json string into an array of Password. Then you might consider populating them in a list if you like.
List<Password> passwordList = Arrays.asList(passwordArray);

How to maintain the order of a JSONObject

I am using a JSONObject in order to remove a certin attribute I don't need in a JSON String:
JSONObject jsonObject = new JSONObject(jsonString);
jsonObject.remove("owner");
jsonString = jsonObject.toString();
It works ok however the problem is that the JSONObject is "an unordered collection of name/value pairs" and I want to maintain the original order the String had before it went through the JSONObject manipulation.
Any idea how to do this?
try this
JSONObject jsonObject = new JSONObject(jsonString) {
/**
* changes the value of JSONObject.map to a LinkedHashMap in order to maintain
* order of keys.
*/
#Override
public JSONObject put(String key, Object value) throws JSONException {
try {
Field map = JSONObject.class.getDeclaredField("map");
map.setAccessible(true);
Object mapValue = map.get(this);
if (!(mapValue instanceof LinkedHashMap)) {
map.set(this, new LinkedHashMap<>());
}
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
return super.put(key, value);
}
};
jsonObject.remove("owner");
jsonString=jsonObject.toString();
You can't.
That is why we call it an unordered collection of name/value pairs.
Why you would need to do this, I'm not sure. But if you want ordering, you'll have to use a json array.
I have faced the same problem recently and just transitioned all our tests (which expect JSON attributes to be in the same order) to another JSON library:
<dependency>
<groupId>org.codehaus.jettison</groupId>
<artifactId>jettison</artifactId>
<version>1.3.5</version>
</dependency>
Internally it uses a LinkedHashMap, which maintains the order of attributes. This library is functionally equivalent to the json.org library, so I don't see any reason why not use it instead, at least for tests.
You can go for the JsonObject provided by the com.google.gson it is nearly the same with the JSONObject by org.json but some different functions.
For converting String to Json object and also maintains the order you can use:
Gson gson = new Gson();
JsonObject jsonObject = gson.fromJson(<Json String>, JsonObject.class);
For eg:-
String jsonString = "your json String";
JsonObject jsonObject = gson.fromJson(jsonString, JsonObject.class);
It just maintains the order of the JsonObject from the String.
If you can edit the server repose then change it to array of JSON objects.
JSON:
[
{PropertyName:"Date of Issue:",PropertyValue:"3/21/2011"},
PropertyName:"Report No:",PropertyValue:"2131196186"},{PropertyName:"Weight:",PropertyValue:"1.00"},
{PropertyName:"Report Type:",PropertyValue:"DG"}
]
And I handled it with JSONArray in client side (Android):
String tempresult="[{PropertyName:"Date of Issue:",PropertyValue:"3/21/2011"},PropertyName:"Report No:",PropertyValue:"2131196186"},PropertyName:"Weight:",PropertyValue:"1.00"},{PropertyName:"Report Type:",PropertyValue:"DG"}]"
JSONArray array = new JSONArray(tempresult);
for (int i = 0; i < array.length(); i++)
{
String key = array.getJSONObject(i).getString("PropertyName");
String value = array.getJSONObject(i).getString("PropertyValue");
rtnObject.put(key.trim(),value.trim()); //rtnObject is LinkedHashMap but can be any other object which can keep order.
}
You can use Jsckson library in case to maintain the order of Json keys.
It internally uses LinkedHashMap ( ordered ).
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
The code to remove a field, the removed JsonToken could itself be read if required.
String jsonString = "{\"name\":\"abc\",\"address\":\"add\",\"data\":[\"some 1\",\"some 2\",\"some3 3\"],\"age\":12,\"position\":8810.21}";
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(jsonString);
System.out.println("In original order:"+node.toString());
JsonToken removedToken = ((ObjectNode) node).remove("address").asToken();
System.out.println("Aft removal order:"+node.toString());
ObjectNode implementation uses a LinkedHashMap, which maintains the insertion order:
public ObjectNode(JsonNodeFactory nc) {
super(nc);
_children = new LinkedHashMap<String, JsonNode>();
}
Go on JSONObject class
Change from HashMap() to LinkedHashMap()
/**
* Construct an empty JSONObject.
*/
public JSONObject() {
this.map = new LinkedHashMap();
}
The LinkedHashMap class extends the Hashmap class. This class uses a doubly linked list containing all the entries of the hashed table, in the order in which the keys were inserted in the table: this allows the keys to be "ordered".
This is not easy, the main idea is to use LinkedHashMap, either pass in to the constructor (JSONObject(Map map)), or modify bytecode to handle the String parameter (JSONObject(String source)), which is the main use case. I got a solution in oson:
public static JSONObject getJSONObject(String source) {
try {
int lineNumberToReplace = 157;
ClassPool classPool = ClassPool.getDefault();
CtClass ctClass = classPool.get("org.json.JSONObject");
if (ctClass.isFrozen() || ctClass.isModified()) {
if (source == null) {
return new JSONObject();
} else {
return new JSONObject(source);
}
}
ctClass.stopPruning(true);
CtConstructor declaredConstructor = ctClass.getDeclaredConstructor(new CtClass[] {});
CodeAttribute codeAttribute = declaredConstructor.getMethodInfo().getCodeAttribute();
LineNumberAttribute lineNumberAttribute = (LineNumberAttribute)codeAttribute.getAttribute(LineNumberAttribute.tag);
// Index in bytecode array where the instruction starts
int startPc = lineNumberAttribute.toStartPc(lineNumberToReplace);
// Index in the bytecode array where the following instruction starts
int endPc = lineNumberAttribute.toStartPc(lineNumberToReplace+1);
// Let's now get the bytecode array
byte[] code = codeAttribute.getCode();
for (int i = startPc; i < endPc; i++) {
// change byte to a no operation code
code[i] = CodeAttribute.NOP;
}
declaredConstructor.insertAt(lineNumberToReplace, true, "$0.map = new java.util.LinkedHashMap();");
ctClass.writeFile();
if (source == null) {
return (JSONObject) ctClass.toClass().getConstructor().newInstance();
} else {
return (JSONObject) ctClass.toClass().getConstructor(String.class).newInstance(source);
}
} catch (Exception e) {
//e.printStackTrace();
}
if (source == null) {
return new JSONObject();
} else {
return new JSONObject(source);
}
}
need to include jar file from using mvn
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.1.GA</version>
</dependency>
From Android 20, JSONObject preserves the order as it uses LinkedHashMap to store namevaluepairs. Android 19 and below uses HashMap to store namevaluepairs. So, Android 19 and below doesn't preserve the order. If you are using 20 or above, don't worry, JSONObject will preserve the order. Or else, use JSONArray instead.
In JDK 8 and above, We can do it by using nashorn engine, supported in JDK 8.
Java 8 support to use js engine to evaluate:
String content = ..json content...
String name = "test";
String result = (String) engine.eval("var json = JSON.stringify("+content+");"
+ "var jsResult = JSON.parse(json);"
+ "jsResult.name = \"" + name + "\";"
+ "jsResult.version = \"1.0\";"
+ "JSON.stringify( jsResult );"
);
I was able to do this with help of classpath overriding.
created package package org.json.simple which is same as in jar and class named as JSONObject.
Took existing code from jar and updated the class by extending LinkedHashmap instead of Hashmap
by doing these 2 steps it will maintain the order, because preference of picking `JSONObject will be higher to pick from the new package created in step 1 than the jar.
I accomplished it by doing a:
JSONObject(json).put(key, ObjectMapper().writeValueAsString(ObjectMapper().readValue(string, whatever::class)))
So essentially I deserialize a string to an ordered class, then I serialize it again. But then I also had to format that string afterwards to remove escapes.
.replace("\\\"", "\"").replace("\"{", "{").replace("}\"", "}")
You may also have to replace null items as well if you don't want nulls.

Categories

Resources