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

The following examples show how to use org.json.JSONObject#query() . 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: JSONPointerTest.java    From json-schema with 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 File: JSONPointerTest.java    From json-schema with 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
Source File: CustomFormatValidatorTest.java    From json-schema with Apache License 2.0 5 votes vote down vote up
@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
Source File: JSONPointerTest.java    From JSON-Java-unit-test with 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 5
Source File: JSONPointerTest.java    From JSON-Java-unit-test with 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()));
    }
}