/** * Method {@code convertToMap} converts an {@code Environment} to a nested Map which represents a yml/json structure. * * @param input the environment to be converted * @return the nested map containing the environment's properties */ private Map<String, Object> convertToMap(Environment input) { // First use the current convertToProperties to get a flat Map from the environment Map<String, Object> properties = convertToProperties(input); // The root map which holds all the first level properties Map<String, Object> rootMap = new LinkedHashMap<>(); for (Map.Entry<String, Object> entry : properties.entrySet()) { String key = entry.getKey(); Object value = entry.getValue(); PropertyNavigator nav = new PropertyNavigator(key); nav.setMapValue(rootMap, value); } return rootMap; }
private void setListValue(List<Object> list, Object value) { int index = getIndex(); // Fill missing elements if needed while (list.size() <= index) { list.add(null); } if (NodeType.MAP.equals(valueType)) { @SuppressWarnings("unchecked") Map<String, Object> map = (Map<String, Object>) list.get(index); if (map == null) { map = new LinkedHashMap<>(); list.set(index, map); } setMapValue(map, value); } else if (NodeType.ARRAY.equals(valueType)) { @SuppressWarnings("unchecked") List<Object> nestedList = (List<Object>) list.get(index); if (nestedList == null) { nestedList = new ArrayList<>(); list.set(index, nestedList); } setListValue(nestedList, value); } else { list.set(index, value); } }
private void setMapValue(Map<String, Object> map, Object value) { String key = getKey(); if (NodeType.MAP.equals(valueType)) { @SuppressWarnings("unchecked") Map<String, Object> nestedMap = (Map<String, Object>) map.get(key); if (nestedMap == null) { nestedMap = new LinkedHashMap<>(); map.put(key, nestedMap); } setMapValue(nestedMap, value); } else if (NodeType.ARRAY.equals(valueType)) { @SuppressWarnings("unchecked") List<Object> list = (List<Object>) map.get(key); if (list == null) { list = new ArrayList<>(); map.put(key, list); } setListValue(list, value); } else { map.put(key, value); } }