NumberConverter { public static void serialize(final double value, final JsonWriter sw) { sw.writeDouble(value); }  static void serializeNullable(@Nullable final Double value, final JsonWriter sw); static void serialize(final double value, final JsonWriter sw); static void serialize(@Nullable final double[] value, final JsonWriter sw); static double deserializeDouble(final JsonReader reader); @SuppressWarnings("unchecked") static ArrayList<Double> deserializeDoubleCollection(final JsonReader reader); static void deserializeDoubleCollection(final JsonReader reader, final Collection<Double> res); @SuppressWarnings("unchecked") static ArrayList<Double> deserializeDoubleNullableCollection(final JsonReader reader); static void deserializeDoubleNullableCollection(final JsonReader reader, final Collection<Double> res); static void serializeNullable(@Nullable final Float value, final JsonWriter sw); static void serialize(final float value, final JsonWriter sw); static void serialize(@Nullable final float[] value, final JsonWriter sw); static float deserializeFloat(final JsonReader reader); @SuppressWarnings("unchecked") static ArrayList<Float> deserializeFloatCollection(final JsonReader reader); static void deserializeFloatCollection(final JsonReader reader, Collection<Float> res); @SuppressWarnings("unchecked") static ArrayList<Float> deserializeFloatNullableCollection(final JsonReader reader); static void deserializeFloatNullableCollection(final JsonReader reader, final Collection<Float> res); static void serializeNullable(@Nullable final Integer value, final JsonWriter sw); static void serialize(final int value, final JsonWriter sw); static void serialize(@Nullable final int[] values, final JsonWriter sw); static void serialize(@Nullable final short[] value, final JsonWriter sw); static short deserializeShort(final JsonReader reader); static int deserializeInt(final JsonReader reader); @SuppressWarnings("unchecked") static ArrayList<Integer> deserializeIntCollection(final JsonReader reader); static int[] deserializeIntArray(final JsonReader reader); static short[] deserializeShortArray(final JsonReader reader); static long[] deserializeLongArray(final JsonReader reader); static float[] deserializeFloatArray(final JsonReader reader); static double[] deserializeDoubleArray(final JsonReader reader); static void deserializeShortCollection(final JsonReader reader, final Collection<Short> res); @SuppressWarnings("unchecked") static ArrayList<Short> deserializeShortNullableCollection(final JsonReader reader); static void deserializeShortNullableCollection(final JsonReader reader, final Collection<Short> res); static void deserializeIntCollection(final JsonReader reader, final Collection<Integer> res); @SuppressWarnings("unchecked") static ArrayList<Integer> deserializeIntNullableCollection(final JsonReader reader); static void deserializeIntNullableCollection(final JsonReader reader, final Collection<Integer> res); static void serializeNullable(@Nullable final Long value, final JsonWriter sw); static void serialize(final long value, final JsonWriter sw); static void serialize(@Nullable final long[] values, final JsonWriter sw); static long deserializeLong(final JsonReader reader); @SuppressWarnings("unchecked") static ArrayList<Long> deserializeLongCollection(final JsonReader reader); static void deserializeLongCollection(final JsonReader reader, final Collection<Long> res); @SuppressWarnings("unchecked") static ArrayList<Long> deserializeLongNullableCollection(final JsonReader reader); static void deserializeLongNullableCollection(final JsonReader reader, final Collection<Long> res); static void serializeNullable(@Nullable final BigDecimal value, final JsonWriter sw); static void serialize(final BigDecimal value, final JsonWriter sw); static BigDecimal deserializeDecimal(final JsonReader reader); static Number deserializeNumber(final JsonReader reader); @SuppressWarnings("unchecked") static ArrayList<BigDecimal> deserializeDecimalCollection(final JsonReader reader); static void deserializeDecimalCollection(final JsonReader reader, final Collection<BigDecimal> res); @SuppressWarnings("unchecked") static ArrayList<BigDecimal> deserializeDecimalNullableCollection(final JsonReader reader); static void deserializeDecimalNullableCollection(final JsonReader reader, final Collection<BigDecimal> res); final static short[] SHORT_EMPTY_ARRAY; final static int[] INT_EMPTY_ARRAY; final static long[] LONG_EMPTY_ARRAY; final static float[] FLOAT_EMPTY_ARRAY; final static double[] DOUBLE_EMPTY_ARRAY; final static Short SHORT_ZERO; final static Integer INT_ZERO; final static Long LONG_ZERO; final static Float FLOAT_ZERO; final static Double DOUBLE_ZERO; static final JsonReader.ReadObject<Double> DOUBLE_READER; static final JsonReader.ReadObject<Double> NULLABLE_DOUBLE_READER; static final JsonWriter.WriteObject<Double> DOUBLE_WRITER; static final JsonReader.ReadObject<double[]> DOUBLE_ARRAY_READER; static final JsonWriter.WriteObject<double[]> DOUBLE_ARRAY_WRITER; static final JsonReader.ReadObject<Float> FLOAT_READER; static final JsonReader.ReadObject<Float> NULLABLE_FLOAT_READER; static final JsonWriter.WriteObject<Float> FLOAT_WRITER; static final JsonReader.ReadObject<float[]> FLOAT_ARRAY_READER; static final JsonWriter.WriteObject<float[]> FLOAT_ARRAY_WRITER; static final JsonReader.ReadObject<Integer> INT_READER; static final JsonReader.ReadObject<Integer> NULLABLE_INT_READER; static final JsonWriter.WriteObject<Integer> INT_WRITER; static final JsonReader.ReadObject<int[]> INT_ARRAY_READER; static final JsonWriter.WriteObject<int[]> INT_ARRAY_WRITER; static final JsonReader.ReadObject<Short> SHORT_READER; static final JsonReader.ReadObject<Short> NULLABLE_SHORT_READER; static final JsonWriter.WriteObject<Short> SHORT_WRITER; static final JsonReader.ReadObject<short[]> SHORT_ARRAY_READER; static final JsonWriter.WriteObject<short[]> SHORT_ARRAY_WRITER; static final JsonReader.ReadObject<Long> LONG_READER; static final JsonReader.ReadObject<Long> NULLABLE_LONG_READER; static final JsonWriter.WriteObject<Long> LONG_WRITER; static final JsonReader.ReadObject<long[]> LONG_ARRAY_READER; static final JsonWriter.WriteObject<long[]> LONG_ARRAY_WRITER; static final JsonReader.ReadObject<BigDecimal> DecimalReader; static final JsonWriter.WriteObject<BigDecimal> DecimalWriter; }
@Test public void testSerialization() { final JsonWriter sw = new JsonWriter(40, null); final int from = -1000000; final int to = 1000000; for (long value = from; value <= to; value++) { sw.reset(); NumberConverter.serialize(value, sw); final String valueString = sw.toString(); final int valueParsed = Integer.valueOf(valueString); Assert.assertEquals(value, valueParsed); } }
@Test public void testCollectionSerialization() { final Random rnd = new Random(1337); final List<Long> collection = new ArrayList<Long>(); for (long i = 1L; i <= 1000000000000000000L; i *= 10) { collection.add(i); collection.add(-i); for (int r = 0; r < 100; r++) { collection.add(Math.abs(rnd.nextLong()) % i); collection.add(-(Math.abs(rnd.nextLong()) % i)); } } collection.add(Long.MIN_VALUE); collection.add(Long.MAX_VALUE); final Long[] boxes = collection.toArray(new Long[0]); final long[] primitives = new long[boxes.length]; for (int i = 0; i < primitives.length; i++) { primitives[i] = boxes[i]; } final String expected; { final StringBuilder tmp = new StringBuilder("["); for (long value : primitives) { tmp.append(value).append(','); } tmp.setLength(tmp.length() - 1); tmp.append(']'); expected = tmp.toString(); } final JsonWriter sw = new JsonWriter(null); NumberConverter.serialize(primitives, sw); Assert.assertEquals(expected, sw.toString()); sw.reset(); sw.serialize(collection, NumberConverter.LONG_WRITER); Assert.assertEquals(expected, sw.toString()); sw.reset(); sw.serialize(boxes, NumberConverter.LONG_WRITER); Assert.assertEquals(expected, sw.toString()); }
@Test public void testNonRandomAccessCollectionSerialization() { final Random rnd = new Random(1337); final List<Long> collection = new LinkedList<Long>(); for (long i = 1L; i <= 1000000000000000000L; i *= 10) { collection.add(i); collection.add(-i); for (int r = 0; r < 100; r++) { collection.add(Math.abs(rnd.nextLong()) % i); collection.add(-(Math.abs(rnd.nextLong()) % i)); } } collection.add(Long.MIN_VALUE); collection.add(Long.MAX_VALUE); final String expected; { final StringBuilder tmp = new StringBuilder("["); for (long value : collection) { tmp.append(value).append(','); } tmp.setLength(tmp.length() - 1); tmp.append(']'); expected = tmp.toString(); } final JsonWriter sw = new JsonWriter(null); sw.serialize(collection, NumberConverter.LONG_WRITER); Assert.assertEquals(expected, sw.toString()); }