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