Made JSONObjectInputStream more robust

This commit is contained in:
Ziver Koc 2021-08-29 01:02:47 +02:00
parent 805ee6afc5
commit 12b0fb219e

View file

@ -30,10 +30,7 @@ import zutil.parser.Base64Decoder;
import zutil.parser.DataNode;
import java.io.*;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.lang.reflect.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -241,14 +238,19 @@ public class JSONObjectInputStream extends InputStream implements ObjectInput, C
// Date and time objects
if (Date.class.isAssignableFrom(objClass)) {
if (json.getString("timestamp") != null) {
obj = new Date(json.getLong("timestamp"));
}
}
else if (Calendar.class.isAssignableFrom(objClass)) {
if (json.getString("timestamp") != null) {
obj = Calendar.getInstance();
((Calendar) obj).setTimeInMillis(json.getLong("timestamp"));
}
}
// Instantiate generic object
else{
try {
obj = objClass.getDeclaredConstructor().newInstance();
// Read all fields from the new object instance
@ -266,6 +268,9 @@ public class JSONObjectInputStream extends InputStream implements ObjectInput, C
json.get(field.getName())));
}
}
} catch (Exception e) {
logger.warning("Unable to instantiate object(" + key + "): " + e.getMessage());
}
}
// Add object to the cache
if (json.getString(MD_OBJECT_ID) != null)