@Test public void testNumber() throws IOException { assertEquals("42", encode(42L)); assertEquals("42.5", encode(42.5)); assertEquals("-789", encode(-789)); assertEquals("-789.1", encode(-789.10)); }
private void encode(Object value, Writer writer) throws IOException { if (value instanceof CharSequence) { CharSequence text = (CharSequence)value; writer.write("\""); for (int i = 0, n = text.length(); i < n; i++) { char c = text.charAt(i); if (c == '"' || c == '\\') { writer.write("\\" + c); } else if (c == '\b') { writer.write("\\b"); } else if (c == '\f') { writer.write("\\f"); } else if (c == '\n') { writer.write("\\n"); } else if (c == '\r') { writer.write("\\r"); } else if (c == '\t') { writer.write("\\t"); } else { writer.write(c); } } writer.write("\""); } else if (value instanceof Number || value instanceof Boolean) { writer.write(value.toString()); } else if (value instanceof Enum<?>) { encode(((Enum<?>)value).ordinal(), writer); } else if (value instanceof Date) { encode(((Date)value).getTime(), writer); } else if (value instanceof LocalDate || value instanceof LocalTime || value instanceof LocalDateTime) { encode(value.toString(), writer); } else if (value instanceof URL) { encode(value.toString(), writer); } else if (value instanceof Iterable<?>) { writer.write("["); depth++; int i = 0; for (Object element : (Iterable<?>)value) { if (i > 0) { writer.write(","); } if (!compact) { writer.write("\n"); indent(writer); } encode(element, writer); i++; } depth--; if (!compact) { writer.write("\n"); indent(writer); } writer.write("]"); } else if (value instanceof Map<?, ?>) { writer.write("{"); depth++; int i = 0; for (Map.Entry<?, ?> entry : ((Map<?, ?>)value).entrySet()) { Object key = entry.getKey(); if (key == null) { continue; } if (i > 0) { writer.write(","); } if (!compact) { writer.write("\n"); indent(writer); } encode(key.toString(), writer); writer.write(":"); if (!compact) { writer.write(" "); } encode(entry.getValue(), writer); i++; } depth--; if (!compact) { writer.write("\n"); indent(writer); } writer.write("}"); } else { writer.append(null); } }
JSONEncoder extends Encoder<Object> { private void encode(Object value, Writer writer) throws IOException { if (value instanceof CharSequence) { CharSequence text = (CharSequence)value; writer.write("\""); for (int i = 0, n = text.length(); i < n; i++) { char c = text.charAt(i); if (c == '"' || c == '\\') { writer.write("\\" + c); } else if (c == '\b') { writer.write("\\b"); } else if (c == '\f') { writer.write("\\f"); } else if (c == '\n') { writer.write("\\n"); } else if (c == '\r') { writer.write("\\r"); } else if (c == '\t') { writer.write("\\t"); } else { writer.write(c); } } writer.write("\""); } else if (value instanceof Number || value instanceof Boolean) { writer.write(value.toString()); } else if (value instanceof Enum<?>) { encode(((Enum<?>)value).ordinal(), writer); } else if (value instanceof Date) { encode(((Date)value).getTime(), writer); } else if (value instanceof LocalDate || value instanceof LocalTime || value instanceof LocalDateTime) { encode(value.toString(), writer); } else if (value instanceof URL) { encode(value.toString(), writer); } else if (value instanceof Iterable<?>) { writer.write("["); depth++; int i = 0; for (Object element : (Iterable<?>)value) { if (i > 0) { writer.write(","); } if (!compact) { writer.write("\n"); indent(writer); } encode(element, writer); i++; } depth--; if (!compact) { writer.write("\n"); indent(writer); } writer.write("]"); } else if (value instanceof Map<?, ?>) { writer.write("{"); depth++; int i = 0; for (Map.Entry<?, ?> entry : ((Map<?, ?>)value).entrySet()) { Object key = entry.getKey(); if (key == null) { continue; } if (i > 0) { writer.write(","); } if (!compact) { writer.write("\n"); indent(writer); } encode(key.toString(), writer); writer.write(":"); if (!compact) { writer.write(" "); } encode(entry.getValue(), writer); i++; } depth--; if (!compact) { writer.write("\n"); indent(writer); } writer.write("}"); } else { writer.append(null); } } }
JSONEncoder extends Encoder<Object> { private void encode(Object value, Writer writer) throws IOException { if (value instanceof CharSequence) { CharSequence text = (CharSequence)value; writer.write("\""); for (int i = 0, n = text.length(); i < n; i++) { char c = text.charAt(i); if (c == '"' || c == '\\') { writer.write("\\" + c); } else if (c == '\b') { writer.write("\\b"); } else if (c == '\f') { writer.write("\\f"); } else if (c == '\n') { writer.write("\\n"); } else if (c == '\r') { writer.write("\\r"); } else if (c == '\t') { writer.write("\\t"); } else { writer.write(c); } } writer.write("\""); } else if (value instanceof Number || value instanceof Boolean) { writer.write(value.toString()); } else if (value instanceof Enum<?>) { encode(((Enum<?>)value).ordinal(), writer); } else if (value instanceof Date) { encode(((Date)value).getTime(), writer); } else if (value instanceof LocalDate || value instanceof LocalTime || value instanceof LocalDateTime) { encode(value.toString(), writer); } else if (value instanceof URL) { encode(value.toString(), writer); } else if (value instanceof Iterable<?>) { writer.write("["); depth++; int i = 0; for (Object element : (Iterable<?>)value) { if (i > 0) { writer.write(","); } if (!compact) { writer.write("\n"); indent(writer); } encode(element, writer); i++; } depth--; if (!compact) { writer.write("\n"); indent(writer); } writer.write("]"); } else if (value instanceof Map<?, ?>) { writer.write("{"); depth++; int i = 0; for (Map.Entry<?, ?> entry : ((Map<?, ?>)value).entrySet()) { Object key = entry.getKey(); if (key == null) { continue; } if (i > 0) { writer.write(","); } if (!compact) { writer.write("\n"); indent(writer); } encode(key.toString(), writer); writer.write(":"); if (!compact) { writer.write(" "); } encode(entry.getValue(), writer); i++; } depth--; if (!compact) { writer.write("\n"); indent(writer); } writer.write("}"); } else { writer.append(null); } } JSONEncoder();  JSONEncoder(boolean compact); }
JSONEncoder extends Encoder<Object> { private void encode(Object value, Writer writer) throws IOException { if (value instanceof CharSequence) { CharSequence text = (CharSequence)value; writer.write("\""); for (int i = 0, n = text.length(); i < n; i++) { char c = text.charAt(i); if (c == '"' || c == '\\') { writer.write("\\" + c); } else if (c == '\b') { writer.write("\\b"); } else if (c == '\f') { writer.write("\\f"); } else if (c == '\n') { writer.write("\\n"); } else if (c == '\r') { writer.write("\\r"); } else if (c == '\t') { writer.write("\\t"); } else { writer.write(c); } } writer.write("\""); } else if (value instanceof Number || value instanceof Boolean) { writer.write(value.toString()); } else if (value instanceof Enum<?>) { encode(((Enum<?>)value).ordinal(), writer); } else if (value instanceof Date) { encode(((Date)value).getTime(), writer); } else if (value instanceof LocalDate || value instanceof LocalTime || value instanceof LocalDateTime) { encode(value.toString(), writer); } else if (value instanceof URL) { encode(value.toString(), writer); } else if (value instanceof Iterable<?>) { writer.write("["); depth++; int i = 0; for (Object element : (Iterable<?>)value) { if (i > 0) { writer.write(","); } if (!compact) { writer.write("\n"); indent(writer); } encode(element, writer); i++; } depth--; if (!compact) { writer.write("\n"); indent(writer); } writer.write("]"); } else if (value instanceof Map<?, ?>) { writer.write("{"); depth++; int i = 0; for (Map.Entry<?, ?> entry : ((Map<?, ?>)value).entrySet()) { Object key = entry.getKey(); if (key == null) { continue; } if (i > 0) { writer.write(","); } if (!compact) { writer.write("\n"); indent(writer); } encode(key.toString(), writer); writer.write(":"); if (!compact) { writer.write(" "); } encode(entry.getValue(), writer); i++; } depth--; if (!compact) { writer.write("\n"); indent(writer); } writer.write("}"); } else { writer.append(null); } } JSONEncoder();  JSONEncoder(boolean compact); @Override void write(Object value, Writer writer); }
JSONEncoder extends Encoder<Object> { private void encode(Object value, Writer writer) throws IOException { if (value instanceof CharSequence) { CharSequence text = (CharSequence)value; writer.write("\""); for (int i = 0, n = text.length(); i < n; i++) { char c = text.charAt(i); if (c == '"' || c == '\\') { writer.write("\\" + c); } else if (c == '\b') { writer.write("\\b"); } else if (c == '\f') { writer.write("\\f"); } else if (c == '\n') { writer.write("\\n"); } else if (c == '\r') { writer.write("\\r"); } else if (c == '\t') { writer.write("\\t"); } else { writer.write(c); } } writer.write("\""); } else if (value instanceof Number || value instanceof Boolean) { writer.write(value.toString()); } else if (value instanceof Enum<?>) { encode(((Enum<?>)value).ordinal(), writer); } else if (value instanceof Date) { encode(((Date)value).getTime(), writer); } else if (value instanceof LocalDate || value instanceof LocalTime || value instanceof LocalDateTime) { encode(value.toString(), writer); } else if (value instanceof URL) { encode(value.toString(), writer); } else if (value instanceof Iterable<?>) { writer.write("["); depth++; int i = 0; for (Object element : (Iterable<?>)value) { if (i > 0) { writer.write(","); } if (!compact) { writer.write("\n"); indent(writer); } encode(element, writer); i++; } depth--; if (!compact) { writer.write("\n"); indent(writer); } writer.write("]"); } else if (value instanceof Map<?, ?>) { writer.write("{"); depth++; int i = 0; for (Map.Entry<?, ?> entry : ((Map<?, ?>)value).entrySet()) { Object key = entry.getKey(); if (key == null) { continue; } if (i > 0) { writer.write(","); } if (!compact) { writer.write("\n"); indent(writer); } encode(key.toString(), writer); writer.write(":"); if (!compact) { writer.write(" "); } encode(entry.getValue(), writer); i++; } depth--; if (!compact) { writer.write("\n"); indent(writer); } writer.write("}"); } else { writer.append(null); } } JSONEncoder();  JSONEncoder(boolean compact); @Override void write(Object value, Writer writer);  }