Skip to content

Commit

Permalink
Fixed JSON length determination
Browse files Browse the repository at this point in the history
  • Loading branch information
shyiko committed Oct 18, 2016
1 parent e0dfa01 commit 881e9c7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ protected Serializable deserializeGeometry(int meta, ByteArrayInputStream inputS
* @throws IOException if there is a problem reading the input stream
*/
protected byte[] deserializeJson(int meta, ByteArrayInputStream inputStream) throws IOException {
int blobLength = inputStream.readInteger(4);
int blobLength = inputStream.readInteger(meta);
return inputStream.read(blobLength);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ private int[] readMetadata(ByteArrayInputStream inputStream, byte[] columnTypes)
case FLOAT:
case DOUBLE:
case BLOB:
case JSON:
case GEOMETRY:
metadata[i] = inputStream.readInteger(1);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class JsonBinaryValueIntegrationTest {

private static final long DEFAULT_TIMEOUT = TimeUnit.SECONDS.toMillis(3);

private BinaryLogClient client;
private BinaryLogClientIntegrationTest.MySQLConnection master;
private Map<Integer, byte[]> jsonValuesByKey;

Expand All @@ -56,16 +57,15 @@ public void setUp() throws Exception {
master = new BinaryLogClientIntegrationTest.MySQLConnection(bundle.getString(prefix + "master.hostname"),
Integer.parseInt(bundle.getString(prefix + "master.port")),
bundle.getString(prefix + "master.username"), bundle.getString(prefix + "master.password"));
BinaryLogClient client = new BinaryLogClient(master.hostname(), master.port(), master.username(),
master.password());
client = new BinaryLogClient(master.hostname(), master.port(), master.username(), master.password());
client.setServerId(client.getServerId() - 1); // avoid clashes between BinaryLogClient instances
client.setKeepAlive(false);
// Uncomment the next line for detailed traces of the events ...
// client.registerEventListener(new TraceEventListener());
CountDownEventListener eventListener;
client.registerEventListener(eventListener = new CountDownEventListener());
// client.registerLifecycleListener(new TraceLifecycleListener());
client.connect(DEFAULT_TIMEOUT);
CountDownEventListener eventListener = new CountDownEventListener();
client.registerEventListener(eventListener);
try {
master.execute("drop database if exists json_test",
"create database json_test",
Expand Down Expand Up @@ -141,6 +141,22 @@ public void setUp() throws Exception {
}
}

@Test
public void testLengthDeserialization() throws Exception {
CountDownEventListener eventListener = new CountDownEventListener();
client.registerEventListener(eventListener);
CapturingEventListener capturingEventListener = new CapturingEventListener();
client.registerEventListener(capturingEventListener);
master.execute("create table json_b (h varchar(255), j JSON, k varchar(255))",
"INSERT INTO json_b VALUES ('sponge', '{}', 'bob');");
eventListener.waitFor(WriteRowsEventData.class, 1, DEFAULT_TIMEOUT);
List<WriteRowsEventData> events = capturingEventListener.getEvents(WriteRowsEventData.class);
Serializable[] data = events.iterator().next().getRows().get(0);
assertEquals(data[0], "sponge");
assertEquals(JsonBinary.parseAsString((byte[]) data[1]), "{}");
assertEquals(data[2], "bob");
}

@Test
public void testNullJsonValue() throws Exception {
assertJson(0, null);
Expand Down

0 comments on commit 881e9c7

Please sign in to comment.