RecordSerializer { public static byte[] serialize(Object v, int type) { if (v == null) { return null; } switch (type) { case ColumnTypes.BYTEARRAY: case ColumnTypes.NOTNULL_BYTEARRAY: return (byte[]) v; case ColumnTypes.INTEGER: case ColumnTypes.NOTNULL_INTEGER: if (v instanceof Integer) { return Bytes.intToByteArray((Integer) v); } else if (v instanceof Number) { return Bytes.intToByteArray(((Number) v).intValue()); } else { return Bytes.intToByteArray(Integer.parseInt(v.toString())); } case ColumnTypes.LONG: case ColumnTypes.NOTNULL_LONG: if (v instanceof Long) { return Bytes.longToByteArray((Long) v); } else if (v instanceof Number) { return Bytes.longToByteArray(((Number) v).longValue()); } else { return Bytes.longToByteArray(Long.parseLong(v.toString())); } case ColumnTypes.STRING: case ColumnTypes.NOTNULL_STRING: if (v instanceof RawString) { RawString rs = (RawString) v; return rs.toByteArray(); } else { return Bytes.string_to_array(v.toString()); } case ColumnTypes.BOOLEAN: case ColumnTypes.NOTNULL_BOOLEAN: if (v instanceof Boolean) { return Bytes.booleanToByteArray((Boolean) v); } else { return Bytes.booleanToByteArray(Boolean.parseBoolean(v.toString())); } case ColumnTypes.DOUBLE: case ColumnTypes.NOTNULL_DOUBLE: if (v instanceof Double) { return Bytes.doubleToByteArray((Double) v); } else if (v instanceof Long) { return Bytes.doubleToByteArray((Long) v); } else if (v instanceof Number) { return Bytes.doubleToByteArray(((Number) v).longValue()); } else { return Bytes.doubleToByteArray(Double.parseDouble(v.toString())); } case ColumnTypes.TIMESTAMP: case ColumnTypes.NOTNULL_TIMESTAMP: if (v instanceof Long) { return Bytes.timestampToByteArray(new java.sql.Timestamp(((Long) v))); } if (!(v instanceof java.sql.Timestamp)) { throw new IllegalArgumentException("bad value type for column " + type + ": required java.sql.Timestamp, but was " + v.getClass() + ", toString of value is " + v); } return Bytes.timestampToByteArray((java.sql.Timestamp) v); default: throw new IllegalArgumentException("bad column type " + type); } } private  RecordSerializer(); static Object deserialize(Bytes data, int type); static Object deserialize(byte[] data, int type); static int deserializeCompare(byte[] data, int type, Object cvalue); static SQLRecordPredicateFunctions.CompareResult deserializeCompare(Bytes data, int type, Object cvalue); static Object deserializeTypeAndValue(ByteArrayCursor dii); static SQLRecordPredicateFunctions.CompareResult compareDeserializeTypeAndValue(ByteArrayCursor dii, Object cvalue); static void skipTypeAndValue(ByteArrayCursor dii); static byte[] serialize(Object v, int type); static void serializeTo(Object v, int type, ExtendedDataOutputStream out); static void validate(Object v, int type); static void serializeTypeAndValue(Object v, int type, ExtendedDataOutputStream oo); static void serializeValue(Object v, int type, ExtendedDataOutputStream oo); static DateTimeFormatter getUTCTimestampFormatter(); static Object convert(int type, Object value); static DataAccessor buildRawDataAccessor(Record record, Table table); static DataAccessor buildRawDataAccessorForPrimaryKey(Bytes key, Table table); static Record makeRecord(Table table, Object... values); static Bytes serializePrimaryKey(Map<String, Object> record, ColumnsList table, String[] columns); static byte[] serializePrimaryKeyRaw(Map<String, Object> record, ColumnsList table, String[] columns); static Bytes serializeIndexKey(DataAccessor record, ColumnsList index, String[] columns); static void validateIndexableValue(DataAccessor record, ColumnsList indexDefinition, String[] columns); static Object deserializePrimaryKey(Bytes key, Table table); static Map<String, Object> deserializePrimaryKeyAsMap(Bytes key, Table table); static Bytes serializeValue(Map<String, Object> record, Table table); static byte[] serializeValueRaw(Map<String, Object> record, Table table, int expectedSize); static byte[] buildRecord(
            int expectedSize, Table table,
            Function<String, Object> evaluator
    ); static Record toRecord(Map<String, Object> record, Table table); @SuppressFBWarnings("RV_RETURN_VALUE_IGNORED") static Map<String, Object> toBean(Record record, Table table);  }
@Test public void testSerializeWithNullAndNonNullTypes() { byte[] iBytes = RecordSerializer.serialize(new Integer(10), ColumnTypes.INTEGER); byte[] iBytesNonNullType = RecordSerializer.serialize(new Integer(10), ColumnTypes.NOTNULL_INTEGER); assertArrayEquals(iBytes, iBytesNonNullType); byte[] lBytes = RecordSerializer.serialize(new Long(1982), ColumnTypes.NOTNULL_LONG); byte[] lBytesNonNullType = RecordSerializer.serialize(new Long(1982), ColumnTypes.LONG); assertArrayEquals(lBytes, lBytesNonNullType); byte[] sBytes = RecordSerializer.serialize("test", ColumnTypes.STRING); byte[] sBytesNonNullType = RecordSerializer.serialize("test", ColumnTypes.NOTNULL_STRING); assertArrayEquals(sBytes, sBytesNonNullType); byte[] dBytes = RecordSerializer.serialize(10.01d, ColumnTypes.DOUBLE); byte[] dBytesNonNullType = RecordSerializer.serialize(10.01d, ColumnTypes.NOTNULL_DOUBLE); assertArrayEquals(dBytes, dBytesNonNullType); byte[] bBytes = RecordSerializer.serialize(Boolean.TRUE, ColumnTypes.BOOLEAN); byte[] bBytesNonNullType = RecordSerializer.serialize(Boolean.TRUE, ColumnTypes.NOTNULL_BOOLEAN); assertArrayEquals(bBytes, bBytesNonNullType); Timestamp timestamp = new Timestamp(System.currentTimeMillis()); byte[] tBytes = RecordSerializer.serialize(timestamp, ColumnTypes.TIMESTAMP); byte[] tBytesNonNullType = RecordSerializer.serialize(timestamp, ColumnTypes.NOTNULL_TIMESTAMP); assertArrayEquals(tBytes, tBytesNonNullType); }
@Test public void testSerializeThrowsExceptionOnNullObject() { assertNull(RecordSerializer.serialize(null, ColumnTypes.STRING)); }
@Test(expected = IllegalArgumentException.class) public void testSerializeThrowsExceptionOnUnknownType() { RecordSerializer.serialize("test", ColumnTypes.ANYTYPE); }