From 7fe588eb5681339a5ec3c565cce6a6d8355c2a5b Mon Sep 17 00:00:00 2001 From: Eric Erhardt Date: Fri, 13 Jun 2025 14:39:00 -0500 Subject: [PATCH] Remove Dependency on Newtonsoft.Json from JsonRpc The call to JsonConvert from JsonRpc.InvokeCoreAsync is unconditionally preserving Newtonsoft.Json in trimmed/aot'd apps. Inspecting the current code, I am not sure how this error case ever happens. The call to InvokeCoreAsync only returns JsonRpcError or JsonRpcResult objects. The else shouldn't ever be taken. Instead of serializing the object using Newtonsoft.Json, simply put the object's Type into the exception message to indicate what the unexpected object was. --- src/StreamJsonRpc/JsonRpc.cs | 2 +- src/StreamJsonRpc/Resources.resx | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/StreamJsonRpc/JsonRpc.cs b/src/StreamJsonRpc/JsonRpc.cs index ea125ce73..42f32b141 100644 --- a/src/StreamJsonRpc/JsonRpc.cs +++ b/src/StreamJsonRpc/JsonRpc.cs @@ -1575,7 +1575,7 @@ protected async Task InvokeCoreAsync(RequestId id, string targ } else { - throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, Resources.ResponseUnexpectedFormat, JsonConvert.SerializeObject(response))); + throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, Resources.ResponseUnexpectedFormat, response?.GetType().FullName ?? "(null)")); } } else diff --git a/src/StreamJsonRpc/Resources.resx b/src/StreamJsonRpc/Resources.resx index f61ffd63e..e87e9b73a 100644 --- a/src/StreamJsonRpc/Resources.resx +++ b/src/StreamJsonRpc/Resources.resx @@ -288,8 +288,8 @@ Failed to serialize the response. - Response is in an unexpected format. Only error and result are supported: {0} - {0} is the response message. + Response is in an unexpected format. Only error and result are supported, but received type: {0} + {0} is the response object type. Optional interface declarations should be unique. {0} has the same optional interface declared multiple times.