/** Converts a value from internal format to a type that can be serialized * as JSON. */ private static Object localToSerial(ColumnMetaData.Rep rep, Object value) { switch (rep) { case BYTE_STRING: return ((ByteString) value).toBase64String(); default: return value; } }
/** Converts a value from internal format to a type that can be serialized * as JSON. */ private static Object localToSerial(ColumnMetaData.Rep rep, Object value) { switch (rep) { case BYTE_STRING: return ((ByteString) value).toBase64String(); default: return value; } }
/** Converts a value from internal format to a type that can be serialized * as JSON. */ private static Object localToSerial(ColumnMetaData.Rep rep, Object value) { switch (rep) { case BYTE_STRING: return ((ByteString) value).toBase64String(); default: return value; } }
/** Converts a value from JDBC format to a type that can be serialized as * JSON. */ private static Object jdbcToSerial(ColumnMetaData.Rep rep, Object value, Calendar calendar) { switch (rep) { case BYTE_STRING: return new ByteString((byte[]) value).toBase64String(); case JAVA_UTIL_DATE: case JAVA_SQL_TIMESTAMP: case JAVA_SQL_DATE: case JAVA_SQL_TIME: long t = ((Date) value).getTime(); if (calendar != null) { t += calendar.getTimeZone().getOffset(t); } switch (rep) { case JAVA_SQL_DATE: return (int) DateTimeUtils.floorDiv(t, DateTimeUtils.MILLIS_PER_DAY); case JAVA_SQL_TIME: return (int) DateTimeUtils.floorMod(t, DateTimeUtils.MILLIS_PER_DAY); default: return t; } default: return value; } }
return new ByteString((byte[]) value).toBase64String(); case JAVA_UTIL_DATE: case JAVA_SQL_TIMESTAMP:
return new ByteString((byte[]) value).toBase64String(); case JAVA_UTIL_DATE: case JAVA_SQL_TIMESTAMP:
private void thereAndBack(byte[] bytes) { final ByteString byteString = new ByteString(bytes); final byte[] bytes2 = byteString.getBytes(); assertThat(bytes, equalTo(bytes2)); final String base64String = byteString.toBase64String(); final ByteString byteString1 = ByteString.ofBase64(base64String); assertThat(byteString, equalTo(byteString1)); }
private void thereAndBack(byte[] bytes) { final ByteString byteString = new ByteString(bytes); final byte[] bytes2 = byteString.getBytes(); assertThat(bytes, equalTo(bytes2)); final String base64String = byteString.toBase64String(); final ByteString byteString1 = ByteString.ofBase64(base64String); assertThat(byteString, equalTo(byteString1)); }
return (new ByteString(protoValue.getBytesValue().toByteArray())).toBase64String(); case STRING: return protoValue.getStringValue();
return (new ByteString(protoValue.getBytesValue().toByteArray())).toBase64String(); case STRING: return protoValue.getStringValue();
@Test public void testBase64() { byte[] bytes = "qwertyasdf".getBytes(UTF_8); // Plain bytes get put into protobuf for simplicitly Common.TypedValue proto = Common.TypedValue.newBuilder().setBytesValue( com.google.protobuf.ByteString.copyFrom(bytes)) .setType(Common.Rep.BYTE_STRING).build(); // But we should get back a b64-string to make sure TypedValue doesn't get confused. Object deserializedObj = TypedValue.getSerialFromProto(proto); assertThat(deserializedObj, is(instanceOf(String.class))); assertEquals(new ByteString(bytes).toBase64String(), (String) deserializedObj); // But we should get a non-b64 byte array as the JDBC representation deserializedObj = TypedValue.protoToJdbc(proto, DateTimeUtils.calendar()); assertThat(deserializedObj, is(instanceOf(byte[].class))); assertArrayEquals(bytes, (byte[]) deserializedObj); }
@Test public void testBase64() { byte[] bytes = "qwertyasdf".getBytes(UTF_8); // Plain bytes get put into protobuf for simplicitly Common.TypedValue proto = Common.TypedValue.newBuilder().setBytesValue( com.google.protobuf.ByteString.copyFrom(bytes)) .setType(Common.Rep.BYTE_STRING).build(); // But we should get back a b64-string to make sure TypedValue doesn't get confused. Object deserializedObj = TypedValue.getSerialFromProto(proto); assertThat(deserializedObj, is(instanceOf(String.class))); assertEquals(new ByteString(bytes).toBase64String(), (String) deserializedObj); // But we should get a non-b64 byte array as the JDBC representation deserializedObj = TypedValue.protoToJdbc(proto, DateTimeUtils.calendar()); assertThat(deserializedObj, is(instanceOf(byte[].class))); assertArrayEquals(bytes, (byte[]) deserializedObj); }