/** * Creates a JSONPointer using an initialization string and tries to * match it to an item within this JSONObject. For example, given a * JSONObject initialized with this document: * <pre> * { * "a":{"b":"c"} * } * </pre> * and this JSONPointer string: * <pre> * "/a/b" * </pre> * Then this method will return the String "c". * A JSONPointerException may be thrown from code called by this method. * * @param jsonPointer string that can be used to create a JSONPointer * @return the item matched by the JSONPointer, otherwise null */ public Object query(String jsonPointer) { return query(new JSONPointer(jsonPointer)); } /**
/** * Creates a JSONPointer using an initialization string and tries to * match it to an item within this JSONObject. For example, given a * JSONObject initialized with this document: * <pre> * { * "a":{"b":"c"} * } * </pre> * and this JSONPointer string: * <pre> * "/a/b" * </pre> * Then this method will return the String "c". * A JSONPointerException may be thrown from code called by this method. * * @param jsonPointer string that can be used to create a JSONPointer * @return the item matched by the JSONPointer, otherwise null */ public Object query(String jsonPointer) { return query(new JSONPointer(jsonPointer)); } /**
/** * JSONObjects can be built from a Map<String, Object>. * In this test all of the map entries are valid JSON types. */ @Test public void jsonObjectByMap() { Map<String, Object> map = new HashMap<String, Object>(); map.put("trueKey", new Boolean(true)); map.put("falseKey", new Boolean(false)); map.put("stringKey", "hello world!"); map.put("escapeStringKey", "h\be\tllo w\u1234orld!"); map.put("intKey", new Long(42)); map.put("doubleKey", new Double(-23.45e67)); JSONObject jsonObject = new JSONObject(map); // validate JSON Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString()); assertTrue("expected 6 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 6); assertTrue("expected \"trueKey\":true", Boolean.TRUE.equals(jsonObject.query("/trueKey"))); assertTrue("expected \"falseKey\":false", Boolean.FALSE.equals(jsonObject.query("/falseKey"))); assertTrue("expected \"stringKey\":\"hello world!\"", "hello world!".equals(jsonObject.query("/stringKey"))); assertTrue("expected \"escapeStringKey\":\"h\be\tllo w\u1234orld!\"", "h\be\tllo w\u1234orld!".equals(jsonObject.query("/escapeStringKey"))); assertTrue("expected \"doubleKey\":-23.45e67", Double.valueOf("-23.45e67").equals(jsonObject.query("/doubleKey"))); }
/** * Creates a JSONPointer using an initialization string and tries to * match it to an item within this JSONObject. For example, given a * JSONObject initialized with this document: * <pre> * { * "a":{"b":"c"} * } * </pre> * and this JSONPointer string: * <pre> * "/a/b" * </pre> * Then this method will return the String "c". * A JSONPointerException may be thrown from code called by this method. * * @param jsonPointer string that can be used to create a JSONPointer * @return the item matched by the JSONPointer, otherwise null */ public Object query(String jsonPointer) { return query(new JSONPointer(jsonPointer)); } /**