public override Exception? ReadJson()

in src/StreamJsonRpc/JsonMessageFormatter.cs [1605:1660]


            public override Exception? ReadJson(JsonReader reader, Type objectType, Exception? existingValue, bool hasExistingValue, JsonSerializer serializer)
            {
                Assumes.NotNull(this.formatter.rpc);
                if (reader.TokenType == JsonToken.Null)
                {
                    return null;
                }

                exceptionRecursionCounter.Value++;
                try
                {
                    if (reader.TokenType != JsonToken.StartObject)
                    {
                        throw new InvalidOperationException("Expected a StartObject token.");
                    }

                    if (exceptionRecursionCounter.Value > this.formatter.rpc.ExceptionOptions.RecursionLimit)
                    {
                        // Exception recursion has gone too deep. Skip this value and return null as if there were no inner exception.
                        // Note that in skipping, the parser may use recursion internally and may still throw if its own limits are exceeded.
                        reader.Skip();
                        return null;
                    }

                    SerializationInfo? info = new SerializationInfo(objectType, new JsonConverterFormatter(serializer));
                    while (reader.Read())
                    {
                        if (reader.TokenType == JsonToken.EndObject)
                        {
                            break;
                        }

                        if (reader.TokenType == JsonToken.PropertyName)
                        {
                            string name = (string)reader.Value!;
                            if (!reader.Read())
                            {
                                throw new EndOfStreamException();
                            }

                            JToken? value = reader.TokenType == JsonToken.Null ? null : JToken.Load(reader);
                            info.AddSafeValue(name, value);
                        }
                        else
                        {
                            throw new InvalidOperationException("Expected PropertyName token but encountered: " + reader.TokenType);
                        }
                    }

                    return ExceptionSerializationHelpers.Deserialize<Exception>(this.formatter.rpc, info, this.formatter.rpc?.TraceSource);
                }
                finally
                {
                    exceptionRecursionCounter.Value--;
                }
            }