diff options
Diffstat (limited to 'src/main/java/org/openslx/util/Json.java')
-rw-r--r-- | src/main/java/org/openslx/util/Json.java | 23 |
1 files changed, 17 insertions, 6 deletions
diff --git a/src/main/java/org/openslx/util/Json.java b/src/main/java/org/openslx/util/Json.java index d9fb659..5e5fbcb 100644 --- a/src/main/java/org/openslx/util/Json.java +++ b/src/main/java/org/openslx/util/Json.java @@ -8,7 +8,8 @@ import java.util.ArrayList; import java.util.List; import java.util.concurrent.atomic.AtomicReference; -import org.apache.log4j.Logger; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.apache.thrift.TBase; import com.google.gson.Gson; @@ -25,7 +26,7 @@ import com.google.gson.JsonSyntaxException; public class Json { - private static final Logger LOGGER = Logger.getLogger(Json.class); + private static final Logger LOGGER = LogManager.getLogger(Json.class); /** * Global static instance. The Gson object is thread-safe. @@ -47,12 +48,18 @@ public class Json { String upperName = field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1); try { + Method getter; + try { + getter = thriftClass.getMethod( "get" + upperName ); + } catch (NoSuchMethodException e) { + getter = thriftClass.getMethod( "is" + upperName ); + } fields.add( new ThriftField( field, - thriftClass.getMethod( "get" + upperName ), + getter, thriftClass.getMethod( "set" + upperName, field.getType() ), thriftClass.getMethod( "isSet" + upperName) ) ); } catch (NoSuchMethodException e) { - // Not a thrift field, apparently + LOGGER.warn( "Nein", e ); } } synchronized ( Json.class ) { @@ -115,7 +122,9 @@ public class Json { throws JsonParseException { if (!(json instanceof JsonObject)) throw new JsonParseException("Need a json object, have " + json.getClass().getSimpleName()); + // We're deserializing a json object {..} here JsonObject obj = (JsonObject) json; + // Create the Thrift object we want to deserialize into final T inst; try { inst = clazz.newInstance(); @@ -123,12 +132,13 @@ public class Json { LOGGER.warn("Could not deserialize to class " + clazz.getName(), e); throw new JsonParseException("Cannot instantiate class " + clazz.getSimpleName()); } + // Iterate over all fields in the Thrift object for (ThriftField field : fields) { JsonElement element = obj.get(field.field.getName()); if (element == null || element.isJsonNull()) continue; try { - field.setter.invoke(inst, context.deserialize(element, field.field.getType())); + field.setter.invoke(inst, new Object[] { context.deserialize(element, field.field.getType()) }); } catch (Exception e) { LOGGER.warn("Could not call " + field.setter.getName() + " on " + clazz.getSimpleName(), e); } @@ -142,7 +152,8 @@ public class Json { JsonObject o = new JsonObject(); for ( ThriftField thrift : fields ) { try { - if ( !(Boolean)thrift.isset.invoke( thriftClass ) ) + Object ret = thrift.isset.invoke( thriftClass ); + if ( !(ret instanceof Boolean) || !(Boolean)ret ) continue; Object value = thrift.getter.invoke( thriftClass ); if ( value == null ) |