Search in sources :

Example 6 with SerializedName

use of com.google.gson.annotations.SerializedName in project lastaflute by lastaflute.

the class LaReflectiveTypeAdapterFactory method getFieldNames.

/**
 * first element holds the default name
 * @param f The field to get name. (NotNull)
 * @return The read-only list of field names. (NotNull)
 */
protected List<String> getFieldNames(Field f) {
    final SerializedName annotation = f.getAnnotation(SerializedName.class);
    if (annotation == null) {
        String name = fieldNamingPolicy.translateName(f);
        return Collections.singletonList(name);
    }
    final String serializedName = annotation.value();
    final String[] alternates = annotation.alternate();
    if (alternates.length == 0) {
        return Collections.singletonList(serializedName);
    }
    final List<String> fieldNames = new ArrayList<String>(alternates.length + 1);
    fieldNames.add(serializedName);
    for (String alternate : alternates) {
        fieldNames.add(alternate);
    }
    return fieldNames;
}
Also used : SerializedName(com.google.gson.annotations.SerializedName) ArrayList(java.util.ArrayList)

Example 7 with SerializedName

use of com.google.gson.annotations.SerializedName in project maple-ir by LLVM-but-worse.

the class ReflectiveTypeAdapterFactory method getFieldName.

/**
 * first element holds the default name
 */
static List<String> getFieldName(FieldNamingStrategy fieldNamingPolicy, Field f) {
    SerializedName serializedName = f.getAnnotation(SerializedName.class);
    List<String> fieldNames = new LinkedList<String>();
    if (serializedName == null) {
        fieldNames.add(fieldNamingPolicy.translateName(f));
    } else {
        fieldNames.add(serializedName.value());
        for (String alternate : serializedName.alternate()) {
            fieldNames.add(alternate);
        }
    }
    return fieldNames;
}
Also used : SerializedName(com.google.gson.annotations.SerializedName) LinkedList(java.util.LinkedList)

Example 8 with SerializedName

use of com.google.gson.annotations.SerializedName in project msgraph-sdk-java by microsoftgraph.

the class AdditionalDataManager method getFields.

private Set<String> getFields() {
    Field[] fields = jsonBackedObject.getClass().getFields();
    Set<String> serializingFields = new HashSet<>();
    for (Field field : fields) {
        SerializedName serializedName;
        if (null != (serializedName = field.getAnnotation(SerializedName.class)) && null != field.getAnnotation(Expose.class)) {
            serializingFields.add(serializedName.value());
        }
    }
    return serializingFields;
}
Also used : Field(java.lang.reflect.Field) SerializedName(com.google.gson.annotations.SerializedName) HashSet(java.util.HashSet)

Example 9 with SerializedName

use of com.google.gson.annotations.SerializedName in project cloudstack by apache.

the class ApiResponseSerializer method serializeResponseObjFieldsXML.

private static void serializeResponseObjFieldsXML(StringBuilder sb, StringBuilder log, ResponseObject obj) {
    boolean isAsync = false;
    if (obj instanceof AsyncJobResponse)
        isAsync = true;
    Field[] fields = getFlattenFields(obj.getClass());
    for (Field field : fields) {
        if ((field.getModifiers() & Modifier.TRANSIENT) != 0) {
            // skip transient fields
            continue;
        }
        SerializedName serializedName = field.getAnnotation(SerializedName.class);
        if (serializedName == null) {
            // skip fields w/o serialized name
            continue;
        }
        boolean logField = true;
        Param param = field.getAnnotation(Param.class);
        if (param != null) {
            RoleType[] allowedRoles = param.authorized();
            if (allowedRoles.length > 0) {
                boolean permittedParameter = false;
                Account caller = CallContext.current().getCallingAccount();
                for (RoleType allowedRole : allowedRoles) {
                    if (allowedRole.getAccountType() == caller.getType()) {
                        permittedParameter = true;
                        break;
                    }
                }
                if (!permittedParameter) {
                    s_logger.trace("Ignoring parameter " + param.name() + " as the caller is not authorized to see it");
                    continue;
                }
            }
            if (param.isSensitive()) {
                logField = false;
            }
        }
        field.setAccessible(true);
        Object fieldValue = null;
        try {
            fieldValue = field.get(obj);
        } catch (IllegalArgumentException e) {
            throw new CloudRuntimeException("how illegal is it?", e);
        } catch (IllegalAccessException e) {
            throw new CloudRuntimeException("come on...we set accessible already", e);
        }
        if (fieldValue != null) {
            if (fieldValue instanceof ResponseObject) {
                ResponseObject subObj = (ResponseObject) fieldValue;
                if (isAsync) {
                    sb.append("<jobresult>");
                    log.append("<jobresult>");
                }
                serializeResponseObjXML(sb, log, subObj);
                if (isAsync) {
                    sb.append("</jobresult>");
                    log.append("</jobresult>");
                }
            } else if (fieldValue instanceof Collection<?>) {
                Collection<?> subResponseList = (Collection<?>) fieldValue;
                boolean usedUuidList = false;
                for (Object value : subResponseList) {
                    if (value instanceof ResponseObject) {
                        ResponseObject subObj = (ResponseObject) value;
                        if (serializedName != null) {
                            subObj.setObjectName(serializedName.value());
                        }
                        serializeResponseObjXML(sb, log, subObj);
                    } else if (value instanceof ExceptionProxyObject) {
                        // Only exception reponses carry a list of
                        // ExceptionProxyObject objects.
                        ExceptionProxyObject idProxy = (ExceptionProxyObject) value;
                        // encountered, put in a uuidList tag.
                        if (!usedUuidList) {
                            sb.append("<" + serializedName.value() + ">");
                            log.append("<" + serializedName.value() + ">");
                            usedUuidList = true;
                        }
                        sb.append("<" + "uuid" + ">" + idProxy.getUuid() + "</" + "uuid" + ">");
                        log.append("<" + "uuid" + ">" + idProxy.getUuid() + "</" + "uuid" + ">");
                        // Append the new descriptive property also.
                        String idFieldName = idProxy.getDescription();
                        if (idFieldName != null) {
                            sb.append("<" + "uuidProperty" + ">" + idFieldName + "</" + "uuidProperty" + ">");
                            log.append("<" + "uuidProperty" + ">" + idFieldName + "</" + "uuidProperty" + ">");
                        }
                    } else if (value instanceof String) {
                        sb.append("<").append(serializedName.value()).append(">").append(value).append("</").append(serializedName.value()).append(">");
                        if (logField) {
                            log.append("<").append(serializedName.value()).append(">").append(value).append("</").append(serializedName.value()).append(">");
                        }
                    }
                }
                if (usedUuidList) {
                    // close the uuidList.
                    sb.append("</").append(serializedName.value()).append(">");
                    log.append("</").append(serializedName.value()).append(">");
                }
            } else if (fieldValue instanceof Date) {
                sb.append("<").append(serializedName.value()).append(">").append(BaseCmd.getDateString((Date) fieldValue)).append("</").append(serializedName.value()).append(">");
                log.append("<").append(serializedName.value()).append(">").append(BaseCmd.getDateString((Date) fieldValue)).append("</").append(serializedName.value()).append(">");
            } else {
                String resultString = escapeSpecialXmlChars(fieldValue.toString());
                if (!(obj instanceof ExceptionResponse)) {
                    resultString = encodeParam(resultString);
                }
                sb.append("<").append(serializedName.value()).append(">").append(resultString).append("</").append(serializedName.value()).append(">");
                if (logField) {
                    log.append("<").append(serializedName.value()).append(">").append(resultString).append("</").append(serializedName.value()).append(">");
                }
            }
        }
    }
}
Also used : Account(com.cloud.user.Account) ExceptionResponse(org.apache.cloudstack.api.response.ExceptionResponse) RoleType(org.apache.cloudstack.acl.RoleType) AsyncJobResponse(org.apache.cloudstack.api.response.AsyncJobResponse) SerializedName(com.google.gson.annotations.SerializedName) Date(java.util.Date) Field(java.lang.reflect.Field) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Param(com.cloud.serializer.Param) ResponseObject(org.apache.cloudstack.api.ResponseObject) Collection(java.util.Collection) ExceptionProxyObject(com.cloud.utils.exception.ExceptionProxyObject) ResponseObject(org.apache.cloudstack.api.ResponseObject) ExceptionProxyObject(com.cloud.utils.exception.ExceptionProxyObject)

Example 10 with SerializedName

use of com.google.gson.annotations.SerializedName in project cloudstack by apache.

the class ApiXmlDocWriter method setResponseFields.

private static ArrayList<Argument> setResponseFields(Field[] responseFields, Class<?> responseClas) {
    ArrayList<Argument> arguments = new ArrayList<Argument>();
    ArrayList<Argument> sortedChildlessArguments = new ArrayList<Argument>();
    ArrayList<Argument> sortedArguments = new ArrayList<Argument>();
    Argument id = null;
    for (Field responseField : responseFields) {
        SerializedName nameAnnotation = responseField.getAnnotation(SerializedName.class);
        if (nameAnnotation != null) {
            Param paramAnnotation = responseField.getAnnotation(Param.class);
            Argument respArg = new Argument(nameAnnotation.value());
            boolean hasChildren = false;
            if (paramAnnotation != null && paramAnnotation.includeInApiDoc()) {
                String description = paramAnnotation.description();
                Class fieldClass = paramAnnotation.responseObject();
                if (description != null && !description.isEmpty()) {
                    respArg.setDescription(description);
                }
                respArg.setDataType(responseField.getType().getSimpleName().toLowerCase());
                if (!paramAnnotation.since().isEmpty()) {
                    respArg.setSinceVersion(paramAnnotation.since());
                }
                if (fieldClass != null) {
                    Class<?> superClass = fieldClass.getSuperclass();
                    if (superClass != null) {
                        String superName = superClass.getName();
                        if (superName.equals(BaseResponse.class.getName())) {
                            ArrayList<Argument> fieldArguments = new ArrayList<Argument>();
                            Field[] fields = fieldClass.getDeclaredFields();
                            fieldArguments = setResponseFields(fields, fieldClass);
                            respArg.setArguments(fieldArguments);
                            hasChildren = true;
                        }
                    }
                }
            }
            if (paramAnnotation != null && paramAnnotation.includeInApiDoc()) {
                if (nameAnnotation.value().equals("id")) {
                    id = respArg;
                } else {
                    if (hasChildren) {
                        respArg.setName(nameAnnotation.value() + "(*)");
                        sortedArguments.add(respArg);
                    } else {
                        sortedChildlessArguments.add(respArg);
                    }
                }
            }
        }
    }
    Collections.sort(sortedArguments);
    Collections.sort(sortedChildlessArguments);
    if (id != null) {
        arguments.add(id);
    }
    arguments.addAll(sortedChildlessArguments);
    arguments.addAll(sortedArguments);
    if (responseClas.getName().equalsIgnoreCase(AsyncJobResponse.class.getName())) {
        Argument jobIdArg = new Argument("jobid", "the ID of the async job");
        arguments.add(jobIdArg);
    } else if (AsyncResponses.contains(responseClas.getName())) {
        Argument jobIdArg = new Argument("jobid", "the ID of the latest async job acting on this object");
        Argument jobStatusArg = new Argument("jobstatus", "the current status of the latest async job acting on this object");
        arguments.add(jobIdArg);
        arguments.add(jobStatusArg);
    }
    return arguments;
}
Also used : SerializedName(com.google.gson.annotations.SerializedName) AsyncJobResponse(org.apache.cloudstack.api.response.AsyncJobResponse) ArrayList(java.util.ArrayList) Field(java.lang.reflect.Field) BaseResponse(org.apache.cloudstack.api.BaseResponse) Param(com.cloud.serializer.Param)

Aggregations

SerializedName (com.google.gson.annotations.SerializedName)11 Field (java.lang.reflect.Field)9 Param (com.cloud.serializer.Param)6 ArrayList (java.util.ArrayList)4 BaseResponse (com.cloud.api.BaseResponse)2 Account (com.cloud.user.Account)2 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)2 ExceptionProxyObject (com.cloud.utils.exception.ExceptionProxyObject)2 Collection (java.util.Collection)2 Date (java.util.Date)2 BaseResponse (org.apache.cloudstack.api.BaseResponse)2 AsyncJobResponse (org.apache.cloudstack.api.response.AsyncJobResponse)2 RoleType (com.cloud.acl.RoleType)1 ResponseObject (com.cloud.api.ResponseObject)1 ApiResponseResponse (com.cloud.api.response.ApiResponseResponse)1 AsyncJobResponse (com.cloud.api.response.AsyncJobResponse)1 KubernetesType (io.kubernetes.client.common.KubernetesType)1 IntOrString (io.kubernetes.client.custom.IntOrString)1 Quantity (io.kubernetes.client.custom.Quantity)1 V1Container (io.kubernetes.client.openapi.models.V1Container)1