Fix up some null values
This commit is contained in:
parent
a28edce3e5
commit
665c7e920f
3 changed files with 25 additions and 22 deletions
|
@ -8,54 +8,55 @@ import java.util.Map;
|
|||
|
||||
/**
|
||||
* Deserializes objects!
|
||||
*
|
||||
* <p>
|
||||
* YOU MUST HAVE A NO-PARAMETER CONSTRUCTOR
|
||||
*/
|
||||
public class DynamicDeserializer
|
||||
{
|
||||
public class DynamicDeserializer {
|
||||
/**
|
||||
* Constructs and deserializes an object from serialized data
|
||||
*/
|
||||
public static <T> T doDeserialize(Class<T> clazz, byte[] data) throws Exception {
|
||||
public static <T> T doDeserialize ( Class<T> clazz , byte[] data ) throws Exception {
|
||||
ByteArrayInputStream BAIS = new ByteArrayInputStream ( data );
|
||||
return deserialize( ( Map<String, Object> ) JsonObject.parseJSON ( BAIS ).getMap() , clazz);
|
||||
return deserialize ( ( Map<String, Object> ) JsonObject.parseJSON ( BAIS ).getMap ( ) , clazz );
|
||||
}
|
||||
|
||||
private static <T> T deserialize(Map<String, Object> map, Class<T> clazz) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
|
||||
if(!clazz.isAnnotationPresent ( DynSerial.class ))return null;
|
||||
private static <T> T deserialize ( Map<String, Object> map , Class<T> clazz ) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
|
||||
if ( ! clazz.isAnnotationPresent ( DynSerial.class ) )
|
||||
return null;
|
||||
|
||||
T object = clazz.getDeclaredConstructor ( ).newInstance ( );
|
||||
T object = clazz.getDeclaredConstructor ( ).newInstance ( );
|
||||
|
||||
Field[] fields = clazz.getDeclaredFields ();
|
||||
Field[] fields = clazz.getDeclaredFields ( );
|
||||
for (
|
||||
Field field :
|
||||
fields
|
||||
) {
|
||||
field.setAccessible ( true );
|
||||
|
||||
if ( field.isAnnotationPresent ( IgnoreSerialization.class ) )
|
||||
continue;
|
||||
|
||||
try{
|
||||
try {
|
||||
|
||||
if( !( field.getType ().isAnnotationPresent ( DynSerial.class ) ))
|
||||
{
|
||||
field.set ( object, map.get ( field.getName () ) );
|
||||
}else {
|
||||
Object tmp = deserialize ( (Map<String, Object> ) map.get ( field.getName () ), field.getType ());
|
||||
field.set ( object, tmp );
|
||||
if ( ! ( field.getType ( ).isAnnotationPresent ( DynSerial.class ) ) ) {
|
||||
field.set ( object , map.get ( field.getName ( ) ) );
|
||||
}
|
||||
}catch(Exception e){
|
||||
else {
|
||||
Object tmp = deserialize ( ( Map<String, Object> ) map.get ( field.getName ( ) ) , field.getType ( ) );
|
||||
field.set ( object , tmp );
|
||||
}
|
||||
} catch ( Exception e ) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Method[] mth = clazz.getDeclaredMethods ();
|
||||
Method[] mth = clazz.getDeclaredMethods ( );
|
||||
for (
|
||||
Method mt :
|
||||
mth
|
||||
) {
|
||||
if(mt.isAnnotationPresent ( Completed.class ))
|
||||
{
|
||||
mt.invoke ( object, true );
|
||||
if ( mt.isAnnotationPresent ( Completed.class ) ) {
|
||||
mt.invoke ( object , true );
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -48,8 +48,9 @@ public class DynamicSerializer {
|
|||
field.setAccessible ( true );
|
||||
if ( field.isAnnotationPresent ( IgnoreSerialization.class ) )
|
||||
continue;
|
||||
|
||||
Object fieldVal = field.get ( inst );
|
||||
if(fieldVal == null)continue;
|
||||
if ( fieldVal == null ) continue;
|
||||
|
||||
String fieldName = field.getName ( );
|
||||
|
||||
|
|
|
@ -77,6 +77,7 @@ public class JsonObject {
|
|||
}
|
||||
|
||||
private String escape(String str) {
|
||||
if(str == null)return "";
|
||||
// Add necessary escape characters (e.g., double quotes, backslashes)
|
||||
// You can implement this method based on your specific requirements.
|
||||
// This is a simplified version for demonstration purposes.
|
||||
|
|
Loading…
Reference in a new issue