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

The following examples show how to use org.json.JSONObject#query() . These examples are extracted from open source projects. 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 Project: json-schema   File: JSONPointerTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Coverage for JSONObject query(String)
 */
@Test
public void queryFromJSONObject() {
    String str = "{" +
            "\"stringKey\":\"hello world!\"," +
            "\"arrayKey\":[0,1,2]," +
            "\"objectKey\": {" +
            "\"a\":\"aVal\"," +
            "\"b\":\"bVal\"" +
            "}" +
            "}";
    JSONObject jsonObject = new JSONObject(str);
    Object obj = jsonObject.query("/stringKey");
    assertTrue("Expected 'hello world!'", "hello world!".equals(obj));
    obj = jsonObject.query("/arrayKey/1");
    assertTrue("Expected 1", Integer.valueOf(1).equals(obj));
    obj = jsonObject.query("/objectKey/b");
    assertTrue("Expected bVal", "bVal".equals(obj));
    try {
        obj = jsonObject.query("/a/b/c");
        assertTrue("Expected JSONPointerException", false);
    } catch (JSONPointerException e) {
        assertTrue("Expected bad key/value exception",
                "value [null] is not an array or object therefore its key b cannot be resolved".
                        equals(e.getMessage()));
    }
}
 
Example 2
Source Project: json-schema   File: JSONPointerTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Coverage for JSONObject query(JSONPointer)
 */
@Test
public void queryFromJSONObjectUsingPointer() {
    String str = "{" +
            "\"stringKey\":\"hello world!\"," +
            "\"arrayKey\":[0,1,2]," +
            "\"objectKey\": {" +
            "\"a\":\"aVal\"," +
            "\"b\":\"bVal\"" +
            "}" +
            "}";
    JSONObject jsonObject = new JSONObject(str);
    Object obj = jsonObject.query(new JSONPointer("/stringKey"));
    assertTrue("Expected 'hello world!'", "hello world!".equals(obj));
    obj = jsonObject.query(new JSONPointer("/arrayKey/1"));
    assertTrue("Expected 1", Integer.valueOf(1).equals(obj));
    obj = jsonObject.query(new JSONPointer("/objectKey/b"));
    assertTrue("Expected bVal", "bVal".equals(obj));
    try {
        obj = jsonObject.query(new JSONPointer("/a/b/c"));
        assertTrue("Expected JSONPointerException", false);
    } catch (JSONPointerException e) {
        assertTrue("Expected bad key/value exception",
                "value [null] is not an array or object therefore its key b cannot be resolved".
                        equals(e.getMessage()));
    }
}
 
Example 3
@Test
public void nameOverride() {
    JSONObject rawSchemaJson = baseSchemaJson();
    JSONObject idPropSchema = (JSONObject) rawSchemaJson.query("/properties/id");
    idPropSchema.put("format", "somethingelse");
    SchemaLoader schemaLoader = SchemaLoader.builder()
            .schemaJson(rawSchemaJson)
            .addFormatValidator("somethingelse", new EvenCharNumValidator())
            .build();
    Object actual = fetchFormatValueFromOutputJson(schemaLoader);
    assertEquals("somethingelse", actual);
}
 
Example 4
/**
 * Coverage for JSONObject query(String)
 */
@Test
public void queryFromJSONObject() {
    String str = "{"+
            "\"stringKey\":\"hello world!\","+
            "\"arrayKey\":[0,1,2],"+
            "\"objectKey\": {"+
                "\"a\":\"aVal\","+
                "\"b\":\"bVal\""+
            "}"+
        "}";    
    JSONObject jsonObject = new JSONObject(str);
    Object obj = jsonObject.query("/stringKey");
    assertTrue("Expected 'hello world!'", "hello world!".equals(obj));
    obj = jsonObject.query("/arrayKey/1");
    assertTrue("Expected 1", Integer.valueOf(1).equals(obj));
    obj = jsonObject.query("/objectKey/b");
    assertTrue("Expected bVal", "bVal".equals(obj));
    try {
        obj = jsonObject.query("/a/b/c");
        assertTrue("Expected JSONPointerException", false);
    } catch (JSONPointerException e) {
        assertTrue("Expected bad key/value exception",
                "value [null] is not an array or object therefore its key b cannot be resolved".
                equals(e.getMessage()));
    }
}
 
Example 5
/**
 * Coverage for JSONObject query(JSONPointer)
 */
@Test
public void queryFromJSONObjectUsingPointer() {
    String str = "{"+
            "\"stringKey\":\"hello world!\","+
            "\"arrayKey\":[0,1,2],"+
            "\"objectKey\": {"+
                "\"a\":\"aVal\","+
                "\"b\":\"bVal\""+
            "}"+
        "}";    
    JSONObject jsonObject = new JSONObject(str);
    Object obj = jsonObject.query(new JSONPointer("/stringKey"));
    assertTrue("Expected 'hello world!'", "hello world!".equals(obj));
    obj = jsonObject.query(new JSONPointer("/arrayKey/1"));
    assertTrue("Expected 1", Integer.valueOf(1).equals(obj));
    obj = jsonObject.query(new JSONPointer("/objectKey/b"));
    assertTrue("Expected bVal", "bVal".equals(obj));
    try {
        obj = jsonObject.query(new JSONPointer("/a/b/c"));
        assertTrue("Expected JSONPointerException", false);
    } catch (JSONPointerException e) {
        assertTrue("Expected bad key/value exception",
                "value [null] is not an array or object therefore its key b cannot be resolved".
                equals(e.getMessage()));
    }
}