I have come up with the following method that translate the generic List
of ArrayMaps
of <String,Object>
objects in the Admin SDK.
It requires the following imports:
import com.google.api.client.json.GenericJson;
import com.google.api.client.util.ArrayMap;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
Here's the method that does the parsing:
public <T extends GenericJson> List<T> parse(Object genericItem, Class<T> clazz){
List<T> result = new ArrayList<T>();
if (genericItem != null){
try {
Constructor<T> constructor = clazz.getConstructor(new Class[0]); // find the default constructor of the input class type
List<ArrayMap<String,Object>> objects = (List<ArrayMap<String,Object>>)genericItem;
for (ArrayMap<String,Object> object: objects){
T id = constructor.newInstance(); // call the default constructor
for (int i = 0; i < object.size(); i++)
id.put(object.getKey(i), object.getValue(i));
result.add(id);
}
} catch (NoSuchMethodException ex) {
Logger.getLogger(ListArrayMapParser.class.getName()).log(Level.SEVERE, null, ex);
} catch (SecurityException ex) {
Logger.getLogger(ListArrayMapParser.class.getName()).log(Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
Logger.getLogger(ListArrayMapParser.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(ListArrayMapParser.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalArgumentException ex) {
Logger.getLogger(ListArrayMapParser.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvocationTargetException ex) {
Logger.getLogger(ListArrayMapParser.class.getName()).log(Level.SEVERE, null, ex);
}
}
return result;
}
This works like this:
List<UserPhone> phones = parser.parse(user.getPhones(), UserPhone.class);
List<UserExternalId> externalIds = parser.parse(user.getExternalIds(), UserExternalId.class);
List<UserOrganization> organizations = parser.parse(user.getOrganizations(), UserOrganization.class);
List<UserEmail> userEmails = parser.parse(user.getEmails(), UserEmail.class);
Once you've done this you can update the phones, externalIds,... and then do user.setPhones(phones)
and the User object will handle the new objects fine when saving.
I don't think you can have the API return the parsed result automatically for you today.