AJsonSerHelper { public void writeNumberLiteral (long value, int numFracDigits) throws IOException { checkAcceptsValueAndPrefixComma (); if (value < 0) { out.write ('-'); value = -value; } if (numFracDigits == 0) { out.write (String.valueOf (value)); } else { final long intPart = value / TEN_POW[numFracDigits]; final String fracPart = String.valueOf (1_000_000_000 + value % TEN_POW[numFracDigits]).substring (10 - numFracDigits, 10); out.write (String.valueOf (intPart)); out.write ("."); out.write (fracPart); } afterValueWritten (); } AJsonSerHelper(OutputStream out); void startObject(); void endObject(); void writeKey(String key); void startArray(); void endArray(); void writeStringLiteral(String s); void writeNumberLiteral(long value, int numFracDigits); void writeNumberLiteral(double value, int numFracDigits); void writeBooleanLiteral(boolean value); void writeNullLiteral(); void writeIntLiteral(int value); void writeLongLiteral(long value); void writeStringArray(Iterable<String> values); void writeStringField(String key, String value); void writeBooleanField(String key, Boolean value); void writeIntField(String key, Integer value); void writeLongField(String key, Long value); static String buildString(AStatement1<AJsonSerHelper, E> code); static final Charset UTF_8; }
@Test public void testNegativeDouble() throws IOException { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final AJsonSerHelper ser = new AJsonSerHelper(baos); ser.writeNumberLiteral(-1.23, 9); final String result = new String(baos.toByteArray(), "utf-8"); assertEquals("-1.230000000", result); }
@Test public void testNumber() throws IOException { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final AJsonSerHelper ser = new AJsonSerHelper(baos); ser.writeNumberLiteral(12345, 0); final String result = new String(baos.toByteArray(), "utf-8"); assertEquals("12345", result); }
@Test public void testNumberWithFrac1() throws IOException { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final AJsonSerHelper ser = new AJsonSerHelper(baos); ser.writeNumberLiteral(12345, 1); final String result = new String(baos.toByteArray(), "utf-8"); assertEquals("1234.5", result); }
@Test public void testNumberWithFrac9() throws IOException { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final AJsonSerHelper ser = new AJsonSerHelper(baos); ser.writeNumberLiteral(1234567890, 9); final String result = new String(baos.toByteArray(), "utf-8"); assertEquals("1.234567890", result); }
@Test public void testDoubleWithFrac0() throws IOException { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final AJsonSerHelper ser = new AJsonSerHelper(baos); ser.writeNumberLiteral(1.23, 0); final String result = new String(baos.toByteArray(), "utf-8"); assertEquals("1", result); }
@Test public void testDoubleWithFrac1() throws IOException { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final AJsonSerHelper ser = new AJsonSerHelper(baos); ser.writeNumberLiteral(1.23, 1); final String result = new String(baos.toByteArray(), "utf-8"); assertEquals("1.2", result); }
@Test public void testDoubleWithFrac9() throws IOException { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final AJsonSerHelper ser = new AJsonSerHelper(baos); ser.writeNumberLiteral(1.23, 9); final String result = new String(baos.toByteArray(), "utf-8"); assertEquals("1.230000000", result); }
@Test public void testNegativeNumber1() throws IOException { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final AJsonSerHelper ser = new AJsonSerHelper(baos); ser.writeNumberLiteral(-1234567890, 1); final String result = new String(baos.toByteArray(), "utf-8"); assertEquals("-123456789.0", result); }
@Test public void testNegativeNumber9() throws IOException { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final AJsonSerHelper ser = new AJsonSerHelper(baos); ser.writeNumberLiteral(-1234567890, 9); final String result = new String(baos.toByteArray(), "utf-8"); assertEquals("-1.234567890", result); }