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

The following examples show how to use org.json.JSONObject#stringToValue() . 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: qaf   File: StringUtil.java    License: MIT License 5 votes vote down vote up
/**
 * Try to convert a string into java primitive type, java object or null. If the
 * string can't be converted, return the string. From 3.0.0 It will return "" for empty string.
 * 
 * @param string
 * @return Object
 */
public static Object toObject(String string) {
	if(null==string || JSONObject.NULL==string){
		return null;
	}
	Object val = JSONObject.stringToValue(string);
	if(null==val || JSONObject.NULL==val){
		return null;
	}
	return val;
}
 
Example 2
/**
 * Check whether JSONObject handles large or high precision numbers correctly
 */
@Test
public void stringToValueNumbersTest() {
    assertTrue("-0 Should be a Double!",JSONObject.stringToValue("-0")  instanceof Double);
    assertTrue("-0.0 Should be a Double!",JSONObject.stringToValue("-0.0") instanceof Double);
    assertTrue("'-' Should be a String!",JSONObject.stringToValue("-") instanceof String);
    assertTrue( "0.2 should be a Double!",
            JSONObject.stringToValue( "0.2" ) instanceof Double );
    assertTrue( "Doubles should be Doubles, even when incorrectly converting floats!",
            JSONObject.stringToValue( new Double( "0.2f" ).toString() ) instanceof Double );
    /**
     * This test documents a need for BigDecimal conversion.
     */
    Object obj = JSONObject.stringToValue( "299792.457999999984" );
    assertTrue( "evaluates to 299792.458 double instead of 299792.457999999984 BigDecimal!",
             obj.equals(new Double(299792.458)) );
    assertTrue( "1 should be an Integer!",
            JSONObject.stringToValue( "1" ) instanceof Integer );
    assertTrue( "Integer.MAX_VALUE should still be an Integer!",
            JSONObject.stringToValue( new Integer( Integer.MAX_VALUE ).toString() ) instanceof Integer );
    assertTrue( "Large integers should be a Long!",
            JSONObject.stringToValue( new Long( Long.sum( Integer.MAX_VALUE, 1 ) ).toString() ) instanceof Long );
    assertTrue( "Long.MAX_VALUE should still be an Integer!",
            JSONObject.stringToValue( new Long( Long.MAX_VALUE ).toString() ) instanceof Long );

    String str = new BigInteger( new Long( Long.MAX_VALUE ).toString() ).add( BigInteger.ONE ).toString();
    assertTrue( "Really large integers currently evaluate to string",
            JSONObject.stringToValue(str).equals("9223372036854775808"));
}
 
Example 3
/**
 * Confirm behavior when JSONObject stringToValue() is called for an
 * empty string
 */
@Test
public void stringToValue() {
    String str = "";
    String valueStr = (String)(JSONObject.stringToValue(str));
    assertTrue("stringToValue() expected empty String, found "+valueStr,
            "".equals(valueStr));
}
 
Example 4
Source Project: RipplePower   File: Decompressor.java    License: Apache License 2.0 5 votes vote down vote up
private Object readValue() throws JSONException {
	switch (read(2)) {
	case 0:
		return new Integer(read(!bit() ? 4 : !bit() ? 7 : 14));
	case 1:
		byte[] bytes = new byte[256];
		int length = 0;
		while (true) {
			int c = read(4);
			if (c == endOfNumber) {
				break;
			}
			bytes[length] = bcd[c];
			length += 1;
		}
		Object value;
		try {
			value = JSONObject.stringToValue(new String(bytes, 0, length,
					"US-ASCII"));
		} catch (UnsupportedEncodingException e) {
			throw new JSONException(e);
		}
		this.values.register(value);
		return value;
	case 2:
		return getAndTick(this.values, this.bitreader);
	case 3:
		return readJSON();
	default:
		throw new JSONException("Impossible.");
	}
}
 
Example 5
Source Project: equalize-xpi-modules   File: Unzipper.java    License: MIT License 4 votes vote down vote up
private Object readValue() throws JSONException {
    switch (read(2)) {
    case 0:
        int nr_bits = !bit() ? 4 : !bit() ? 7 : 14;
        int integer = read(nr_bits);
        switch (nr_bits) {
        case 7:
            integer += int4;
            break;
        case 14:
            integer += int7;
            break;
        }
        return integer;
    case 1:
        byte[] bytes = new byte[256];
        int length = 0;
        while (true) {
            int c = read(4);
            if (c == endOfNumber) {
                break;
            }
            bytes[length] = bcd[c];
            length += 1;
        }
        Object value;
        try {
            value = JSONObject.stringToValue(new String(bytes, 0, length,
                    "US-ASCII"));
        } catch (java.io.UnsupportedEncodingException e) {
            throw new JSONException(e);
        }
        this.valuekeep.register(value);
        return value;
    case 2:
        return getAndTick(this.valuekeep, this.bitreader);
    case 3:
        return readJSON();
    default:
        throw new JSONException("Impossible.");
    }
}
 
Example 6
private Object readValue() throws JSONException {
	switch (read(2)) {
	case 0:
		final int nr_bits = !bit() ? 4 : !bit() ? 7 : 14;
		int integer = read(nr_bits);
		switch (nr_bits) {
		case 7:
			integer += int4;
			break;
		case 14:
			integer += int7;
			break;
		}
		return integer;
	case 1:
		final byte[] bytes = new byte[256];
		int length = 0;
		while (true) {
			final int c = read(4);
			if (c == endOfNumber) {
				break;
			}
			bytes[length] = bcd[c];
			length += 1;
		}
		Object value;
		try {
			value = JSONObject.stringToValue(new String(bytes, 0, length,
					"US-ASCII"));
		} catch (final java.io.UnsupportedEncodingException e) {
			throw new JSONException(e);
		}
		this.valuekeep.register(value);
		return value;
	case 2:
		return getAndTick(this.valuekeep, this.bitreader);
	case 3:
		return readJSON();
	default:
		throw new JSONException("Impossible.");
	}
}