public static HashMap<String, Object> flatten (Map<String, Object> map) { return flatten(map, null); //This function loops on itself. This is the initial part of it. The other flatten function with an extra variable is needed when the function begins looping on itself. }
protected HashMap<String, String> flattenMeta(Map<String, Object> meta) { meta = HashMapHelper.flatten(meta); HashMap<String, String> flatMeta = new HashMap<>(); for (Map.Entry<String, Object> entry : meta.entrySet()) { if (entry.getValue() instanceof String) { flatMeta.put(entry.getKey(), (String) entry.getValue()); } } return flatMeta; }
public static HashMap<String, Object> flatten(Map<String, Object> map, String previousKey) { HashMap<String, Object> outputMap = new HashMap<>(); //Here we loop over the keyset, and retrieve each value for that. for (String key : map.keySet()) { Object value = map.get(key); //Here the key is determined based on whether it is a new key or a nested key. String newKey = previousKey == null ? key : previousKey + "/" + key; //If the value is a hashmap, the function loops on itself, otherwise the value is placed into the final map. // In Firebase if we have a number indexing the data, Firebase thinks its an array list. if (value instanceof ArrayList) { ArrayList arrayList = (ArrayList) value; HashMap<String, Object> tempMap = new HashMap<>(); for(Integer i = 0; i < arrayList.size(); i++) { if (arrayList.get(i) != null) { tempMap.put(i.toString(), arrayList.get(i)); } } value = tempMap; } if (value instanceof HashMap) { outputMap.putAll(flatten((HashMap) value, newKey)); } else if (value instanceof String) { outputMap.put(newKey, (String) value); } } return outputMap; }
void deserializeMeta(Map<String, Object> value){ if (value != null) { Map<String, String> oldData = model.metaMap(); // Expand Map<String, Object> newData = HashMapHelper.flatten(value); // Updating the old bundle for (String key : newData.keySet()) { if (oldData.get(key) == null || !oldData.get(key).equals(newData.get(key))) { // We don't store availability data in the Firebase meta - it's handled by the online flag if (!key.equals(Keys.Availability)) { oldData.put(key, newData.get(key).toString()); } } } model.setMetaMap(oldData); model = DaoCore.updateEntity(model); } }