JsonValueFactory { public JsonValue createJsonValue(String string) { if (charAtIs(string, 0, '"') && charAtIs(string, string.length() - 1, '"')) { return new StringValue(string.substring(1, string.length() - 1)); } else if ("true".equalsIgnoreCase(string) || "false".equalsIgnoreCase(string)) { return new BooleanValue(Boolean.parseBoolean(string)); } else if ("null".equalsIgnoreCase(string)) { return new NullValue(); } else { char c = string.charAt(0); if ((c >= '0' && c <= '9') || c == '.' || c == '-' || c == '+') { if (c == '0') { if (string.length() > 2 && (string.charAt(1) == 'x' || string.charAt(1) == 'X')) { try { return new LongValue(Long.parseLong(string.substring(2), 16)); } catch (NumberFormatException notHexNumber) { } } else { try { return new LongValue(Long.parseLong(string.substring(1), 8)); } catch (NumberFormatException notOctNumber) { try { return new LongValue(Long.parseLong(string)); } catch (NumberFormatException notLong) { try { return new DoubleValue(Double.parseDouble(string)); } catch (NumberFormatException notNumber) { } } } } } else { try { return new LongValue(Long.parseLong(string)); } catch (NumberFormatException notLong) { try { return new DoubleValue(Double.parseDouble(string)); } catch (NumberFormatException notNumber) { } } } } } return new StringValue(string); }  JsonValue createJsonValue(String string);  }
@Test public void createsStringValueWithoutQuotesFromString() { JsonValue jsonValue = jsonValueFactory.createJsonValue("\"to be or not to be\""); assertTrue(jsonValue.isString()); assertEquals("to be or not to be", jsonValue.getStringValue()); }
@Test public void createsStringValueFromString() { JsonValue jsonValue = jsonValueFactory.createJsonValue("to be or not to be"); assertTrue(jsonValue.isString()); assertEquals("to be or not to be", jsonValue.getStringValue()); }
@Test public void createsBooleanValueFromString() { JsonValue jsonValue = jsonValueFactory.createJsonValue("true"); assertTrue(jsonValue.isBoolean()); assertTrue(jsonValue.getBooleanValue()); }
@Test public void createsBooleanValueIgnoringCaseFromString() { JsonValue jsonValue = jsonValueFactory.createJsonValue("True"); assertTrue(jsonValue.isBoolean()); assertTrue(jsonValue.getBooleanValue()); }
@Test public void createsNullValueFromString() { JsonValue jsonValue = jsonValueFactory.createJsonValue("null"); assertTrue(jsonValue.isNull()); }
@Test public void createsNullValueIgnoringCaseFromString() { JsonValue jsonValue = jsonValueFactory.createJsonValue("Null"); assertTrue(jsonValue.isNull()); }
@UseDataProvider("numericValueData") @Test public void createsNumericValueFromString(String rawString, boolean isDouble, Number number) throws Exception { JsonValue jsonValue = jsonValueFactory.createJsonValue(rawString); assertEquals(isDouble, jsonValue.isDouble()); assertEquals(number, jsonValue.getNumberValue()); }
@Test public void createsStringValueWhenFailsToParseStringAsHexNUmber() throws Exception { JsonValue jsonValue = jsonValueFactory.createJsonValue("0xzzz"); assertTrue(jsonValue.isString()); assertEquals("0xzzz", jsonValue.getStringValue()); }
@Test public void createsStringValueWhenFailsToParseStringAsOctNUmber() throws Exception { JsonValue jsonValue = jsonValueFactory.createJsonValue("0zzz"); assertTrue(jsonValue.isString()); assertEquals("0zzz", jsonValue.getStringValue()); }
@Test public void createsStringValueWhenFailsToParseStringAsNUmber() throws Exception { JsonValue jsonValue = jsonValueFactory.createJsonValue("123x"); assertTrue(jsonValue.isString()); assertEquals("123x", jsonValue.getStringValue()); }