Skip to content

Commit 3cf9f9c

Browse files
authored
Merge pull request #137 from eclipse/msp_serializeNull
Always write a null value into message body
2 parents 8be8e5a + bf163ad commit 3cf9f9c

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

org.eclipse.lsp4j.jsonrpc/src/main/java/org/eclipse/lsp4j/jsonrpc/json/adapters/MessageTypeAdapter.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ public void write(JsonWriter out, Message message) throws IOException {
287287
out.name("params");
288288
Object params = requestMessage.getParams();
289289
if (params == null)
290-
out.nullValue();
290+
writeNullValue(out);
291291
else
292292
gson.toJson(params, params.getClass(), out);
293293
} else if (message instanceof ResponseMessage) {
@@ -301,7 +301,7 @@ public void write(JsonWriter out, Message message) throws IOException {
301301
out.name("result");
302302
Object result = responseMessage.getResult();
303303
if (result == null)
304-
out.nullValue();
304+
writeNullValue(out);
305305
else
306306
gson.toJson(result, result.getClass(), out);
307307
}
@@ -312,12 +312,22 @@ public void write(JsonWriter out, Message message) throws IOException {
312312
out.name("params");
313313
Object params = notificationMessage.getParams();
314314
if (params == null)
315-
out.nullValue();
315+
writeNullValue(out);
316316
else
317317
gson.toJson(params, params.getClass(), out);
318318
}
319319

320320
out.endObject();
321321
}
322322

323+
/**
324+
* Use this method to write a {@code null} value even if the JSON writer is set to not serialize {@code null}.
325+
*/
326+
protected void writeNullValue(JsonWriter out) throws IOException {
327+
boolean previousSerializeNulls = out.getSerializeNulls();
328+
out.setSerializeNulls(true);
329+
out.nullValue();
330+
out.setSerializeNulls(previousSerializeNulls);
331+
}
332+
323333
}

org.eclipse.lsp4j.jsonrpc/src/test/java/org/eclipse/lsp4j/jsonrpc/test/IntegrationTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,8 @@ public CompletableFuture<MyParam> askServer(MyParam param) {
344344
logMessages.await(Level.INFO, "Unsupported notification method: $/foo1");
345345
logMessages.await(Level.INFO, "Unsupported request method: $/foo2");
346346

347-
Assert.assertEquals("Content-Length: 26" + CRLF + CRLF
348-
+ "{\"jsonrpc\":\"2.0\",\"id\":\"1\"}",
347+
Assert.assertEquals("Content-Length: 40" + CRLF + CRLF
348+
+ "{\"jsonrpc\":\"2.0\",\"id\":\"1\",\"result\":null}",
349349
out.toString());
350350
} finally {
351351
logMessages.unregister();

org.eclipse.lsp4j/src/test/java/org/eclipse/lsp4j/test/services/NullResponseTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class NullResponseTest implements LanguageServer {
4747
request.setId("1");
4848
request.setMethod("shutdown");
4949
re.consume(request);
50-
Assert.assertEquals("{\"jsonrpc\":\"2.0\",\"id\":\"1\"}", handler.serialize(msgs.get(0)));
50+
Assert.assertEquals("{\"jsonrpc\":\"2.0\",\"id\":\"1\",\"result\":null}", handler.serialize(msgs.get(0)));
5151
msgs.clear();
5252
shutdownReturn = new Object();
5353
re.consume(request);

0 commit comments

Comments
 (0)