public void visitArrayField(FieldAttributes f, Type typeOfF, Object obj) { try { if (!json.isJsonObject()) { throw new JsonParseException("Expecting object found: " + json); } JsonObject jsonObject = json.getAsJsonObject(); String fName = getFieldName(f); JsonArray jsonChild = (JsonArray) jsonObject.get(fName); if (jsonChild != null) { Object array = visitChildAsArray(typeOfF, jsonChild); f.set(obj, array); } else { f.set(obj, null); } } catch (IllegalAccessException e) { throw new RuntimeException(e); } }
public void visitObjectField(FieldAttributes f, Type typeOfF, Object obj) { try { if (!json.isJsonObject()) { throw new JsonParseException("Expecting object found: " + json); } JsonObject jsonObject = json.getAsJsonObject(); String fName = getFieldName(f); JsonElement jsonChild = jsonObject.get(fName); if (jsonChild != null) { Object child = visitChildAsObject(typeOfF, jsonChild); f.set(obj, child); } else { f.set(obj, null); } } catch (IllegalAccessException e) { throw new RuntimeException(e); } }
public boolean visitFieldUsingCustomHandler(FieldAttributes f, Type declaredTypeOfField, Object parent) { try { String fName = getFieldName(f); if (!json.isJsonObject()) { throw new JsonParseException("Expecting object found: " + json); } JsonElement child = json.getAsJsonObject().get(fName); boolean isPrimitive = Primitives.isPrimitive(declaredTypeOfField); if (child == null) { // Child will be null if the field wasn't present in Json return true; } else if (child.isJsonNull()) { if (!isPrimitive) { f.set(parent, null); } return true; } ObjectTypePair objTypePair = new ObjectTypePair(null, declaredTypeOfField, false, false); Pair<JsonDeserializer<?>, ObjectTypePair> pair = objTypePair.getMatchingHandler(deserializers); if (pair == null) { return false; } Object value = invokeCustomDeserializer(child, pair); if (value != null || !isPrimitive) { f.set(parent, value); } return true; } catch (IllegalAccessException e) { throw new RuntimeException(); } }