Java Code Examples for com.google.api.client.util.FieldInfo#isPrimitive()

The following examples show how to use com.google.api.client.util.FieldInfo#isPrimitive() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JsonParser.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
/**
 * Parses the next field from the given JSON parser into the given destination object.
 *
 * @param context destination context stack (possibly empty)
 * @param destination destination object instance or {@code null} for none (for example empty
 *     context stack)
 * @param customizeParser optional parser customizer or {@code null} for none
 */
private void parse(
    ArrayList<Type> context, Object destination, CustomizeJsonParser customizeParser)
    throws IOException {
  if (destination instanceof GenericJson) {
    ((GenericJson) destination).setFactory(getFactory());
  }
  JsonToken curToken = startParsingObjectOrArray();
  Class<?> destinationClass = destination.getClass();
  ClassInfo classInfo = ClassInfo.of(destinationClass);
  boolean isGenericData = GenericData.class.isAssignableFrom(destinationClass);
  if (!isGenericData && Map.class.isAssignableFrom(destinationClass)) {
    // The destination class is not a sub-class of GenericData but is of Map, so parse data
    // using parseMap.
    @SuppressWarnings("unchecked")
    Map<String, Object> destinationMap = (Map<String, Object>) destination;
    parseMap(
        null,
        destinationMap,
        Types.getMapValueParameter(destinationClass),
        context,
        customizeParser);
    return;
  }
  while (curToken == JsonToken.FIELD_NAME) {
    String key = getText();
    nextToken();
    // stop at items for feeds
    if (customizeParser != null && customizeParser.stopAt(destination, key)) {
      return;
    }
    // get the field from the type information
    FieldInfo fieldInfo = classInfo.getFieldInfo(key);
    if (fieldInfo != null) {
      // skip final fields
      if (fieldInfo.isFinal() && !fieldInfo.isPrimitive()) {
        throw new IllegalArgumentException("final array/object fields are not supported");
      }
      Field field = fieldInfo.getField();
      int contextSize = context.size();
      context.add(field.getGenericType());
      Object fieldValue =
          parseValue(
              field, fieldInfo.getGenericType(), context, destination, customizeParser, true);
      context.remove(contextSize);
      fieldInfo.setValue(destination, fieldValue);
    } else if (isGenericData) {
      // store unknown field in generic JSON
      GenericData object = (GenericData) destination;
      object.set(key, parseValue(null, null, context, destination, customizeParser, true));
    } else {
      // unrecognized field, skip value.
      if (customizeParser != null) {
        customizeParser.handleUnrecognizedKey(destination, key);
      }
      skipChildren();
    }
    curToken = nextToken();
  }
}
 
Example 2
Source File: GoogleAtom.java    From google-api-java-client with Apache License 2.0 4 votes vote down vote up
private static void appendFieldsFor(
    StringBuilder fieldsBuf, Class<?> dataClass, int[] numFields) {
  if (Map.class.isAssignableFrom(dataClass) || Collection.class.isAssignableFrom(dataClass)) {
    throw new IllegalArgumentException(
        "cannot specify field mask for a Map or Collection class: " + dataClass);
  }
  ClassInfo classInfo = ClassInfo.of(dataClass);
  for (String name : new TreeSet<String>(classInfo.getNames())) {
    FieldInfo fieldInfo = classInfo.getFieldInfo(name);
    if (fieldInfo.isFinal()) {
      continue;
    }
    if (++numFields[0] != 1) {
      fieldsBuf.append(',');
    }
    fieldsBuf.append(name);
    // TODO(yanivi): handle Java arrays?
    Class<?> fieldClass = fieldInfo.getType();
    if (Collection.class.isAssignableFrom(fieldClass)) {
      // TODO(yanivi): handle Java collection of Java collection or Java map?
      fieldClass = (Class<?>) Types.getIterableParameter(fieldInfo.getField().getGenericType());
    }
    // TODO(yanivi): implement support for map when server implements support for *:*
    if (fieldClass != null) {
      if (fieldInfo.isPrimitive()) {
        if (name.charAt(0) != '@' && !name.equals("text()")) {
          // TODO(yanivi): wait for bug fix from server to support text() -- already fixed???
          // buf.append("/text()");
        }
      } else if (!Collection.class.isAssignableFrom(fieldClass)
          && !Map.class.isAssignableFrom(fieldClass)) {
        int[] subNumFields = new int[1];
        int openParenIndex = fieldsBuf.length();
        fieldsBuf.append('(');
        // TODO(yanivi): abort if found cycle to avoid infinite loop
        appendFieldsFor(fieldsBuf, fieldClass, subNumFields);
        updateFieldsBasedOnNumFields(fieldsBuf, openParenIndex, subNumFields[0]);
      }
    }
  }
}