JsonMessageReader { public T next() throws IOException { if (firstRead) { int firstChar = reader.read(); if (firstChar == -1) { return null; } else { reader.unread(firstChar); firstRead = false; } } if (streamParser.hasNext()) { try { return GSON.fromJson(streamParser.next(), messageClass); } catch (JsonIOException e) { throw new IOException(e); } catch (JsonParseException ignore) { } } return null; } JsonMessageReader(InputStream source, Class<T> messageClass); T next();  }
@Test public void shouldParseSequenceOfProcessStatusObjects() throws IOException { final String src = "{\"stream\":\"Step 0 : FROM busybox\\n\"}\n" + "{\"status\":\"The image you are pulling has been verified\",\"id\":\"busybox:latest\"}\n"; final JsonMessageReader<ProgressStatus> reader = new JsonMessageReader<>(new ByteArrayInputStream(src.getBytes()), ProgressStatus.class); final ProgressStatus status1 = reader.next(); final ProgressStatus status2 = reader.next(); assertEquals(status1.getStream(), "Step 0 : FROM busybox\n"); assertEquals(status2.getStatus(), "The image you are pulling has been verified"); assertEquals(status2.getId(), "busybox:latest"); assertNull(reader.next()); }
@Test public void shouldReturnNullIfJsonIsIncorrect() throws IOException { final String src = "not json"; final JsonMessageReader<ProgressStatus> reader = new JsonMessageReader<>(new ByteArrayInputStream(src.getBytes()), ProgressStatus.class); assertNull(reader.next()); }