Skip to content

Commit

Permalink
Honor [JsonConverter] attributes on properties of custom params obj…
Browse files Browse the repository at this point in the history
…ects

Fixes microsoft#735
  • Loading branch information
AArnott committed Nov 11, 2021
1 parent d1120bf commit adadf4b
Showing 1 changed file with 9 additions and 26 deletions.
35 changes: 9 additions & 26 deletions src/StreamJsonRpc/JsonMessageFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -636,35 +636,18 @@ private void TokenizeUserData(JsonRpcMessage jsonRpcMessage)
throw new NotSupportedException(Resources.ParameterObjectsNotSupportedInJsonRpc10);
}

JObject tokenizedArgumentsObject = new();
if (request.NamedArguments is not null)
{
foreach (KeyValuePair<string, object?> pair in request.NamedArguments)
{
Type? declaredType = null;
request.NamedArgumentDeclaredTypes?.TryGetValue(pair.Key, out declaredType);
tokenizedArgumentsObject.Add(pair.Key, this.TokenizeUserData(declaredType, pair.Value));
}
}
else if (DefaultSerializer.ContractResolver.ResolveContract(request.Arguments.GetType()) is JsonObjectContract contract)
{
// The arguments should be interpreted as named arguments,
// but as they were not detected as in a dictionary,
// it may be an anonymous type. Allow Newtonsoft.Json to determine the property names and values.
foreach (JsonProperty property in contract.Properties)
{
if (property.PropertyName is not null && property.ValueProvider is not null)
{
tokenizedArgumentsObject.Add(property.PropertyName, this.TokenizeUserData(property.PropertyType, property.ValueProvider.GetValue(request.Arguments)));
}
}
}
else
// Tokenize the user data using the user-supplied serializer.
JObject? paramsObject = JObject.FromObject(request.Arguments, this.JsonSerializer);

// Json.Net TypeHandling could insert a $type child JToken to the paramsObject above.
// This $type JToken should not be there to maintain Json RPC Spec compatibility. We will
// strip the token out here.
if (this.JsonSerializer.TypeNameHandling != TypeNameHandling.None)
{
throw new JsonSerializationException("Unsupported arguments object type: " + request.Arguments.GetType());
paramsObject.Remove("$type");
}

request.Arguments = tokenizedArgumentsObject;
request.Arguments = paramsObject;
}

break;
Expand Down

0 comments on commit adadf4b

Please sign in to comment.