Java Code Examples for com.google.api.client.util.FieldInfo#setValue()
The following examples show how to use
com.google.api.client.util.FieldInfo#setValue() .
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: BaseApiService.java From connector-sdk with Apache License 2.0 | 6 votes |
private static Object setDefaultValuesForPrimitiveTypes(Object object) { if (!(object instanceof GenericJson)) { return object; } GenericJson genericJson = (GenericJson) object; for (FieldInfo f : genericJson.getClassInfo().getFieldInfos()) { if (Boolean.class.equals(f.getType())) { f.setValue(genericJson, Optional.ofNullable(f.getValue(genericJson)).orElse(false)); } else if (Integer.class.equals(f.getType())) { f.setValue(genericJson, Optional.ofNullable(f.getValue(genericJson)).orElse(0)); } else if (GenericJson.class.isAssignableFrom(f.getType())) { setDefaultValuesForPrimitiveTypes(f.getValue(genericJson)); } else if (Collection.class.isAssignableFrom(f.getType())) { Object collection = f.getValue(genericJson); if (collection == null) { f.setValue(genericJson, Collections.emptyList()); } else { Collection<?> values = (Collection<?>) collection; for (Object v : values) { setDefaultValuesForPrimitiveTypes(v); } } } } return object; }
Example 2
Source File: JsonParser.java From google-http-java-client with Apache License 2.0 | 4 votes |
/** * 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 3
Source File: HttpHeaders.java From google-http-java-client with Apache License 2.0 | 4 votes |
/** Parses the specified case-insensitive header pair into this HttpHeaders instance. */ void parseHeader(String headerName, String headerValue, ParseHeaderState state) { List<Type> context = state.context; ClassInfo classInfo = state.classInfo; ArrayValueMap arrayValueMap = state.arrayValueMap; StringBuilder logger = state.logger; if (logger != null) { logger.append(headerName + ": " + headerValue).append(StringUtils.LINE_SEPARATOR); } // use field information if available FieldInfo fieldInfo = classInfo.getFieldInfo(headerName); if (fieldInfo != null) { Type type = Data.resolveWildcardTypeOrTypeVariable(context, fieldInfo.getGenericType()); // type is now class, parameterized type, or generic array type if (Types.isArray(type)) { // array that can handle repeating values Class<?> rawArrayComponentType = Types.getRawArrayComponentType(context, Types.getArrayComponentType(type)); arrayValueMap.put( fieldInfo.getField(), rawArrayComponentType, parseValue(rawArrayComponentType, context, headerValue)); } else if (Types.isAssignableToOrFrom( Types.getRawArrayComponentType(context, type), Iterable.class)) { // iterable that can handle repeating values @SuppressWarnings("unchecked") Collection<Object> collection = (Collection<Object>) fieldInfo.getValue(this); if (collection == null) { collection = Data.newCollectionInstance(type); fieldInfo.setValue(this, collection); } Type subFieldType = type == Object.class ? null : Types.getIterableParameter(type); collection.add(parseValue(subFieldType, context, headerValue)); } else { // parse value based on field type fieldInfo.setValue(this, parseValue(type, context, headerValue)); } } else { // store header values in an array list @SuppressWarnings("unchecked") ArrayList<String> listValue = (ArrayList<String>) this.get(headerName); if (listValue == null) { listValue = new ArrayList<String>(); this.set(headerName, listValue); } listValue.add(headerValue); } }