Skip to content

Commit

Permalink
😊 修复 JsonNode 转换为字符串类型出现双引号包裹问题
Browse files Browse the repository at this point in the history
  • Loading branch information
MonkSoul committed Dec 27, 2024
1 parent 17df0c4 commit 8ba341a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,18 @@ internal static TResult?
/// <param name="jsonNode">
/// <see cref="JsonNode" />
/// </param>
/// <param name="returnType">转换的目标类型</param>
/// <param name="resultType">转换的目标类型</param>
/// <param name="jsonSerializerOptions">
/// <see cref="JsonSerializerOptions" />
/// </param>
/// <returns>
/// <see cref="object" />
/// </returns>
internal static object? As(this JsonNode? jsonNode, Type returnType,
internal static object? As(this JsonNode? jsonNode, Type resultType,
JsonSerializerOptions? jsonSerializerOptions = null)
{
// 空检查
ArgumentNullException.ThrowIfNull(returnType);
ArgumentNullException.ThrowIfNull(resultType);

// 空检查
if (jsonNode is null)
Expand All @@ -80,13 +80,16 @@ internal static TResult?
}

// 处理目标类型为字符串类型
if (returnType == typeof(string))
if (resultType == typeof(string))
{
return jsonNode.ToJsonString(jsonSerializerOptions);
// 处理转换为字符串类型出现双引号包裹问题
return jsonNode.GetValueKind() is JsonValueKind.String
? jsonNode.GetValue<string>()
: jsonNode.ToJsonString(jsonSerializerOptions);
}

// 处理目标类型为 XElement 类型
if (returnType == typeof(XElement))
if (resultType == typeof(XElement))
{
// 初始化 MemoryStream 实例
using var ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonNode.ToJsonString(jsonSerializerOptions)));
Expand All @@ -111,7 +114,7 @@ internal static TResult?
}

// 反序列化输出目标类型实例
return JsonSerializer.Deserialize(memoryStream.ToArray(), returnType, jsonSerializerOptions);
return JsonSerializer.Deserialize(memoryStream.ToArray(), resultType, jsonSerializerOptions);
}

/// <summary>
Expand Down
17 changes: 10 additions & 7 deletions framework/Furion/V5_Experience/Core/Extensions/JsonExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,18 @@ internal static TResult?
/// <param name="jsonNode">
/// <see cref="JsonNode" />
/// </param>
/// <param name="returnType">转换的目标类型</param>
/// <param name="resultType">转换的目标类型</param>
/// <param name="jsonSerializerOptions">
/// <see cref="JsonSerializerOptions" />
/// </param>
/// <returns>
/// <see cref="object" />
/// </returns>
internal static object? As(this JsonNode? jsonNode, Type returnType,
internal static object? As(this JsonNode? jsonNode, Type resultType,
JsonSerializerOptions? jsonSerializerOptions = null)
{
// 空检查
ArgumentNullException.ThrowIfNull(returnType);
ArgumentNullException.ThrowIfNull(resultType);

// 空检查
if (jsonNode is null)
Expand All @@ -80,13 +80,16 @@ internal static TResult?
}

// 处理目标类型为字符串类型
if (returnType == typeof(string))
if (resultType == typeof(string))
{
return jsonNode.ToJsonString(jsonSerializerOptions);
// 处理转换为字符串类型出现双引号包裹问题
return jsonNode.GetValueKind() is JsonValueKind.String
? jsonNode.GetValue<string>()
: jsonNode.ToJsonString(jsonSerializerOptions);
}

// 处理目标类型为 XElement 类型
if (returnType == typeof(XElement))
if (resultType == typeof(XElement))
{
// 初始化 MemoryStream 实例
using var ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonNode.ToJsonString(jsonSerializerOptions)));
Expand All @@ -111,7 +114,7 @@ internal static TResult?
}

// 反序列化输出目标类型实例
return JsonSerializer.Deserialize(memoryStream.ToArray(), returnType, jsonSerializerOptions);
return JsonSerializer.Deserialize(memoryStream.ToArray(), resultType, jsonSerializerOptions);
}

/// <summary>
Expand Down

0 comments on commit 8ba341a

Please sign in to comment.