diff options
author | Brian Vosburgh | 2015-12-01 17:35:26 +0000 |
---|---|---|
committer | Brian Vosburgh | 2015-12-01 19:43:42 +0000 |
commit | bab2a16b01710278afeda505fc49f776c8fc6e2e (patch) | |
tree | 444ef070cdd22bd7273fa21be95de4ca2a7d0f62 /common | |
parent | 721607b8b6ec01789a6d769035ae17019c1cbcae (diff) | |
download | webtools.dali-bab2a16b01710278afeda505fc49f776c8fc6e2e.tar.gz webtools.dali-bab2a16b01710278afeda505fc49f776c8fc6e2e.tar.xz webtools.dali-bab2a16b01710278afeda505fc49f776c8fc6e2e.zip |
add JSON generation support
Diffstat (limited to 'common')
4 files changed, 2108 insertions, 0 deletions
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/ObjectTools.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/ObjectTools.java index 1e456b2e16..7727056e10 100644 --- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/ObjectTools.java +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/ObjectTools.java @@ -506,6 +506,18 @@ public final class ObjectTools { } + // ********** JSON ********** + + /** + * @see #method(Object, String, Class[]) + */ + public static String toJSON(Object object) { + StringBuilder sb = new StringBuilder(); + StringBuilderTools.appendJSON(sb, object); + return sb.toString(); + } + + // ********** suppressed constructor ********** /** diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/StringBuilderTools.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/StringBuilderTools.java index e06ab071d0..7eaee51c74 100644 --- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/StringBuilderTools.java +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/StringBuilderTools.java @@ -9,7 +9,11 @@ ******************************************************************************/ package org.eclipse.jpt.common.utility.internal; +import java.lang.reflect.Field; import java.util.Iterator; +import java.util.Map; +import org.eclipse.jpt.common.utility.internal.comparator.ComparatorAdapter; +import org.eclipse.jpt.common.utility.internal.iterable.IterableTools; /** * {@link StringBuilder} utility methods. @@ -1985,6 +1989,1168 @@ public final class StringBuilderTools { } + // ********** JSON ********** + + /** + * Append the JSON representation of the specified object. + * <ul> + * <li>Nulls are appended as JSON nulls. + * <li>Strings are delimited with double quotes and escaped appropriately. + * <li>Character arrays treated like strings. + * <li>Boolean primitives and wrappers are appended as JSON booleans. + * <li>Maps with string keys are appended as JSON objects. + * <li>Iterables, object arrrays, and primitive arrays are appended as JSON arrays. + * <li>Number primitives and wrappers are appended as JSON numbers. + * <li>All other types of objects are appended as JSON objects, + * with the attribute values derived using Java reflection. + * </ul> + * Circular object references are not supported and will result in a + * {@link StackOverflowError}. + */ + public static void appendJSON(StringBuilder sb, Object object) { + if (object == null) { + appendJSONNull(sb); + } else { + appendJSON_(sb, object); + } + } + + /** + * Assume non-<code>null</code> object. + */ + private static void appendJSON_(StringBuilder sb, Object object) { + if (object instanceof String) { + appendJSON_(sb, (String) object); + } + else if (object instanceof char[]) { + appendJSON_(sb, (char[]) object); + } + else if (object instanceof Boolean) { + appendJSON_(sb, (Boolean) object); + } + else if (object instanceof Map) { + appendJSON_(sb, (Map<?, ?>) object); + } + else if (object instanceof Iterable) { + appendJSON_(sb, (Iterable<?>) object); + } + else if (object instanceof Object[]) { + appendJSON_(sb, (Object[]) object); + } + else if (object instanceof Number) { + appendJSON_(sb, (Number) object); + } + else if (object instanceof int[]) { + appendJSON_(sb, (int[]) object); + } + else if (object instanceof double[]) { + appendJSON_(sb, (double[]) object); + } + else if (object instanceof byte[]) { + appendJSON_(sb, (byte[]) object); + } + else if (object instanceof short[]) { + appendJSON_(sb, (short[]) object); + } + else if (object instanceof long[]) { + appendJSON_(sb, (long[]) object); + } + else if (object instanceof float[]) { + appendJSON_(sb, (float[]) object); + } + else if (object instanceof boolean[]) { + appendJSON_(sb, (boolean[]) object); + } + else { + appendJSON__(sb, object); + } + } + + /** + * Append the JSON representation of the specified map as a JSON object + * if all the map's keys are strings; otherwise append a reflectively-derived + * representation of the map. + * Circular references are not supported and will result in a + * {@link StackOverflowError}. + */ + public static void appendJSON(StringBuilder sb, Map<?, ?> map) { + if (map == null) { + appendJSONNull(sb); + } else { + appendJSON_(sb, map); + } + } + + /** + * Assume non-<code>null</code> map. + */ + private static void appendJSON_(StringBuilder sb, Map<?, ?> map) { + int resetIndex = sb.length(); + boolean reset = false; + sb.append('{'); + Iterator<?> stream = map.entrySet().iterator(); + if (stream.hasNext()) { + do { + Map.Entry<?, ?> entry = (Map.Entry<?, ?>) stream.next(); + Object key = entry.getKey(); + if (key instanceof String) { + appendJSON(sb, (String) key); + } + else if (key instanceof char[]) { // unlikely (as not very useful) + appendJSON(sb, (char[]) key); + } + else { + sb.setLength(resetIndex); + reset = true; + break; + } + sb.append(':'); + appendJSON(sb, entry.getValue()); + sb.append(','); + } while (stream.hasNext()); + if ( ! reset) { + sb.setLength(sb.length() - 1); // strip off extra comma + } + } + if (reset) { + appendJSON__(sb, map); // object reflection + } else { + sb.append('}'); + } + } + + /** + * Assume non-<code>null</code> non-standard object. + */ + private static void appendJSON__(StringBuilder sb, Object object) { + sb.append('{'); + Iterator<Field> stream = IterableTools.sort(ClassTools.allInstanceFields(object.getClass()), FIELD_COMPARATOR).iterator(); + if (stream.hasNext()) { + do { + Field field = stream.next(); + appendJSON(sb, field.getName()); + sb.append(':'); + field.setAccessible(true); + Object value; + try { + value = field.get(object); + } catch (IllegalAccessException ex) { + throw new RuntimeException(ex); + } + appendJSON(sb, value); + sb.append(','); + } while (stream.hasNext()); + sb.setLength(sb.length() - 1); // strip off extra comma + } + sb.append('}'); + } + + /** + * Compare fields' names. + */ + private static final ComparatorAdapter<Field> FIELD_COMPARATOR = new FieldComparator(); + static class FieldComparator + extends ComparatorAdapter<Field> + { + @Override + public int compare(Field field1, Field field2) { + return field1.getName().compareTo(field2.getName()); + } + } + + /** + * Append a JSON null. + */ + public static void appendJSONNull(StringBuilder sb) { + sb.append(String.valueOf((Object) null)); // "null" + } + + /** + * Append the JSON representation of the specified string, + * delimitint it with double quotes and escaping the appropriate characters. + */ + public static void appendJSON(StringBuilder sb, String string) { + if (string == null) { + appendJSONNull(sb); + } else { + appendJSON_(sb, string); + } + } + + /** + * Assume non-<code>null</code> string. + */ + private static void appendJSON_(StringBuilder sb, String string) { + int stringLength = string.length(); + if (stringLength == 0) { + sb.append(EMPTY_JSON_STRING_LITERAL); + } else { + sb.ensureCapacity(sb.length() + stringLength + 6); + appendJSON_(sb, string, stringLength); + } + } + + /** + * Assume non-<code>null</code> non-empty string. + */ + private static void appendJSON_(StringBuilder sb, String string, int stringLength) { + sb.append(CharacterTools.QUOTE); + appendJSONContent_(sb, string, stringLength); + sb.append(CharacterTools.QUOTE); + } + + /** + * Append the JSON string literal content for the specified string, + * escaping the appropriate characters. + */ + public static void appendJSONContent(StringBuilder sb, String string) { + if (string == null) { + appendJSONNull(sb); + } else { + appendJSONContent_(sb, string); + } + } + + /** + * Assume non-<code>null</code> string. + */ + private static void appendJSONContent_(StringBuilder sb, String string) { + int stringLength = string.length(); + if (stringLength != 0) { + sb.ensureCapacity(sb.length() + stringLength + 6); + appendJSONContent_(sb, string, stringLength); + } + } + + /** + * Assume non-<code>null</code> string. + */ + private static void appendJSONContent_(StringBuilder sb, String string, int stringLength) { + for (int i = 0; i < stringLength; i++) { + appendJSON(sb, string.charAt(i)); + } + } + + /** + * Append the JSON representation of the specified string, + * delimitint it with double quotes and escaping the appropriate characters. + */ + public static void appendJSON(StringBuilder sb, char[] string) { + if (string == null) { + appendJSONNull(sb); + } else { + appendJSON_(sb, string); + } + } + + /** + * Assume non-<code>null</code> string. + */ + private static void appendJSON_(StringBuilder sb, char[] string) { + int stringLength = string.length; + if (stringLength == 0) { + sb.append(EMPTY_JSON_STRING_LITERAL); + } else { + sb.ensureCapacity(sb.length() + stringLength + 6); + appendJSON_(sb, string, stringLength); + } + } + + /** + * Assume non-<code>null</code> non-empty string. + */ + private static void appendJSON_(StringBuilder sb, char[] string, int stringLength) { + sb.append(CharacterTools.QUOTE); + appendJSONContent_(sb, string, stringLength); + sb.append(CharacterTools.QUOTE); + } + + /** + * Append the JSON string literal content for the specified string, + * escaping the appropriate characters. + */ + public static void appendJSONContent(StringBuilder sb, char[] string) { + if (string == null) { + appendJSONNull(sb); + } else { + appendJSONContent_(sb, string); + } + } + + /** + * Assume non-<code>null</code> string. + */ + private static void appendJSONContent_(StringBuilder sb, char[] string) { + int stringLength = string.length; + if (stringLength != 0) { + sb.ensureCapacity(sb.length() + stringLength + 6); + appendJSONContent_(sb, string, stringLength); + } + } + + /** + * Assume non-<code>null</code> string. + */ + private static void appendJSONContent_(StringBuilder sb, char[] string, int stringLength) { + for (int i = 0; i < stringLength; i++) { + appendJSON(sb, string[i]); + } + } + + /** + * Escape the character if necessary. + */ + private static void appendJSON(StringBuilder sb, char c) { + switch (c) { + case '"': // double-quote + sb.append("\\\""); //$NON-NLS-1$ + break; + case '\\': // backslash + sb.append("\\\\"); //$NON-NLS-1$ + break; +// case '/': // slash +// sb.append("\\/"); //$NON-NLS-1$ +// break; + case '\b': // backspace + sb.append("\\b"); //$NON-NLS-1$ + break; + case '\f': // form-feed FF + sb.append("\\f"); //$NON-NLS-1$ + break; + case '\n': // line-feed LF + sb.append("\\n"); //$NON-NLS-1$ + break; + case '\r': // carriage-return CR + sb.append("\\r"); //$NON-NLS-1$ + break; + case '\t': // horizontal tab + sb.append("\\t"); //$NON-NLS-1$ + break; + default: + if (c < 32) { + if (c < 16) { + sb.append("\\u000"); //$NON-NLS-1$ + sb.append(Integer.toHexString(c)); + } else { + sb.append("\\u00"); //$NON-NLS-1$ + sb.append(Integer.toHexString(c)); + } + } else { + sb.append(c); + } + break; + } + } + + /** + * Append a JSON array representing the specified iterable. + */ + public static void appendJSON(StringBuilder sb, Iterable<?> objects) { + if (objects == null) { + appendJSONNull(sb); + } else { + appendJSON_(sb, objects); + } + } + + /** + * Assume non-<code>null</code> iterable. + */ + private static void appendJSON_(StringBuilder sb, Iterable<?> objects) { + sb.append('['); + Iterator<?> stream = objects.iterator(); + if (stream.hasNext()) { + do { + appendJSON(sb, stream.next()); + sb.append(','); + } while (stream.hasNext()); + sb.setLength(sb.length() - 1); // strip off extra comma + } + sb.append(']'); + } + + /** + * Append a JSON array representing the specified array. + */ + public static void appendJSON(StringBuilder sb, Object[] array) { + if (array == null) { + appendJSONNull(sb); + } else { + appendJSON_(sb, array); + } + } + + /** + * Assume non-<code>null</code> array. + */ + private static void appendJSON_(StringBuilder sb, Object[] array) { + if (array instanceof Boolean[]) { + appendJSON_(sb, (Boolean[]) array); + } + else if (array instanceof Number[]) { + appendJSON_(sb, (Number[]) array); + } + else { + appendJSON__(sb, array); + } + } + + /** + * Assume non-<code>null</code> non-standard array. + */ + private static void appendJSON__(StringBuilder sb, Object[] array) { + sb.append('['); + if (array.length > 0) { + for (Object each : array) { + appendJSON(sb, each); + sb.append(','); + } + sb.setLength(sb.length() - 1); // strip off extra comma + } + sb.append(']'); + } + + /** + * Append a JSON array representing the specified array. + */ + public static void appendJSON(StringBuilder sb, Boolean[] array) { + if (array == null) { + appendJSONNull(sb); + } else { + appendJSON_(sb, array); + } + } + + /** + * Assume non-<code>null</code> array. + */ + private static void appendJSON_(StringBuilder sb, Boolean[] array) { + sb.append('['); + if (array.length > 0) { + for (Boolean each : array) { + appendJSON(sb, each); + sb.append(','); + } + sb.setLength(sb.length() - 1); // strip off extra comma + } + sb.append(']'); + } + + public static void appendJSON(StringBuilder sb, Boolean b) { + if (b == null) { + appendJSONNull(sb); + } else { + appendJSON_(sb, b); + } + } + + /** + * Assume non-<code>null</code> number. + */ + private static void appendJSON_(StringBuilder sb, Boolean b) { + appendJSON(sb, b.booleanValue()); + } + + public static void appendJSON(StringBuilder sb, boolean[] array) { + if (array == null) { + appendJSONNull(sb); + } else { + appendJSON_(sb, array); + } + } + + /** + * Assume non-<code>null</code> array. + */ + private static void appendJSON_(StringBuilder sb, boolean[] array) { + sb.append('['); + if (array.length > 0) { + for (boolean each : array) { + appendJSON(sb, each); + sb.append(','); + } + sb.setLength(sb.length() - 1); // strip off extra comma + } + sb.append(']'); + } + + public static void appendJSON(StringBuilder sb, boolean b) { + sb.append(b); + } + + /** + * Append a JSON array representing the specified array. + */ + public static void appendJSON(StringBuilder sb, Number[] array) { + if (array == null) { + appendJSONNull(sb); + } else { + appendJSON_(sb, array); + } + } + + /** + * Assume non-<code>null</code> array. + */ + private static void appendJSON_(StringBuilder sb, Number[] array) { + if (array instanceof Integer[]) { + appendJSON_(sb, (Integer[]) array); + } + else if (array instanceof Double[]) { + appendJSON_(sb, (Double[]) array); + } + else if (array instanceof Byte[]) { + appendJSON_(sb, (Byte[]) array); + } + else if (array instanceof Float[]) { + appendJSON_(sb, (Float[]) array); + } + else if (array instanceof Long[]) { + appendJSON_(sb, (Long[]) array); + } + else if (array instanceof Short[]) { + appendJSON_(sb, (Short[]) array); + } + else if (array instanceof java.math.BigDecimal[]) { + appendJSON_(sb, (java.math.BigDecimal[]) array); + } + else if (array instanceof java.math.BigInteger[]) { + appendJSON_(sb, (java.math.BigInteger[]) array); + } + else { + appendJSON__(sb, array); + } + } + + /** + * Append a JSON number literal representing the specified number. + */ + public static void appendJSON(StringBuilder sb, Number n) { + if (n == null) { + appendJSONNull(sb); + } else { + appendJSON_(sb, n); + } + } + + /** + * Assume non-<code>null</code> number. + */ + private static void appendJSON_(StringBuilder sb, Number n) { + if (n instanceof Integer) { + appendJSON_(sb, (Integer) n); + } + else if (n instanceof Double) { + appendJSON_(sb, (Double) n); + } + else if (n instanceof Byte) { + appendJSON_(sb, (Byte) n); + } + else if (n instanceof Float) { + appendJSON_(sb, (Float) n); + } + else if (n instanceof Long) { + appendJSON_(sb, (Long) n); + } + else if (n instanceof Short) { + appendJSON_(sb, (Short) n); + } + else if (n instanceof java.math.BigDecimal) { + appendJSON_(sb, (java.math.BigDecimal) n); + } + else if (n instanceof java.math.BigInteger) { + appendJSON_(sb, (java.math.BigInteger) n); + } + else { + appendJSON__(sb, n); + } + } + + /** + * Assume non-<code>null</code> non-standard number. + */ + private static void appendJSON__(StringBuilder sb, Number n) { + String s = n.toString(); + try { + @SuppressWarnings("unused") + java.math.BigDecimal bd = new java.math.BigDecimal(s); + sb.append(s); // toString() produces valid "number" + } catch (NumberFormatException ex) { + appendJSON__(sb, (Object) n); // object reflection + } + } + + /** + * Append a JSON array representing the specified array. + */ + public static void appendJSON(StringBuilder sb, Integer[] array) { + if (array == null) { + appendJSONNull(sb); + } else { + appendJSON_(sb, array); + } + } + + /** + * Assume non-<code>null</code> array. + */ + private static void appendJSON_(StringBuilder sb, Integer[] array) { + sb.append('['); + if (array.length > 0) { + for (Integer each : array) { + appendJSON(sb, each); + sb.append(','); + } + sb.setLength(sb.length() - 1); // strip off extra comma + } + sb.append(']'); + } + + /** + * Append a JSON number literal representing the specified number. + */ + public static void appendJSON(StringBuilder sb, Integer i) { + if (i == null) { + appendJSONNull(sb); + } else { + appendJSON_(sb, i); + } + } + + /** + * Assume non-<code>null</code> number. + */ + private static void appendJSON_(StringBuilder sb, Integer i) { + appendJSON(sb, i.intValue()); + } + + /** + * Append a JSON array representing the specified array. + */ + public static void appendJSON(StringBuilder sb, int[] array) { + if (array == null) { + appendJSONNull(sb); + } else { + appendJSON_(sb, array); + } + } + + /** + * Assume non-<code>null</code> array. + */ + private static void appendJSON_(StringBuilder sb, int[] array) { + sb.append('['); + if (array.length > 0) { + for (int each : array) { + appendJSON(sb, each); + sb.append(','); + } + sb.setLength(sb.length() - 1); // strip off extra comma + } + sb.append(']'); + } + + /** + * Append a JSON number literal representing the specified number. + */ + public static void appendJSON(StringBuilder sb, int i) { + sb.append(i); + } + + /** + * Append a JSON array representing the specified array. + */ + public static void appendJSON(StringBuilder sb, Double[] array) { + if (array == null) { + appendJSONNull(sb); + } else { + appendJSON_(sb, array); + } + } + + /** + * Assume non-<code>null</code> array. + */ + private static void appendJSON_(StringBuilder sb, Double[] array) { + sb.append('['); + if (array.length > 0) { + for (Double each : array) { + appendJSON(sb, each); + sb.append(','); + } + sb.setLength(sb.length() - 1); // strip off extra comma + } + sb.append(']'); + } + + /** + * Append a JSON number literal representing the specified number. + */ + public static void appendJSON(StringBuilder sb, Double d) { + if (d == null) { + appendJSONNull(sb); + } else { + appendJSON_(sb, d); + } + } + + /** + * Assume non-<code>null</code> number. + */ + private static void appendJSON_(StringBuilder sb, Double d) { + appendJSON(sb, d.doubleValue()); + } + + /** + * Append a JSON array representing the specified array. + */ + public static void appendJSON(StringBuilder sb, double[] array) { + if (array == null) { + appendJSONNull(sb); + } else { + appendJSON_(sb, array); + } + } + + /** + * Assume non-<code>null</code> array. + */ + private static void appendJSON_(StringBuilder sb, double[] array) { + sb.append('['); + if (array.length > 0) { + for (double each : array) { + appendJSON(sb, each); + sb.append(','); + } + sb.setLength(sb.length() - 1); // strip off extra comma + } + sb.append(']'); + } + + /** + * Append a JSON number literal representing the specified number. + */ + public static void appendJSON(StringBuilder sb, double d) { + sb.append(d); + } + + /** + * Append a JSON array representing the specified array. + */ + public static void appendJSON(StringBuilder sb, Byte[] array) { + if (array == null) { + appendJSONNull(sb); + } else { + appendJSON_(sb, array); + } + } + + /** + * Assume non-<code>null</code> array. + */ + private static void appendJSON_(StringBuilder sb, Byte[] array) { + sb.append('['); + if (array.length > 0) { + for (Byte each : array) { + appendJSON(sb, each); + sb.append(','); + } + sb.setLength(sb.length() - 1); // strip off extra comma + } + sb.append(']'); + } + + /** + * Append a JSON number literal representing the specified number. + */ + public static void appendJSON(StringBuilder sb, Byte b) { + if (b == null) { + appendJSONNull(sb); + } else { + appendJSON_(sb, b); + } + } + + /** + * Assume non-<code>null</code> number. + */ + private static void appendJSON_(StringBuilder sb, Byte b) { + appendJSON(sb, b.byteValue()); + } + + /** + * Append a JSON array representing the specified array. + */ + public static void appendJSON(StringBuilder sb, byte[] array) { + if (array == null) { + appendJSONNull(sb); + } else { + appendJSON_(sb, array); + } + } + + /** + * Assume non-<code>null</code> array. + */ + private static void appendJSON_(StringBuilder sb, byte[] array) { + sb.append('['); + if (array.length > 0) { + for (byte each : array) { + appendJSON(sb, each); + sb.append(','); + } + sb.setLength(sb.length() - 1); // strip off extra comma + } + sb.append(']'); + } + + /** + * Append a JSON number literal representing the specified number. + */ + public static void appendJSON(StringBuilder sb, byte b) { + sb.append(b); + } + + /** + * Append a JSON array representing the specified array. + */ + public static void appendJSON(StringBuilder sb, Float[] array) { + if (array == null) { + appendJSONNull(sb); + } else { + appendJSON_(sb, array); + } + } + + /** + * Assume non-<code>null</code> array. + */ + private static void appendJSON_(StringBuilder sb, Float[] array) { + sb.append('['); + if (array.length > 0) { + for (Float each : array) { + appendJSON(sb, each); + sb.append(','); + } + sb.setLength(sb.length() - 1); // strip off extra comma + } + sb.append(']'); + } + + /** + * Append a JSON number literal representing the specified number. + */ + public static void appendJSON(StringBuilder sb, Float f) { + if (f == null) { + appendJSONNull(sb); + } else { + appendJSON_(sb, f); + } + } + + /** + * Assume non-<code>null</code> number. + */ + private static void appendJSON_(StringBuilder sb, Float f) { + appendJSON(sb, f.floatValue()); + } + + /** + * Append a JSON array representing the specified array. + */ + public static void appendJSON(StringBuilder sb, float[] array) { + if (array == null) { + appendJSONNull(sb); + } else { + appendJSON_(sb, array); + } + } + + /** + * Assume non-<code>null</code> array. + */ + private static void appendJSON_(StringBuilder sb, float[] array) { + sb.append('['); + if (array.length > 0) { + for (float each : array) { + appendJSON(sb, each); + sb.append(','); + } + sb.setLength(sb.length() - 1); // strip off extra comma + } + sb.append(']'); + } + + /** + * Append a JSON number literal representing the specified number. + */ + public static void appendJSON(StringBuilder sb, float f) { + sb.append(f); + } + + /** + * Append a JSON array representing the specified array. + */ + public static void appendJSON(StringBuilder sb, Long[] array) { + if (array == null) { + appendJSONNull(sb); + } else { + appendJSON_(sb, array); + } + } + + /** + * Assume non-<code>null</code> array. + */ + private static void appendJSON_(StringBuilder sb, Long[] array) { + sb.append('['); + if (array.length > 0) { + for (Long each : array) { + appendJSON(sb, each); + sb.append(','); + } + sb.setLength(sb.length() - 1); // strip off extra comma + } + sb.append(']'); + } + + /** + * Append a JSON number literal representing the specified number. + */ + public static void appendJSON(StringBuilder sb, Long l) { + if (l == null) { + appendJSONNull(sb); + } else { + appendJSON_(sb, l); + } + } + + /** + * Assume non-<code>null</code> number. + */ + private static void appendJSON_(StringBuilder sb, Long l) { + appendJSON(sb, l.longValue()); + } + + /** + * Append a JSON array representing the specified array. + */ + public static void appendJSON(StringBuilder sb, long[] array) { + if (array == null) { + appendJSONNull(sb); + } else { + appendJSON_(sb, array); + } + } + + /** + * Assume non-<code>null</code> array. + */ + private static void appendJSON_(StringBuilder sb, long[] array) { + sb.append('['); + if (array.length > 0) { + for (long each : array) { + appendJSON(sb, each); + sb.append(','); + } + sb.setLength(sb.length() - 1); // strip off extra comma + } + sb.append(']'); + } + + /** + * Append a JSON number literal representing the specified number. + */ + public static void appendJSON(StringBuilder sb, long l) { + sb.append(l); + } + + /** + * Append a JSON array representing the specified array. + */ + public static void appendJSON(StringBuilder sb, Short[] array) { + if (array == null) { + appendJSONNull(sb); + } else { + appendJSON_(sb, array); + } + } + + /** + * Assume non-<code>null</code> array. + */ + private static void appendJSON_(StringBuilder sb, Short[] array) { + sb.append('['); + if (array.length > 0) { + for (Short each : array) { + appendJSON(sb, each); + sb.append(','); + } + sb.setLength(sb.length() - 1); // strip off extra comma + } + sb.append(']'); + } + + /** + * Append a JSON number literal representing the specified number. + */ + public static void appendJSON(StringBuilder sb, Short s) { + if (s == null) { + appendJSONNull(sb); + } else { + appendJSON_(sb, s); + } + } + + /** + * Assume non-<code>null</code> number. + */ + private static void appendJSON_(StringBuilder sb, Short s) { + appendJSON(sb, s.shortValue()); + } + + /** + * Append a JSON array representing the specified array. + */ + public static void appendJSON(StringBuilder sb, short[] array) { + if (array == null) { + appendJSONNull(sb); + } else { + appendJSON_(sb, array); + } + } + + /** + * Assume non-<code>null</code> array. + */ + private static void appendJSON_(StringBuilder sb, short[] array) { + sb.append('['); + if (array.length > 0) { + for (short each : array) { + appendJSON(sb, each); + sb.append(','); + } + sb.setLength(sb.length() - 1); // strip off extra comma + } + sb.append(']'); + } + + /** + * Append a JSON number literal representing the specified number. + */ + public static void appendJSON(StringBuilder sb, short s) { + sb.append(s); + } + + /** + * Append a JSON array representing the specified array. + */ + public static void appendJSON(StringBuilder sb, java.math.BigDecimal[] array) { + if (array == null) { + appendJSONNull(sb); + } else { + appendJSON_(sb, array); + } + } + + /** + * Assume non-<code>null</code> array. + */ + private static void appendJSON_(StringBuilder sb, java.math.BigDecimal[] array) { + sb.append('['); + if (array.length > 0) { + for (java.math.BigDecimal each : array) { + appendJSON(sb, each); + sb.append(','); + } + sb.setLength(sb.length() - 1); // strip off extra comma + } + sb.append(']'); + } + + /** + * Append a JSON number literal representing the specified number. + */ + public static void appendJSON(StringBuilder sb, java.math.BigDecimal bd) { + if (bd == null) { + appendJSONNull(sb); + } else { + appendJSON_(sb, bd); + } + } + + /** + * Assume non-<code>null</code> number. + */ + private static void appendJSON_(StringBuilder sb, java.math.BigDecimal bd) { + sb.append(bd.toString()); + } + + /** + * Append a JSON array representing the specified array. + */ + public static void appendJSON(StringBuilder sb, java.math.BigInteger[] array) { + if (array == null) { + appendJSONNull(sb); + } else { + appendJSON_(sb, array); + } + } + + /** + * Assume non-<code>null</code> array. + */ + private static void appendJSON_(StringBuilder sb, java.math.BigInteger[] array) { + sb.append('['); + if (array.length > 0) { + for (java.math.BigInteger each : array) { + appendJSON(sb, each); + sb.append(','); + } + sb.setLength(sb.length() - 1); // strip off extra comma + } + sb.append(']'); + } + + /** + * Append a JSON number literal representing the specified number. + */ + public static void appendJSON(StringBuilder sb, java.math.BigInteger bi) { + if (bi == null) { + appendJSONNull(sb); + } else { + appendJSON_(sb, bi); + } + } + + /** + * Assume non-<code>null</code> number. + */ + private static void appendJSON_(StringBuilder sb, java.math.BigInteger bi) { + sb.append(bi.toString()); + } + + /** + * Assume non-<code>null</code> non-standard <code>Number</code> array. + */ + private static void appendJSON__(StringBuilder sb, Number[] array) { + sb.append('['); + if (array.length > 0) { + for (Number number : array) { + appendJSON(sb, number); + sb.append(','); + } + sb.setLength(sb.length() - 1); // strip off extra comma + } + sb.append(']'); + } + + /** + * Value: {@value} + */ + public static final String EMPTY_JSON_STRING_LITERAL = "\"\""; //$NON-NLS-1$ + + // ********** toString() helper methods ********** /** diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/META-INF/MANIFEST.MF b/common/tests/org.eclipse.jpt.common.utility.tests/META-INF/MANIFEST.MF index 8f49328f8b..a705ae0007 100644 --- a/common/tests/org.eclipse.jpt.common.utility.tests/META-INF/MANIFEST.MF +++ b/common/tests/org.eclipse.jpt.common.utility.tests/META-INF/MANIFEST.MF @@ -31,3 +31,4 @@ Export-Package: org.eclipse.jpt.common.utility.tests.internal;x-internal:=true, org.eclipse.jpt.common.utility.tests.internal.reference;x-internal:=true, org.eclipse.jpt.common.utility.tests.internal.swing;x-internal:=true, org.eclipse.jpt.common.utility.tests.internal.transformer;x-internal:=true +Import-Package: org.apache.commons.lang.math;version="2.6.0" diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/StringBuilderToolsTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/StringBuilderToolsTests.java index 0b33cb3c8a..a729a8bc1a 100644 --- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/StringBuilderToolsTests.java +++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/StringBuilderToolsTests.java @@ -9,6 +9,12 @@ ******************************************************************************/ package org.eclipse.jpt.common.utility.tests.internal; +import java.math.BigDecimal; +import java.math.BigInteger; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; +import org.eclipse.jpt.common.utility.internal.SimplePair; import org.eclipse.jpt.common.utility.internal.StringBuilderTools; import org.eclipse.jpt.common.utility.internal.iterable.IterableTools; import org.junit.Assert; @@ -428,6 +434,926 @@ public class StringBuilderToolsTests // ********** StringBuilderTools-specific ********** + // ********** JSON ********** + + public void testAppendJSONMap() throws Exception { + StringBuilder sb = new StringBuilder(); + Map<String, String> map = new HashMap<>(); + map.put("left", "name"); + map.put("right", "Fred"); + StringBuilderTools.appendJSON(sb, map); + assertEquals("{\"left\":\"name\",\"right\":\"Fred\"}", sb.toString()); + sb = new StringBuilder(); + StringBuilderTools.appendJSON(sb, (Object) map); + assertEquals("{\"left\":\"name\",\"right\":\"Fred\"}", sb.toString()); + } + + public void testAppendJSONMap_charArray() throws Exception { + StringBuilder sb = new StringBuilder(); + Map<char[], String> map = new HashMap<>(); + map.put("left".toCharArray(), "name"); + map.put("right".toCharArray(), "Fred"); + StringBuilderTools.appendJSON(sb, map); + assertEquals("{\"left\":\"name\",\"right\":\"Fred\"}", sb.toString()); + sb = new StringBuilder(); + StringBuilderTools.appendJSON(sb, (Object) map); + assertEquals("{\"left\":\"name\",\"right\":\"Fred\"}", sb.toString()); + } + + public void testAppendJSONMap_reflection() throws Exception { + StringBuilder sb = new StringBuilder(); + Map<Object, String> map = new HashMap<>(); + map.put(Integer.valueOf(42), "Fred"); + StringBuilderTools.appendJSON(sb, map); + String expected = "{\"entrySet\":[{\"hash\":42,\"key\":42,\"next\":null,\"value\":\"Fred\"}],\"keySet\":null,\"loadFactor\":0.75,\"modCount\":1,\"size\":1,\"table\":[null,null,null,null,null,null,null,null,null,null,{\"hash\":42,\"key\":42,\"next\":null,\"value\":\"Fred\"},null,null,null,null,null],\"threshold\":12,\"values\":null}"; + assertEquals(expected, sb.toString()); + sb = new StringBuilder(); + StringBuilderTools.appendJSON(sb, (Object) map); + assertEquals(expected, sb.toString()); + } + + public void testAppendJSONMap_empty() throws Exception { + StringBuilder sb = new StringBuilder(); + StringBuilderTools.appendJSON(sb, new HashMap<>()); + assertEquals("{}", sb.toString()); + } + + public void testAppendJSONMap_null() throws Exception { + StringBuilder sb = new StringBuilder(); + Map<String, String> map = null; + StringBuilderTools.appendJSON(sb, map); + assertEquals("null", sb.toString()); + } + + public void testAppendJSONString() throws Exception { + StringBuilder sb = new StringBuilder(); + String s = "foo"; + StringBuilderTools.appendJSON(sb, s); + assertEquals("\"foo\"", sb.toString()); + sb = new StringBuilder(); + StringBuilderTools.appendJSON(sb, (Object) s); + assertEquals("\"foo\"", sb.toString()); + } + + public void testAppendJSONString_specialChars() throws Exception { + StringBuilder sb = new StringBuilder(); + String s = "\"foo\""; + StringBuilderTools.appendJSON(sb, s); + assertEquals("\"\\\"foo\\\"\"", sb.toString()); + + sb = new StringBuilder(); + s = "\\"; + StringBuilderTools.appendJSON(sb, s); + assertEquals("\"\\\\\"", sb.toString()); + + sb = new StringBuilder(); + s = "\b"; + StringBuilderTools.appendJSON(sb, s); + assertEquals("\"\\b\"", sb.toString()); + + sb = new StringBuilder(); + s = "\f"; + StringBuilderTools.appendJSON(sb, s); + assertEquals("\"\\f\"", sb.toString()); + + sb = new StringBuilder(); + s = "\n"; + StringBuilderTools.appendJSON(sb, s); + assertEquals("\"\\n\"", sb.toString()); + + sb = new StringBuilder(); + s = "\r"; + StringBuilderTools.appendJSON(sb, s); + assertEquals("\"\\r\"", sb.toString()); + + sb = new StringBuilder(); + s = "\t"; + StringBuilderTools.appendJSON(sb, s); + assertEquals("\"\\t\"", sb.toString()); + + sb = new StringBuilder(); + s = "\u0012"; + StringBuilderTools.appendJSON(sb, s); + assertEquals("\"\\u0012\"", sb.toString()); + + sb = new StringBuilder(); + s = "\u0002"; + StringBuilderTools.appendJSON(sb, s); + assertEquals("\"\\u0002\"", sb.toString()); + } + + public void testAppendJSONString_empty() throws Exception { + StringBuilder sb = new StringBuilder(); + String s = ""; + StringBuilderTools.appendJSON(sb, s); + assertEquals("\"\"", sb.toString()); + } + + public void testAppendJSONString_null() throws Exception { + StringBuilder sb = new StringBuilder(); + String s = null; + StringBuilderTools.appendJSON(sb, s); + assertEquals("null", sb.toString()); + } + + public void testAppendJSONContentString() throws Exception { + StringBuilder sb = new StringBuilder(); + String s = "foo"; + StringBuilderTools.appendJSONContent(sb, s); + assertEquals("foo", sb.toString()); + } + + public void testAppendJSONContentString_empty() throws Exception { + StringBuilder sb = new StringBuilder(); + String s = ""; + StringBuilderTools.appendJSONContent(sb, s); + assertEquals("", sb.toString()); + } + + public void testAppendJSONContentString_null() throws Exception { + StringBuilder sb = new StringBuilder(); + String s = null; + StringBuilderTools.appendJSONContent(sb, s); + assertEquals("null", sb.toString()); + } + + public void testAppendJSONCharArray() throws Exception { + StringBuilder sb = new StringBuilder(); + String s = "foo"; + StringBuilderTools.appendJSON(sb, s.toCharArray()); + assertEquals("\"foo\"", sb.toString()); + sb = new StringBuilder(); + StringBuilderTools.appendJSON(sb, (Object) s.toCharArray()); + assertEquals("\"foo\"", sb.toString()); + } + + public void testAppendJSONCharArray_empty() throws Exception { + StringBuilder sb = new StringBuilder(); + String s = ""; + StringBuilderTools.appendJSON(sb, s.toCharArray()); + assertEquals("\"\"", sb.toString()); + } + + public void testAppendJSONCharArray_null() throws Exception { + StringBuilder sb = new StringBuilder(); + char[] s = null; + StringBuilderTools.appendJSON(sb, s); + assertEquals("null", sb.toString()); + } + + public void testAppendJSONContentCharArray() throws Exception { + StringBuilder sb = new StringBuilder(); + String s = "foo"; + StringBuilderTools.appendJSONContent(sb, s.toCharArray()); + assertEquals("foo", sb.toString()); + } + + public void testAppendJSONContentCharArray_empty() throws Exception { + StringBuilder sb = new StringBuilder(); + String s = ""; + StringBuilderTools.appendJSONContent(sb, s.toCharArray()); + assertEquals("", sb.toString()); + } + + public void testAppendJSONContentCharArray_null() throws Exception { + StringBuilder sb = new StringBuilder(); + char[] s = null; + StringBuilderTools.appendJSONContent(sb, s); + assertEquals("null", sb.toString()); + } + + public void testAppendJSONIterable() throws Exception { + StringBuilder sb = new StringBuilder(); + Object[] array = new Object[] { + new SimplePair<>("name", "Fred"), + new SimplePair<>("age", Integer.valueOf(42)), + new SimplePair<>("sex", "male"), + null + }; + Iterable<?> iterable = IterableTools.iterable(array); + StringBuilderTools.appendJSON(sb, iterable); + String expected = "[{\"left\":\"name\",\"right\":\"Fred\"},{\"left\":\"age\",\"right\":42},{\"left\":\"sex\",\"right\":\"male\"},null]"; + assertEquals(expected, sb.toString()); + sb = new StringBuilder(); + StringBuilderTools.appendJSON(sb, (Object) iterable); + assertEquals(expected, sb.toString()); + } + + public void testAppendJSONIterable_empty() throws Exception { + StringBuilder sb = new StringBuilder(); + Object[] array = new Object[0]; + Iterable<?> iterable = IterableTools.iterable(array); + StringBuilderTools.appendJSON(sb, iterable); + assertEquals("[]", sb.toString()); + } + + public void testAppendJSONIterable_null() throws Exception { + StringBuilder sb = new StringBuilder(); + Iterable<?> iterable = null; + StringBuilderTools.appendJSON(sb, iterable); + assertEquals("null", sb.toString()); + } + + public void testAppendJSONObjectArray() throws Exception { + StringBuilder sb = new StringBuilder(); + Object[] array = new Object[] { + new SimplePair<>("name", "Fred"), + new SimplePair<>("age", Integer.valueOf(42)), + new SimplePair<>("sex", "male"), + null + }; + StringBuilderTools.appendJSON(sb, array); + String expected = "[{\"left\":\"name\",\"right\":\"Fred\"},{\"left\":\"age\",\"right\":42},{\"left\":\"sex\",\"right\":\"male\"},null]"; + assertEquals(expected, sb.toString()); + sb = new StringBuilder(); + StringBuilderTools.appendJSON(sb, (Object) array); + assertEquals(expected, sb.toString()); + } + + public void testAppendJSONObjectArray_empty() throws Exception { + StringBuilder sb = new StringBuilder(); + Object[] array = new Object[0]; + StringBuilderTools.appendJSON(sb, array); + assertEquals("[]", sb.toString()); + } + + public void testAppendJSONObjectArray_null() throws Exception { + StringBuilder sb = new StringBuilder(); + Object[] array = null; + StringBuilderTools.appendJSON(sb, array); + assertEquals("null", sb.toString()); + } + + public void testAppendJSONObject() throws Exception { + StringBuilder sb = new StringBuilder(); + StringBuilderTools.appendJSON(sb, new SimplePair<>("name", "Fred")); + assertEquals("{\"left\":\"name\",\"right\":\"Fred\"}", sb.toString()); + } + + public void testAppendJSONObject_empty() throws Exception { + StringBuilder sb = new StringBuilder(); + StringBuilderTools.appendJSON(sb, new Object()); + assertEquals("{}", sb.toString()); + } + + public void testAppendJSONBooleanArray() throws Exception { + StringBuilder sb = new StringBuilder(); + Boolean[] array = new Boolean[] { Boolean.valueOf(true), Boolean.valueOf(false), Boolean.valueOf(true), null }; + StringBuilderTools.appendJSON(sb, array); + assertEquals("[true,false,true,null]", sb.toString()); + sb = new StringBuilder(); + StringBuilderTools.appendJSON(sb, (Object[]) array); + assertEquals("[true,false,true,null]", sb.toString()); + } + + public void testAppendJSONBooleanArray_empty() throws Exception { + StringBuilder sb = new StringBuilder(); + Boolean[] array = new Boolean[0]; + StringBuilderTools.appendJSON(sb, array); + assertEquals("[]", sb.toString()); + sb = new StringBuilder(); + StringBuilderTools.appendJSON(sb, (Object[]) array); + assertEquals("[]", sb.toString()); + } + + public void testAppendJSONBooleanArray_null() throws Exception { + StringBuilder sb = new StringBuilder(); + Boolean[] array = null; + StringBuilderTools.appendJSON(sb, array); + assertEquals("null", sb.toString()); + } + + public void testAppendJSONBoolean() throws Exception { + StringBuilder sb = new StringBuilder(); + StringBuilderTools.appendJSON(sb, Boolean.valueOf(true)); + assertEquals("true", sb.toString()); + sb = new StringBuilder(); + StringBuilderTools.appendJSON(sb, Boolean.valueOf(false)); + assertEquals("false", sb.toString()); + sb = new StringBuilder(); + StringBuilderTools.appendJSON(sb, (Object) Boolean.valueOf(true)); + assertEquals("true", sb.toString()); + sb = new StringBuilder(); + StringBuilderTools.appendJSON(sb, (Object) Boolean.valueOf(false)); + assertEquals("false", sb.toString()); + } + + public void testAppendJSONBooleanPrimitiveArray() throws Exception { + StringBuilder sb = new StringBuilder(); + boolean[] array = new boolean[] { true, false, true }; + StringBuilderTools.appendJSON(sb, array); + assertEquals("[true,false,true]", sb.toString()); + sb = new StringBuilder(); + StringBuilderTools.appendJSON(sb, (Object) array); + assertEquals("[true,false,true]", sb.toString()); + } + + public void testAppendJSONBooleanPrimitiveArray_empty() throws Exception { + StringBuilder sb = new StringBuilder(); + boolean[] array = new boolean[0]; + StringBuilderTools.appendJSON(sb, array); + assertEquals("[]", sb.toString()); + } + + public void testAppendJSONBooleanPrimitiveArray_null() throws Exception { + StringBuilder sb = new StringBuilder(); + boolean[] array = null; + StringBuilderTools.appendJSON(sb, array); + assertEquals("null", sb.toString()); + } + + public void testAppendJSONBooleanPrimitive() throws Exception { + StringBuilder sb = new StringBuilder(); + StringBuilderTools.appendJSON(sb, true); + assertEquals("true", sb.toString()); + sb = new StringBuilder(); + StringBuilderTools.appendJSON(sb, false); + assertEquals("false", sb.toString()); + } + + public void testAppendJSONNumber() throws Exception { + Number n; + StringBuilder sb; + + sb = new StringBuilder(); + n = Integer.valueOf(7); + StringBuilderTools.appendJSON(sb, n); + assertEquals("7", sb.toString()); + + sb = new StringBuilder(); + n = Double.valueOf(7.7); + StringBuilderTools.appendJSON(sb, n); + assertEquals("7.7", sb.toString()); + + sb = new StringBuilder(); + n = Byte.valueOf((byte) 7); + StringBuilderTools.appendJSON(sb, n); + assertEquals("7", sb.toString()); + + sb = new StringBuilder(); + n = Float.valueOf(7.7f); + StringBuilderTools.appendJSON(sb, n); + assertEquals("7.7", sb.toString()); + + sb = new StringBuilder(); + n = Long.valueOf(123456789123456789L); + StringBuilderTools.appendJSON(sb, n); + assertEquals("123456789123456789", sb.toString()); + + sb = new StringBuilder(); + n = Short.valueOf((short) 7); + StringBuilderTools.appendJSON(sb, n); + assertEquals("7", sb.toString()); + + sb = new StringBuilder(); + n = BigDecimal.valueOf(123456789123456789L, 5); + StringBuilderTools.appendJSON(sb, n); + assertEquals("1234567891234.56789", sb.toString()); + + sb = new StringBuilder(); + n = BigInteger.valueOf(123456789123456789L); + StringBuilderTools.appendJSON(sb, n); + assertEquals("123456789123456789", sb.toString()); + + sb = new StringBuilder(); + n = new AtomicInteger(7); + StringBuilderTools.appendJSON(sb, n); + assertEquals("7", sb.toString()); + + sb = new StringBuilder(); + n = null; + StringBuilderTools.appendJSON(sb, n); + assertEquals("null", sb.toString()); + } + + public void testAppendJSONIntegerArray() throws Exception { + StringBuilder sb = new StringBuilder(); + Integer i0 = Integer.valueOf(7); + Integer i1 = Integer.valueOf(-3); + Integer i2 = Integer.valueOf(0); + Integer[] array = new Integer[] { i0, i1, i2, null }; + StringBuilderTools.appendJSON(sb, array); + assertEquals("[7,-3,0,null]", sb.toString()); + sb = new StringBuilder(); + StringBuilderTools.appendJSON(sb, (Number[]) array); + assertEquals("[7,-3,0,null]", sb.toString()); + } + + public void testAppendJSONIntegerArray_empty() throws Exception { + StringBuilder sb = new StringBuilder(); + Integer[] array = new Integer[0]; + StringBuilderTools.appendJSON(sb, array); + assertEquals("[]", sb.toString()); + } + + public void testAppendJSONIntegerArray_null() throws Exception { + StringBuilder sb = new StringBuilder(); + Integer[] array = null; + StringBuilderTools.appendJSON(sb, array); + assertEquals("null", sb.toString()); + } + + public void testAppendJSONInteger() throws Exception { + StringBuilder sb = new StringBuilder(); + Integer i = Integer.valueOf(7); + StringBuilderTools.appendJSON(sb, i); + assertEquals("7", sb.toString()); + } + + public void testAppendJSONIntegerPrimitiveArray() throws Exception { + StringBuilder sb = new StringBuilder(); + int i0 = 7; + int i1 = -3; + int i2 = 0; + int[] array = new int[] { i0, i1, i2 }; + StringBuilderTools.appendJSON(sb, array); + assertEquals("[7,-3,0]", sb.toString()); + sb = new StringBuilder(); + StringBuilderTools.appendJSON(sb, (Object) array); + assertEquals("[7,-3,0]", sb.toString()); + } + + public void testAppendJSONIntegerPrimitiveArray_empty() throws Exception { + StringBuilder sb = new StringBuilder(); + int[] array = new int[0]; + StringBuilderTools.appendJSON(sb, array); + assertEquals("[]", sb.toString()); + } + + public void testAppendJSONIntegerPrimitiveArray_null() throws Exception { + StringBuilder sb = new StringBuilder(); + int[] array = null; + StringBuilderTools.appendJSON(sb, array); + assertEquals("null", sb.toString()); + } + + public void testAppendJSONIntegerPrimitive() throws Exception { + StringBuilder sb = new StringBuilder(); + int i = 7; + StringBuilderTools.appendJSON(sb, i); + assertEquals("7", sb.toString()); + } + + public void testAppendJSONDoubleArray() throws Exception { + StringBuilder sb = new StringBuilder(); + Double d0 = Double.valueOf(7.7); + Double d1 = Double.valueOf(-3.2); + Double d2 = Double.valueOf(0.0); + Double[] array = new Double[] { d0, d1, d2, null }; + StringBuilderTools.appendJSON(sb, array); + assertEquals("[7.7,-3.2,0.0,null]", sb.toString()); + sb = new StringBuilder(); + StringBuilderTools.appendJSON(sb, (Number[]) array); + assertEquals("[7.7,-3.2,0.0,null]", sb.toString()); + } + + public void testAppendJSONDoubleArray_empty() throws Exception { + StringBuilder sb = new StringBuilder(); + Double[] array = new Double[0]; + StringBuilderTools.appendJSON(sb, array); + assertEquals("[]", sb.toString()); + } + + public void testAppendJSONDoubleArray_null() throws Exception { + StringBuilder sb = new StringBuilder(); + Double[] array = null; + StringBuilderTools.appendJSON(sb, array); + assertEquals("null", sb.toString()); + } + + public void testAppendJSONDouble() throws Exception { + StringBuilder sb = new StringBuilder(); + Double d = Double.valueOf(7.0); + StringBuilderTools.appendJSON(sb, d); + assertEquals("7.0", sb.toString()); + } + + public void testAppendJSONDoublePrimitiveArray() throws Exception { + StringBuilder sb = new StringBuilder(); + double d0 = 7.7; + double d1 = -3.2; + double d2 = 0.0; + double[] array = new double[] { d0, d1, d2 }; + StringBuilderTools.appendJSON(sb, array); + assertEquals("[7.7,-3.2,0.0]", sb.toString()); + sb = new StringBuilder(); + StringBuilderTools.appendJSON(sb, (Object) array); + assertEquals("[7.7,-3.2,0.0]", sb.toString()); + } + + public void testAppendJSONDoublePrimitiveArray_empty() throws Exception { + StringBuilder sb = new StringBuilder(); + double[] array = new double[0]; + StringBuilderTools.appendJSON(sb, array); + assertEquals("[]", sb.toString()); + } + + public void testAppendJSONDoublePrimitiveArray_null() throws Exception { + StringBuilder sb = new StringBuilder(); + double[] array = null; + StringBuilderTools.appendJSON(sb, array); + assertEquals("null", sb.toString()); + } + + public void testAppendJSONDoublePrimitive() throws Exception { + StringBuilder sb = new StringBuilder(); + double d = 7.0; + StringBuilderTools.appendJSON(sb, d); + assertEquals("7.0", sb.toString()); + } + + public void testAppendJSONByteArray() throws Exception { + StringBuilder sb = new StringBuilder(); + Byte b0 = Byte.valueOf((byte) 7); + Byte b1 = Byte.valueOf((byte) -3); + Byte b2 = Byte.valueOf((byte) 0); + Byte[] array = new Byte[] { b0, b1, b2, null }; + StringBuilderTools.appendJSON(sb, array); + assertEquals("[7,-3,0,null]", sb.toString()); + sb = new StringBuilder(); + StringBuilderTools.appendJSON(sb, (Number[]) array); + assertEquals("[7,-3,0,null]", sb.toString()); + } + + public void testAppendJSONByteArray_empty() throws Exception { + StringBuilder sb = new StringBuilder(); + Byte[] array = new Byte[0]; + StringBuilderTools.appendJSON(sb, array); + assertEquals("[]", sb.toString()); + } + + public void testAppendJSONByteArray_null() throws Exception { + StringBuilder sb = new StringBuilder(); + Byte[] array = null; + StringBuilderTools.appendJSON(sb, array); + assertEquals("null", sb.toString()); + } + + public void testAppendJSONByte() throws Exception { + StringBuilder sb = new StringBuilder(); + Byte b = Byte.valueOf((byte) 7); + StringBuilderTools.appendJSON(sb, b); + assertEquals("7", sb.toString()); + } + + public void testAppendJSONBytePrimitiveArray() throws Exception { + StringBuilder sb = new StringBuilder(); + byte b0 = 7; + byte b1 = -3; + byte b2 = 0; + byte[] array = new byte[] { b0, b1, b2 }; + StringBuilderTools.appendJSON(sb, array); + assertEquals("[7,-3,0]", sb.toString()); + sb = new StringBuilder(); + StringBuilderTools.appendJSON(sb, (Object) array); + assertEquals("[7,-3,0]", sb.toString()); + } + + public void testAppendJSONBytePrimitiveArray_empty() throws Exception { + StringBuilder sb = new StringBuilder(); + byte[] array = new byte[0]; + StringBuilderTools.appendJSON(sb, array); + assertEquals("[]", sb.toString()); + } + + public void testAppendJSONBytePrimitiveArray_null() throws Exception { + StringBuilder sb = new StringBuilder(); + byte[] array = null; + StringBuilderTools.appendJSON(sb, array); + assertEquals("null", sb.toString()); + } + + public void testAppendJSONBytePrimitive() throws Exception { + StringBuilder sb = new StringBuilder(); + byte b = 7; + StringBuilderTools.appendJSON(sb, b); + assertEquals("7", sb.toString()); + } + + public void testAppendJSONFloatArray() throws Exception { + StringBuilder sb = new StringBuilder(); + Float f0 = Float.valueOf(7.7f); + Float f1 = Float.valueOf(-3.2f); + Float f2 = Float.valueOf(0.0f); + Float[] array = new Float[] { f0, f1, f2, null }; + StringBuilderTools.appendJSON(sb, array); + assertEquals("[7.7,-3.2,0.0,null]", sb.toString()); + sb = new StringBuilder(); + StringBuilderTools.appendJSON(sb, (Number[]) array); + assertEquals("[7.7,-3.2,0.0,null]", sb.toString()); + } + + public void testAppendJSONFloatArray_empty() throws Exception { + StringBuilder sb = new StringBuilder(); + Float[] array = new Float[0]; + StringBuilderTools.appendJSON(sb, array); + assertEquals("[]", sb.toString()); + } + + public void testAppendJSONFloatArray_null() throws Exception { + StringBuilder sb = new StringBuilder(); + Float[] array = null; + StringBuilderTools.appendJSON(sb, array); + assertEquals("null", sb.toString()); + } + + public void testAppendJSONFloat() throws Exception { + StringBuilder sb = new StringBuilder(); + Float f = Float.valueOf(7.0f); + StringBuilderTools.appendJSON(sb, f); + assertEquals("7.0", sb.toString()); + } + + public void testAppendJSONFloatPrimitiveArray() throws Exception { + StringBuilder sb = new StringBuilder(); + float f0 = 7.7f; + float f1 = -3.2f; + float f2 = 0.0f; + float[] array = new float[] { f0, f1, f2 }; + StringBuilderTools.appendJSON(sb, array); + assertEquals("[7.7,-3.2,0.0]", sb.toString()); + sb = new StringBuilder(); + StringBuilderTools.appendJSON(sb, (Object) array); + assertEquals("[7.7,-3.2,0.0]", sb.toString()); + } + + public void testAppendJSONFloatPrimitiveArray_empty() throws Exception { + StringBuilder sb = new StringBuilder(); + float[] array = new float[0]; + StringBuilderTools.appendJSON(sb, array); + assertEquals("[]", sb.toString()); + } + + public void testAppendJSONFloatPrimitiveArray_null() throws Exception { + StringBuilder sb = new StringBuilder(); + float[] array = null; + StringBuilderTools.appendJSON(sb, array); + assertEquals("null", sb.toString()); + } + + public void testAppendJSONFloatPrimitive() throws Exception { + StringBuilder sb = new StringBuilder(); + float f = 7.0f; + StringBuilderTools.appendJSON(sb, f); + assertEquals("7.0", sb.toString()); + } + + public void testAppendJSONLongArray() throws Exception { + StringBuilder sb = new StringBuilder(); + Long l0 = Long.valueOf(123456789123456789L); + Long l1 = Long.valueOf(-123456789123456789L); + Long l2 = Long.valueOf(0); + Long[] array = new Long[] { l0, l1, l2, null }; + StringBuilderTools.appendJSON(sb, array); + assertEquals("[123456789123456789,-123456789123456789,0,null]", sb.toString()); + sb = new StringBuilder(); + StringBuilderTools.appendJSON(sb, (Number[]) array); + assertEquals("[123456789123456789,-123456789123456789,0,null]", sb.toString()); + } + + public void testAppendJSONLongArray_empty() throws Exception { + StringBuilder sb = new StringBuilder(); + Long[] array = new Long[0]; + StringBuilderTools.appendJSON(sb, array); + assertEquals("[]", sb.toString()); + } + + public void testAppendJSONLongArray_null() throws Exception { + StringBuilder sb = new StringBuilder(); + Long[] array = null; + StringBuilderTools.appendJSON(sb, array); + assertEquals("null", sb.toString()); + } + + public void testAppendJSONLong() throws Exception { + StringBuilder sb = new StringBuilder(); + Long l = Long.valueOf(123456789123456789L); + StringBuilderTools.appendJSON(sb, l); + assertEquals("123456789123456789", sb.toString()); + } + + public void testAppendJSONLongPrimitiveArray() throws Exception { + StringBuilder sb = new StringBuilder(); + long l0 = 123456789123456789L; + long l1 = -123456789123456789L; + long l2 = 0; + long[] array = new long[] { l0, l1, l2 }; + StringBuilderTools.appendJSON(sb, array); + assertEquals("[123456789123456789,-123456789123456789,0]", sb.toString()); + sb = new StringBuilder(); + StringBuilderTools.appendJSON(sb, (Object) array); + assertEquals("[123456789123456789,-123456789123456789,0]", sb.toString()); + } + + public void testAppendJSONLongPrimitiveArray_empty() throws Exception { + StringBuilder sb = new StringBuilder(); + long[] array = new long[0]; + StringBuilderTools.appendJSON(sb, array); + assertEquals("[]", sb.toString()); + } + + public void testAppendJSONLongPrimitiveArray_null() throws Exception { + StringBuilder sb = new StringBuilder(); + long[] array = null; + StringBuilderTools.appendJSON(sb, array); + assertEquals("null", sb.toString()); + } + + public void testAppendJSONLongPrimitive() throws Exception { + StringBuilder sb = new StringBuilder(); + long l = 123456789123456789L; + StringBuilderTools.appendJSON(sb, l); + assertEquals("123456789123456789", sb.toString()); + } + + public void testAppendJSONShortArray() throws Exception { + StringBuilder sb = new StringBuilder(); + Short s0 = Short.valueOf((short) 5); + Short s1 = Short.valueOf((short) -3); + Short s2 = Short.valueOf((short) 0); + Short[] array = new Short[] { s0, s1, s2, null }; + StringBuilderTools.appendJSON(sb, array); + assertEquals("[5,-3,0,null]", sb.toString()); + sb = new StringBuilder(); + StringBuilderTools.appendJSON(sb, (Number[]) array); + assertEquals("[5,-3,0,null]", sb.toString()); + } + + public void testAppendJSONShortArray_empty() throws Exception { + StringBuilder sb = new StringBuilder(); + Short[] array = new Short[0]; + StringBuilderTools.appendJSON(sb, array); + assertEquals("[]", sb.toString()); + } + + public void testAppendJSONShortArray_null() throws Exception { + StringBuilder sb = new StringBuilder(); + Short[] array = null; + StringBuilderTools.appendJSON(sb, array); + assertEquals("null", sb.toString()); + } + + public void testAppendJSONShort() throws Exception { + StringBuilder sb = new StringBuilder(); + Short s = Short.valueOf((short) 5); + StringBuilderTools.appendJSON(sb, s); + assertEquals("5", sb.toString()); + } + + public void testAppendJSONShortPrimitiveArray() throws Exception { + StringBuilder sb = new StringBuilder(); + short s0 = 5; + short s1 = -3; + short s2 = 0; + short[] array = new short[] { s0, s1, s2 }; + StringBuilderTools.appendJSON(sb, array); + assertEquals("[5,-3,0]", sb.toString()); + sb = new StringBuilder(); + StringBuilderTools.appendJSON(sb, (Object) array); + assertEquals("[5,-3,0]", sb.toString()); + } + + public void testAppendJSONShortPrimitiveArray_empty() throws Exception { + StringBuilder sb = new StringBuilder(); + short[] array = new short[0]; + StringBuilderTools.appendJSON(sb, array); + assertEquals("[]", sb.toString()); + } + + public void testAppendJSONShortPrimitiveArray_null() throws Exception { + StringBuilder sb = new StringBuilder(); + short[] array = null; + StringBuilderTools.appendJSON(sb, array); + assertEquals("null", sb.toString()); + } + + public void testAppendJSONShortPrimitive() throws Exception { + StringBuilder sb = new StringBuilder(); + short s = 5; + StringBuilderTools.appendJSON(sb, s); + assertEquals("5", sb.toString()); + } + + public void testAppendJSONBigDecimalArray() throws Exception { + StringBuilder sb = new StringBuilder(); + BigDecimal bd0 = BigDecimal.valueOf(123456789123456789L, 5); + BigDecimal bd1 = BigDecimal.valueOf(987654321987654321L, 9); + BigDecimal bd2 = BigDecimal.valueOf(987654321987654321L, -9); + BigDecimal[] array = new BigDecimal[] { bd0, bd1, bd2, null }; + StringBuilderTools.appendJSON(sb, array); + assertEquals("[1234567891234.56789,987654321.987654321,9.87654321987654321E+26,null]", sb.toString()); + sb = new StringBuilder(); + StringBuilderTools.appendJSON(sb, (Number[]) array); + assertEquals("[1234567891234.56789,987654321.987654321,9.87654321987654321E+26,null]", sb.toString()); + } + + public void testAppendJSONBigDecimalArray_empty() throws Exception { + StringBuilder sb = new StringBuilder(); + BigDecimal[] array = new BigDecimal[0]; + StringBuilderTools.appendJSON(sb, array); + assertEquals("[]", sb.toString()); + } + + public void testAppendJSONBigDecimalArray_null() throws Exception { + StringBuilder sb = new StringBuilder(); + BigDecimal[] array = null; + StringBuilderTools.appendJSON(sb, array); + assertEquals("null", sb.toString()); + } + + public void testAppendJSONBigDecimal() throws Exception { + StringBuilder sb = new StringBuilder(); + BigDecimal bd = BigDecimal.valueOf(123456789123456789L, 5); + StringBuilderTools.appendJSON(sb, bd); + assertEquals("1234567891234.56789", sb.toString()); + } + + public void testAppendJSONBigIntegerArray() throws Exception { + StringBuilder sb = new StringBuilder(); + BigInteger bi0 = BigInteger.valueOf(123456789123456789L); + BigInteger bi1 = BigInteger.valueOf(987654321987654321L); + BigInteger[] array = new BigInteger[] { bi0, bi1, null }; + StringBuilderTools.appendJSON(sb, array); + assertEquals("[123456789123456789,987654321987654321,null]", sb.toString()); + sb = new StringBuilder(); + StringBuilderTools.appendJSON(sb, (Number[]) array); + assertEquals("[123456789123456789,987654321987654321,null]", sb.toString()); + } + + public void testAppendJSONBigIntegerArray_empty() throws Exception { + StringBuilder sb = new StringBuilder(); + BigInteger[] array = new BigInteger[0]; + StringBuilderTools.appendJSON(sb, array); + assertEquals("[]", sb.toString()); + } + + public void testAppendJSONBigIntegerArray_null() throws Exception { + StringBuilder sb = new StringBuilder(); + BigInteger[] array = null; + StringBuilderTools.appendJSON(sb, array); + assertEquals("null", sb.toString()); + } + + public void testAppendJSONBigInteger() throws Exception { + StringBuilder sb = new StringBuilder(); + BigInteger bi = BigInteger.valueOf(123456789123456789L); + StringBuilderTools.appendJSON(sb, bi); + assertEquals("123456789123456789", sb.toString()); + } + + public void testAppendJSONFraction() throws Exception { + StringBuilder sb = new StringBuilder(); + org.apache.commons.lang.math.Fraction f = org.apache.commons.lang.math.Fraction.getFraction(2, 7); + StringBuilderTools.appendJSON(sb, f); + assertEquals("{\"denominator\":7,\"hashCode\":0,\"numerator\":2,\"toProperString\":null,\"toString\":\"2/7\"}", sb.toString()); + } + + public void testAppendJSONNumberArray() throws Exception { + StringBuilder sb = new StringBuilder(); + Number n0 = new AtomicInteger(7); + Number n1 = new AtomicInteger(-3); + Number n2 = new AtomicInteger(0); + Number[] array = new Number[] { n0, n1, n2, null }; + StringBuilderTools.appendJSON(sb, array); + assertEquals("[7,-3,0,null]", sb.toString()); + sb = new StringBuilder(); + StringBuilderTools.appendJSON(sb, (Object[]) array); + assertEquals("[7,-3,0,null]", sb.toString()); + } + + public void testAppendJSONNumberArray_mixed() throws Exception { + StringBuilder sb = new StringBuilder(); + Number n0 = new AtomicInteger(7); + Number n1 = Integer.valueOf(-3); + Number n2 = Double.valueOf(0.33); + Number[] array = new Number[] { n0, n1, n2, null }; + StringBuilderTools.appendJSON(sb, array); + assertEquals("[7,-3,0.33,null]", sb.toString()); + sb = new StringBuilder(); + StringBuilderTools.appendJSON(sb, (Object[]) array); + assertEquals("[7,-3,0.33,null]", sb.toString()); + } + + public void testAppendJSONNumberArray_empty() throws Exception { + StringBuilder sb = new StringBuilder(); + Number[] array = new Number[0]; + StringBuilderTools.appendJSON(sb, array); + assertEquals("[]", sb.toString()); + sb = new StringBuilder(); + StringBuilderTools.appendJSON(sb, (Object[]) array); + assertEquals("[]", sb.toString()); + } + + public void testAppendJSONNumberArray_null() throws Exception { + StringBuilder sb = new StringBuilder(); + Number[] array = null; + StringBuilderTools.appendJSON(sb, array); + assertEquals("null", sb.toString()); + } + + + // ********** array/iterable ********** + public void testAppendObjectArray_null() throws Exception { StringBuilder sb = new StringBuilder(); Object[] array = null; @@ -477,6 +1403,9 @@ public class StringBuilderToolsTests assertEquals("[foo, bar, baz]", sb.toString()); } + + // ********** toString() stuff ********** + public void testAppendHashCodeToString() throws Exception { StringBuilder sb = new StringBuilder(); Object object = new Object(); |