Java Code Examples for org.json.JSONTokener#next()

The following examples show how to use org.json.JSONTokener#next() . 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: deserializejson.java    From openbd-core with GNU General Public License v3.0 6 votes vote down vote up
public static cfData getCfDataFromJSon(String jsonString, boolean strictMapping) throws Exception {
	if ( jsonString.isEmpty() )
		return cfStringData.EMPTY_STRING;
	else if (jsonString.startsWith("{"))
		return convertToCf(new JSONObject(jsonString), strictMapping);
	else if (jsonString.startsWith("["))
		return convertToCf(new JSONArray(jsonString), strictMapping);
	else{
		JSONTokener tokener = new JSONTokener( jsonString );
		Object value = tokener.nextValue();
		if ( tokener.next() > 0 ){
			throw new Exception("invalid JSON string");
		}
		
		if ( value instanceof String ){
			return new cfStringData( (String) value );
		}else if ( value instanceof Boolean ){
			return cfBooleanData.getcfBooleanData( (Boolean) value, ( (Boolean) value ).booleanValue() ? "true" : "false" );
		}else if ( value instanceof Number ){
			return cfNumberData.getNumber( (Number) value );
		}else if ( value == JSONObject.NULL ){
			return cfNullData.NULL;
		}else
			return new cfStringData( jsonString );
	}
}
 
Example 2
Source File: JSONTokenerTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
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 3
Source File: JSONTokenerTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
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 4
Source File: JSONTokenerTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public void testNextString() throws JSONException {
    assertEquals("", new JSONTokener("'").nextString('\''));
    assertEquals("", new JSONTokener("\"").nextString('\"'));
    assertEquals("ABC", new JSONTokener("ABC'DEF").nextString('\''));
    assertEquals("ABC", new JSONTokener("ABC'''DEF").nextString('\''));

    // nextString permits slash-escaping of arbitrary characters!
    assertEquals("ABC", new JSONTokener("A\\B\\C'DEF").nextString('\''));

    JSONTokener tokener = new JSONTokener(" 'abc' 'def' \"ghi\"");
    tokener.next();
    assertEquals('\'', tokener.next());
    assertEquals("abc", tokener.nextString('\''));
    tokener.next();
    assertEquals('\'', tokener.next());
    assertEquals("def", tokener.nextString('\''));
    tokener.next();
    assertEquals('"', tokener.next());
    assertEquals("ghi", tokener.nextString('\"'));
    assertFalse(tokener.more());
}
 
Example 5
Source File: JSONTokenerTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testNextNWithAllRemaining() throws JSONException {
    JSONTokener tokener = new JSONTokener("ABCDEF");
    tokener.next(3);
    try {
        tokener.next(3);
    } catch (JSONException e) {
        AssertionFailedError error = new AssertionFailedError("off-by-one error?");
        error.initCause(e);
        throw error;
    }
}
 
Example 6
Source File: JSONTokenerTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testNext0() throws JSONException {
    JSONTokener tokener = new JSONTokener("ABCDEF");
    tokener.next(5);
    tokener.next();
    try {
        tokener.next(0);
    } catch (JSONException e) {
        Error error = new AssertionFailedError("Returning an empty string should be valid");
        error.initCause(e);
        throw error;
    }
}
 
Example 7
Source File: JSONUtils.java    From orion.server with Eclipse Public License 1.0 4 votes vote down vote up
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 '}'");
        }
    }
}