private void addAsArrayElement(ObjectTypePair elementTypePair) { if (elementTypePair.getObject() == null) { root.getAsJsonArray().add(JsonNull.createJsonNull()); } else { JsonElement childElement = getJsonElementForChild(elementTypePair); root.getAsJsonArray().add(childElement); } }
public void visitArrayField(FieldAttributes f, Type typeOfF, Object obj) { try { if (isFieldNull(f, obj)) { if (serializeNulls) { addChildAsElement(f, JsonNull.createJsonNull()); } } else { Object array = getFieldValue(f, obj); addAsChildOfObject(f, new ObjectTypePair(array, typeOfF, false)); } } catch (CircularReferenceException e) { throw e.createDetailedException(f); } }
/** objTypePair.getObject() must not be null */ @SuppressWarnings({"unchecked", "rawtypes"}) private JsonElement findAndInvokeCustomSerializer(ObjectTypePair objTypePair) { Pair<JsonSerializer<?>, ObjectTypePair> pair = objTypePair.getMatchingHandler(serializers); if (pair == null) { return null; } JsonSerializer serializer = pair.first; objTypePair = pair.second; start(objTypePair); try { JsonElement element = serializer.serialize(objTypePair.getObject(), objTypePair.getType(), context); return element == null ? JsonNull.createJsonNull() : element; } finally { end(objTypePair); } }
public void visitObjectField(FieldAttributes f, Type typeOfF, Object obj) { try { if (isFieldNull(f, obj)) { if (serializeNulls) { addChildAsElement(f, JsonNull.createJsonNull()); } } else { Object fieldValue = getFieldValue(f, obj); // we should not get more specific component type yet since it is // possible that a custom // serializer is registered for the componentType addAsChildOfObject(f, new ObjectTypePair(fieldValue, typeOfF, false)); } } catch (CircularReferenceException e) { throw e.createDetailedException(f); } }
public boolean visitUsingCustomHandler(ObjectTypePair objTypePair) { try { Object obj = objTypePair.getObject(); if (obj == null) { if (serializeNulls) { assignToRoot(JsonNull.createJsonNull()); } return true; } JsonElement element = findAndInvokeCustomSerializer(objTypePair); if (element != null) { assignToRoot(element); return true; } return false; } catch (CircularReferenceException e) { throw e.createDetailedException(null); } }
public boolean visitFieldUsingCustomHandler( FieldAttributes f, Type declaredTypeOfField, Object parent) { try { Preconditions.checkState(root.isJsonObject()); Object obj = f.get(parent); if (obj == null) { if (serializeNulls) { addChildAsElement(f, JsonNull.createJsonNull()); } return true; } ObjectTypePair objTypePair = new ObjectTypePair(obj, declaredTypeOfField, false); JsonElement child = findAndInvokeCustomSerializer(objTypePair); if (child != null) { addChildAsElement(f, child); return true; } return false; } catch (IllegalAccessException e) { throw new RuntimeException(); } catch (CircularReferenceException e) { throw e.createDetailedException(f); } }
public void visitPrimitive(Object obj) { JsonElement json = obj == null ? JsonNull.createJsonNull() : new JsonPrimitive(obj); assignToRoot(json); }
@Override public void visit(JsonNull value) { out.append(value.toString()); }