Java Code Examples for org.json.JSONTokener#back()
The following examples show how to use
org.json.JSONTokener#back() .
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: JSONTokenerTest.java From j2objc with Apache License 2.0 | 6 votes |
public void testCharacterNavigation() throws JSONException { JSONTokener abcdeTokener = new JSONTokener("ABCDE"); assertEquals('A', abcdeTokener.next()); assertEquals('B', abcdeTokener.next('B')); assertEquals("CD", abcdeTokener.next(2)); try { abcdeTokener.next(2); fail(); } catch (JSONException e) { } assertEquals('E', abcdeTokener.nextClean()); assertEquals('\0', abcdeTokener.next()); assertFalse(abcdeTokener.more()); abcdeTokener.back(); assertTrue(abcdeTokener.more()); assertEquals('E', abcdeTokener.next()); }
Example 2
Source File: JSONTokenerTest.java From j2objc with Apache License 2.0 | 6 votes |
public void testBackNextAndMore() throws JSONException { JSONTokener abcTokener = new JSONTokener("ABC"); assertTrue(abcTokener.more()); abcTokener.next(); abcTokener.next(); assertTrue(abcTokener.more()); abcTokener.next(); assertFalse(abcTokener.more()); abcTokener.back(); assertTrue(abcTokener.more()); abcTokener.next(); assertFalse(abcTokener.more()); abcTokener.back(); abcTokener.back(); abcTokener.back(); abcTokener.back(); // you can back up before the beginning of a String! assertEquals('A', abcTokener.next()); }
Example 3
Source File: OrgJsonDeserializer.java From jjwt with Apache License 2.0 | 6 votes |
private Object parse(String json) throws JSONException { JSONTokener tokener = new JSONTokener(json); char c = tokener.nextClean(); //peak ahead tokener.back(); //revert if (c == '{') { //json object JSONObject o = new JSONObject(tokener); return toMap(o); } else if (c == '[') { JSONArray a = new JSONArray(tokener); return toList(a); } else { //raw json value Object value = tokener.nextValue(); return convertIfNecessary(value); } }
Example 4
Source File: JsonBasedPropertiesProvider.java From cfg4j with Apache License 2.0 | 5 votes |
/** * Get {@link Properties} for a given {@code inputStream} treating it as a JSON file. * * @param inputStream input stream representing JSON file * @return properties representing values from {@code inputStream} * @throws IllegalStateException when unable to read properties */ @Override public Properties getProperties(InputStream inputStream) { requireNonNull(inputStream); Properties properties = new Properties(); try { JSONTokener tokener = new JSONTokener(inputStream); if (tokener.end()) { return properties; } if (tokener.nextClean() == '"') { tokener.back(); properties.put("content", tokener.nextValue().toString()); } else { tokener.back(); JSONObject obj = new JSONObject(tokener); Map<String, Object> yamlAsMap = convertToMap(obj); properties.putAll(flatten(yamlAsMap)); } return properties; } catch (Exception e) { throw new IllegalStateException("Unable to load json configuration from provided stream", e); } }
Example 5
Source File: JSONUtils.java From orion.server with Eclipse Public License 1.0 | 4 votes |
public static Map<String, Object> parseJSON(String jsonBody) throws JSONException { final Map<String, Object> params = new HashMap<String, Object>(); final JSONTokener x = new JSONTokener(jsonBody); char c; String key; if (x.nextClean() != '{') { throw new IllegalArgumentException(format("String '%s' is not a valid JSON object representation, a JSON object text must begin with '{'", jsonBody)); } for (;;) { c = x.nextClean(); switch (c) { case 0: throw new IllegalArgumentException(format("String '%s' is not a valid JSON object representation, a JSON object text must end with '}'", jsonBody)); case '}': return params; default: x.back(); key = x.nextValue().toString(); } /* * The key is followed by ':'. We will also tolerate '=' or '=>'. */ c = x.nextClean(); if (c == '=') { if (x.next() != '>') { x.back(); } } else if (c != ':') { throw new IllegalArgumentException(format("String '%s' is not a valid JSON object representation, expected a ':' after the key '%s'", jsonBody, key)); } Object value = x.nextValue(); // guard from null values if (value != null) { if (value instanceof JSONArray) { // only plain simple arrays in this version JSONArray array = (JSONArray) value; Object[] values = new Object[array.length()]; for (int i = 0; i < array.length(); i++) { values[i] = array.get(i); } value = values; } params.put(key, value); } /* * Pairs are separated by ','. We will also tolerate ';'. */ switch (x.nextClean()) { case ';': case ',': if (x.nextClean() == '}') { return params; } x.back(); break; case '}': return params; default: throw new IllegalArgumentException("Expected a ',' or '}'"); } } }
Example 6
Source File: JSONTokenerTest.java From JSON-Java-unit-test with Apache License 2.0 | 4 votes |
/** * Verify that next and back are working properly and tracking the correct positions * with different new line combinations. */ @Test public void testNextBackComboWithNewLines() { final String testString = "this is\nA test\r\nWith some different\rNew Lines"; // ^ ^ ^ ^ // index positions 0 8 16 36 final JSONTokener tokener = new JSONTokener(testString); assertEquals(" at 0 [character 1 line 1]", tokener.toString()); assertEquals('t',tokener.next()); assertEquals(" at 1 [character 2 line 1]", tokener.toString()); tokener.skipTo('\n'); assertEquals("skipTo() improperly modifying indexes"," at 7 [character 8 line 1]", tokener.toString()); assertEquals('\n',tokener.next()); assertEquals(" at 8 [character 0 line 2]", tokener.toString()); assertEquals('A',tokener.next()); assertEquals(" at 9 [character 1 line 2]", tokener.toString()); tokener.back(); assertEquals(" at 8 [character 0 line 2]", tokener.toString()); tokener.skipTo('\r'); assertEquals("skipTo() improperly modifying indexes"," at 14 [character 6 line 2]", tokener.toString()); // verify \r\n combo doesn't increment the line twice assertEquals('\r', tokener.next()); assertEquals(" at 15 [character 0 line 3]", tokener.toString()); assertEquals('\n', tokener.next()); assertEquals(" at 16 [character 0 line 3]", tokener.toString()); // verify stepping back after reading the \n of an \r\n combo doesn't increment the line incorrectly tokener.back(); assertEquals(" at 15 [character 6 line 2]", tokener.toString()); assertEquals('\n', tokener.next()); assertEquals(" at 16 [character 0 line 3]", tokener.toString()); assertEquals('W', tokener.next()); assertEquals(" at 17 [character 1 line 3]", tokener.toString()); assertEquals('i', tokener.next()); assertEquals(" at 18 [character 2 line 3]", tokener.toString()); tokener.skipTo('\r'); assertEquals("skipTo() improperly modifying indexes"," at 35 [character 19 line 3]", tokener.toString()); assertEquals('\r', tokener.next()); assertEquals(" at 36 [character 0 line 4]", tokener.toString()); tokener.back(); assertEquals(" at 35 [character 19 line 3]", tokener.toString()); assertEquals('\r', tokener.next()); assertEquals(" at 36 [character 0 line 4]", tokener.toString()); assertEquals('N', tokener.next()); assertEquals(" at 37 [character 1 line 4]", tokener.toString()); // verify we get the same data just walking though, no calls to back final JSONTokener t2 = new JSONTokener(testString); for(int i=0; i<7; i++) { assertTrue(t2.toString().startsWith(" at " + i + " ")); assertEquals(testString.charAt(i), t2.next()); } assertEquals(" at 7 [character 8 line 1]", t2.toString()); assertEquals(testString.charAt(7), t2.next()); assertEquals(" at 8 [character 0 line 2]", t2.toString()); for(int i=8; i<14; i++) { assertTrue(t2.toString().startsWith(" at " + i + " ")); assertEquals(testString.charAt(i), t2.next()); } assertEquals(" at 14 [character 6 line 2]", t2.toString()); assertEquals('\r', t2.next()); assertEquals(" at 15 [character 0 line 3]", t2.toString()); assertEquals('\n', t2.next()); assertEquals(" at 16 [character 0 line 3]", t2.toString()); assertEquals('W', t2.next()); assertEquals(" at 17 [character 1 line 3]", t2.toString()); for(int i=17; i<37; i++) { assertTrue(t2.toString().startsWith(" at " + i + " ")); assertEquals(testString.charAt(i), t2.next()); } assertEquals(" at 37 [character 1 line 4]", t2.toString()); for(int i=37; i<testString.length(); i++) { assertTrue(t2.toString().startsWith(" at " + i + " ")); assertEquals(testString.charAt(i), t2.next()); } assertEquals(" at "+ testString.length() +" [character 9 line 4]", t2.toString()); // end of the input assertEquals(0, t2.next()); assertFalse(t2.more()); }