Java Code Examples for org.json.JSONObject#toMap()

The following examples show how to use org.json.JSONObject#toMap() . 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: KM200ErrorService.java    From openhab1-addons with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * This function updates the errors
 *
 */
void updateErrors(JSONObject nodeRoot) {
    synchronized (errorMap) {
        /* Update the list of errors */
        try {
            removeAllErrors();
            JSONArray sPoints = nodeRoot.getJSONArray("values");
            for (int i = 0; i < sPoints.length(); i++) {
                JSONObject subJSON = sPoints.getJSONObject(i);
                HashMap<String, String> valMap = new HashMap<String, String>();
                Map<String, Object> oMap = subJSON.toMap();
                for (String para : oMap.keySet()) {
                    logger.debug("Set: {} val: {}", para, oMap.get(para));
                    valMap.put(para, oMap.get(para).toString());
                }
                errorMap.add(valMap);
            }
        } catch (Exception e) {
            logger.error("Error in parsing of the errorlist: {}", e.getMessage());
        }
    }
}
 
Example 2
Source File: Main.java    From nanoleaf-desktop with MIT License 5 votes vote down vote up
private Map<String, Object> getDevices() {
    PropertyManager manager = new PropertyManager(PROPERTIES_FILEPATH);
    String devicesStr = manager.getProperty("devices");
    if (devicesStr != null) {
        JSONObject json = new JSONObject(devicesStr);
        return json.toMap();
    }
    return new HashMap<String, Object>();
}
 
Example 3
Source File: GroupCreatorDialog.java    From nanoleaf-desktop with MIT License 5 votes vote down vote up
private Map<String, Object> getLocalDeviceData()
{
	PropertyManager manager = new PropertyManager(Main.PROPERTIES_FILEPATH);
	String devicesStr = manager.getProperty("devices");
	if (devicesStr != null)
	{
		JSONObject json = new JSONObject(devicesStr);
		return json.toMap();
	}
	return new HashMap<String, Object>();
}
 
Example 4
Source File: AuroraFinder.java    From nanoleaf-desktop with MIT License 5 votes vote down vote up
private Map<String, Object> getDevices()
{
	PropertyManager manager = new PropertyManager(Main.PROPERTIES_FILEPATH);
	String devicesStr = manager.getProperty("devices");
	if (devicesStr != null)
	{
		JSONObject json = new JSONObject(devicesStr);
		return json.toMap();
	}
	return new HashMap<String, Object>();
}
 
Example 5
Source File: DeviceChangerDialog.java    From nanoleaf-desktop with MIT License 5 votes vote down vote up
private Map<String, Object> getDevices()
{
	PropertyManager manager = new PropertyManager(Main.PROPERTIES_FILEPATH);
	String devicesStr = manager.getProperty("devices");
	if (devicesStr != null)
	{
		JSONObject json = new JSONObject(devicesStr);
		return json.toMap();
	}
	return new HashMap<String, Object>();
}
 
Example 6
Source File: FilterService.java    From spring-boot-rest-api-helpers with MIT License 5 votes vote down vote up
public long countBy(QueryParamWrapper queryParamWrapper, BaseRepository<T, I> repo) {
    JSONObject filter = queryParamWrapper.getFilter();
    JSONArray filterOr = queryParamWrapper.getFilterOr();
    String usesSnakeCase = env.getProperty("spring-boot-rest-api-helpers.use-snake-case");
    if (filter != null && filter.length() > 0) {
        HashMap<String, Object> map = (HashMap<String, Object>) filter.toMap();

        if (usesSnakeCase != null && usesSnakeCase.equals("true")) {
            map = convertToCamelCase(map);
        }

        return repo.count(
                specifications.customSpecificationBuilder(map));

    } else if (filterOr != null && filterOr.length() > 0) {

        return repo.count((Specification<T>) (root, query, builder) -> {
            List list = filterOr.toList();
            if (usesSnakeCase != null && usesSnakeCase.equals("true")) {
                //map = convertToCamelCase(map); TODO for list
            }
            return specifications.customSpecificationBuilder(builder, query, root, list);
        });

    } else {
        return repo.count();
    }
}
 
Example 7
Source File: OptionsDialog.java    From DeskChan with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static Map stringToMap(String text) throws Exception{
	text = text.trim();
	if (!text.startsWith("{")) text = "{" + text;
	if (!text.endsWith("}"))   text = text + "}";

	JSONObject json = new JSONObject(text);
	return json.toMap();
}
 
Example 8
Source File: FileData.java    From LightningStorage with Apache License 2.0 4 votes vote down vote up
public FileData(final JSONObject jsonObject) {
  localMap = new HashMap<>(jsonObject.toMap());
}
 
Example 9
Source File: Document.java    From yacy_grid_mcp with GNU Lesser General Public License v2.1 4 votes vote down vote up
public Document(JSONObject obj) {
    super(obj.toMap());
}
 
Example 10
Source File: WebDocument.java    From yacy_grid_mcp with GNU Lesser General Public License v2.1 4 votes vote down vote up
public WebDocument(JSONObject obj) {
    super(obj.toMap());
}
 
Example 11
Source File: MapTypeHandler.java    From taskana with Apache License 2.0 4 votes vote down vote up
private Map<String, Object> convertToMap(String fieldValue) {
  JSONObject jsonObj = new JSONObject(fieldValue);
  return jsonObj.toMap();
}
 
Example 12
Source File: JSONObjectTest.java    From JSON-Java-unit-test with Apache License 2.0 4 votes vote down vote up
/**
 * Exercise JSONObject toMap() method.
 */
@Test
public void toMap() {
    String jsonObjectStr =
            "{" +
            "\"key1\":" +
                "[1,2," +
                    "{\"key3\":true}" +
                "]," +
            "\"key2\":" +
                "{\"key1\":\"val1\",\"key2\":" +
                    "{\"key2\":null}," +
                "\"key3\":42" +
                "}," +
            "\"key3\":" +
                "[" +
                    "[\"value1\",2.1]" +
                "," +
                    "[null]" +
                "]" +
            "}";

    JSONObject jsonObject = new JSONObject(jsonObjectStr);
    Map<?,?> map = jsonObject.toMap();

    assertTrue("Map should not be null", map != null);
    assertTrue("Map should have 3 elements", map.size() == 3);

    List<?> key1List = (List<?>)map.get("key1");
    assertTrue("key1 should not be null", key1List != null);
    assertTrue("key1 list should have 3 elements", key1List.size() == 3);
    assertTrue("key1 value 1 should be 1", key1List.get(0).equals(Integer.valueOf(1)));
    assertTrue("key1 value 2 should be 2", key1List.get(1).equals(Integer.valueOf(2)));

    Map<?,?> key1Value3Map = (Map<?,?>)key1List.get(2);
    assertTrue("Map should not be null", key1Value3Map != null);
    assertTrue("Map should have 1 element", key1Value3Map.size() == 1);
    assertTrue("Map key3 should be true", key1Value3Map.get("key3").equals(Boolean.TRUE));

    Map<?,?> key2Map = (Map<?,?>)map.get("key2");
    assertTrue("key2 should not be null", key2Map != null);
    assertTrue("key2 map should have 3 elements", key2Map.size() == 3);
    assertTrue("key2 map key 1 should be val1", key2Map.get("key1").equals("val1"));
    assertTrue("key2 map key 3 should be 42", key2Map.get("key3").equals(Integer.valueOf(42)));

    Map<?,?> key2Val2Map = (Map<?,?>)key2Map.get("key2");
    assertTrue("key2 map key 2 should not be null", key2Val2Map != null);
    assertTrue("key2 map key 2 should have an entry", key2Val2Map.containsKey("key2"));
    assertTrue("key2 map key 2 value should be null", key2Val2Map.get("key2") == null);

    List<?> key3List = (List<?>)map.get("key3");
    assertTrue("key3 should not be null", key3List != null);
    assertTrue("key3 list should have 3 elements", key3List.size() == 2);

    List<?> key3Val1List = (List<?>)key3List.get(0);
    assertTrue("key3 list val 1 should not be null", key3Val1List != null);
    assertTrue("key3 list val 1 should have 2 elements", key3Val1List.size() == 2);
    assertTrue("key3 list val 1 list element 1 should be value1", key3Val1List.get(0).equals("value1"));
    assertTrue("key3 list val 1 list element 2 should be 2.1", key3Val1List.get(1).equals(Double.valueOf("2.1")));

    List<?> key3Val2List = (List<?>)key3List.get(1);
    assertTrue("key3 list val 2 should not be null", key3Val2List != null);
    assertTrue("key3 list val 2 should have 1 element", key3Val2List.size() == 1);
    assertTrue("key3 list val 2 list element 1 should be null", key3Val2List.get(0) == null);

    // Assert that toMap() is a deep copy
    jsonObject.getJSONArray("key3").getJSONArray(0).put(0, "still value 1");
    assertTrue("key3 list val 1 list element 1 should be value1", key3Val1List.get(0).equals("value1"));

    // assert that the new map is mutable
    assertTrue("Removing a key should succeed", map.remove("key3") != null);
    assertTrue("Map should have 2 elements", map.size() == 2);
}