From 15238001cb80a896597321ab268a661f47845668 Mon Sep 17 00:00:00 2001 From: Ivan Murzak Date: Fri, 16 Jan 2026 18:02:19 -0800 Subject: [PATCH 1/2] Handle JsonException in TryUnstringifyJson calls Wrapped calls to JsonUtils.TryUnstringifyJson in try-catch blocks to handle potential JsonException and prevent crashes when input is not a stringified JSON. Also moved the file header comment in JsonUtils.cs outside the namespace for clarity. --- ReflectorNet/src/Reflector/MethodWrapper.cs | 26 ++++++++++++++++----- ReflectorNet/src/Utils/Json/JsonUtils.cs | 14 +++++------ 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/ReflectorNet/src/Reflector/MethodWrapper.cs b/ReflectorNet/src/Reflector/MethodWrapper.cs index e930ae75..7c0c3ada 100644 --- a/ReflectorNet/src/Reflector/MethodWrapper.cs +++ b/ReflectorNet/src/Reflector/MethodWrapper.cs @@ -268,10 +268,17 @@ public virtual bool VerifyParameters(IReadOnlyDictionary? named if (!isPrimitive) { // Handle stringified json - if (JsonUtils.TryUnstringifyJson(jsonElement, out var unstringifiedJson)) + try + { + if (JsonUtils.TryUnstringifyJson(jsonElement, out var unstringifiedJson)) + { + value = unstringifiedJson; + jsonElement = unstringifiedJson!.Value; + } + } + catch (JsonException) { - value = unstringifiedJson; - jsonElement = unstringifiedJson!.Value; + // Ignore, value is not a stringified JSON } } try @@ -352,10 +359,17 @@ public virtual bool VerifyParameters(IReadOnlyDictionary? named if (!isPrimitive) { // Handle stringified json - if (JsonUtils.TryUnstringifyJson(jsonElement, out var unstringifiedJson)) + try { - value = unstringifiedJson; - jsonElement = unstringifiedJson!.Value; + if (JsonUtils.TryUnstringifyJson(jsonElement, out var unstringifiedJson)) + { + value = unstringifiedJson; + jsonElement = unstringifiedJson!.Value; + } + } + catch (JsonException) + { + // Ignore, value is not a stringified JSON } } try diff --git a/ReflectorNet/src/Utils/Json/JsonUtils.cs b/ReflectorNet/src/Utils/Json/JsonUtils.cs index 72497604..f28461ff 100644 --- a/ReflectorNet/src/Utils/Json/JsonUtils.cs +++ b/ReflectorNet/src/Utils/Json/JsonUtils.cs @@ -1,14 +1,14 @@ +/* +* ReflectorNet +* Author: Ivan Murzak (https://github.com/IvanMurzak) +* Copyright (c) 2025 Ivan Murzak +* Licensed under the Apache License, Version 2.0. See LICENSE file in the project root for full license information. +*/ + using System.Text.Json; namespace com.IvanMurzak.ReflectorNet.Utils { - /* - * ReflectorNet - * Author: Ivan Murzak (https://github.com/IvanMurzak) - * Copyright (c) 2025 Ivan Murzak - * Licensed under the Apache License, Version 2.0. See LICENSE file in the project root for full license information. - */ - public static class JsonUtils { public static bool TryUnstringifyJson(JsonElement jsonElement, out JsonElement? result) From d083877532e65103fb3f1ada507f373550db0e98 Mon Sep 17 00:00:00 2001 From: Ivan Murzak Date: Fri, 16 Jan 2026 18:08:41 -0800 Subject: [PATCH 2/2] fix: Simplify JSON unstringification by removing redundant try-catch blocks --- ReflectorNet/src/Reflector/MethodWrapper.cs | 26 +++++---------------- ReflectorNet/src/Utils/Json/JsonUtils.cs | 12 +++++++--- 2 files changed, 15 insertions(+), 23 deletions(-) diff --git a/ReflectorNet/src/Reflector/MethodWrapper.cs b/ReflectorNet/src/Reflector/MethodWrapper.cs index 7c0c3ada..e930ae75 100644 --- a/ReflectorNet/src/Reflector/MethodWrapper.cs +++ b/ReflectorNet/src/Reflector/MethodWrapper.cs @@ -268,17 +268,10 @@ public virtual bool VerifyParameters(IReadOnlyDictionary? named if (!isPrimitive) { // Handle stringified json - try - { - if (JsonUtils.TryUnstringifyJson(jsonElement, out var unstringifiedJson)) - { - value = unstringifiedJson; - jsonElement = unstringifiedJson!.Value; - } - } - catch (JsonException) + if (JsonUtils.TryUnstringifyJson(jsonElement, out var unstringifiedJson)) { - // Ignore, value is not a stringified JSON + value = unstringifiedJson; + jsonElement = unstringifiedJson!.Value; } } try @@ -359,17 +352,10 @@ public virtual bool VerifyParameters(IReadOnlyDictionary? named if (!isPrimitive) { // Handle stringified json - try - { - if (JsonUtils.TryUnstringifyJson(jsonElement, out var unstringifiedJson)) - { - value = unstringifiedJson; - jsonElement = unstringifiedJson!.Value; - } - } - catch (JsonException) + if (JsonUtils.TryUnstringifyJson(jsonElement, out var unstringifiedJson)) { - // Ignore, value is not a stringified JSON + value = unstringifiedJson; + jsonElement = unstringifiedJson!.Value; } } try diff --git a/ReflectorNet/src/Utils/Json/JsonUtils.cs b/ReflectorNet/src/Utils/Json/JsonUtils.cs index f28461ff..a0a5d40e 100644 --- a/ReflectorNet/src/Utils/Json/JsonUtils.cs +++ b/ReflectorNet/src/Utils/Json/JsonUtils.cs @@ -16,9 +16,15 @@ public static bool TryUnstringifyJson(JsonElement jsonElement, out JsonElement? if (jsonElement.ValueKind == JsonValueKind.String) { var str = jsonElement.GetString() ?? string.Empty; - - result = System.Text.Json.JsonSerializer.Deserialize(str); - return true; + try + { + result = System.Text.Json.JsonSerializer.Deserialize(str); + return true; + } + catch (JsonException) + { + // Ignore, value is not a stringified JSON + } } result = null; return false;