public String serialize(Message message) { StringWriter writer = new StringWriter(); serialize(message, writer); return writer.toString(); }
public String serialize(Message message) { StringWriter writer = new StringWriter(); serialize(message, writer); return writer.toString(); }
@Override public void consume(Message message) { try { String content = jsonHandler.serialize(message); byte[] contentBytes = content.getBytes(encoding); int contentLength = contentBytes.length; String header = getHeader(contentLength); byte[] headerBytes = header.getBytes(StandardCharsets.US_ASCII); synchronized (outputLock) { output.write(headerBytes); output.write(contentBytes); output.flush(); } } catch (IOException exception) { throw new JsonRpcException(exception); } }
@Override public void consume(Message message) { try { String content = jsonHandler.serialize(message); byte[] contentBytes = content.getBytes(encoding); int contentLength = contentBytes.length; String header = getHeader(contentLength); byte[] headerBytes = header.getBytes(StandardCharsets.US_ASCII); synchronized (outputLock) { output.write(headerBytes); output.write(contentBytes); output.flush(); } } catch (IOException exception) { throw new JsonRpcException(exception); } }
private void assertSerialize(final Message message, final CharSequence expected) { Assert.assertEquals(expected.toString().trim(), LineEndings.toSystemLineEndings(this.jsonHandler.serialize(message))); }
@Test public void testSerializeImmutableList() { MessageJsonHandler handler = new MessageJsonHandler(Collections.emptyMap()); NotificationMessage message = new NotificationMessage(); message.setMethod("foo"); List<Object> list = new ArrayList<>(); list.add("a"); list.add("b"); message.setParams(Collections.unmodifiableList(list)); String json = handler.serialize(message); Assert.assertEquals("{\"jsonrpc\":\"2.0\",\"method\":\"foo\",\"params\":[\"a\",\"b\"]}", json); }
@Test public void testSerializeEmptyList() { MessageJsonHandler handler = new MessageJsonHandler(Collections.emptyMap()); NotificationMessage message = new NotificationMessage(); message.setMethod("foo"); message.setParams(Collections.EMPTY_LIST); String json = handler.serialize(message); Assert.assertEquals("{\"jsonrpc\":\"2.0\",\"method\":\"foo\",\"params\":[]}", json); }
@Test public void testNullResponse() throws InterruptedException, ExecutionException { LogMessageAccumulator logMessages = new LogMessageAccumulator(); try { logMessages.registerTo(GenericEndpoint.class); Endpoint endpoint = ServiceEndpoints.toEndpoint(this); Map<String, JsonRpcMethod> methods = ServiceEndpoints.getSupportedMethods(LanguageServer.class); MessageJsonHandler handler = new MessageJsonHandler(methods); List<Message> msgs = new ArrayList<>(); MessageConsumer consumer = (message) -> { msgs.add(message); }; RemoteEndpoint re = new RemoteEndpoint(consumer, endpoint); RequestMessage request = new RequestMessage(); request.setId("1"); request.setMethod("shutdown"); re.consume(request); Assert.assertEquals("{\"jsonrpc\":\"2.0\",\"id\":\"1\",\"result\":null}", handler.serialize(msgs.get(0))); msgs.clear(); shutdownReturn = new Object(); re.consume(request); Assert.assertEquals("{\"jsonrpc\":\"2.0\",\"id\":\"1\",\"result\":{}}", handler.serialize(msgs.get(0))); } finally { logMessages.unregister(); } }