VB.Net中使用System.Json反序列化Object类型类属性的问题咨询
VB.Net中System.Text.Json序列化/反序列化问题解决方案
一、解决JsonConverter的废弃错误
你遇到的方法签名废弃问题,是因为System.Text.Json的API更新:原Read方法中Utf8JsonReader参数的ByRef写法已被淘汰,现在需要改为ByVal。VB.Net完全支持更新后的API,正确的方法签名应为:
Public Overrides Function Read(ByVal reader As Utf8JsonReader, typeToConvert As Type, options As JsonSerializerOptions) As Object
二、自定义转换器处理Object类型反序列化
由于OriginalValue和NewValue是Object类型,反序列化时需要根据PropertyName映射到具体类型(如Demand、Location),可通过自定义转换器实现:
- 实现Change类的自定义转换器
Public Class ChangeConverter Inherits JsonConverter(Of Change) Public Overrides Function Read(ByVal reader As Utf8JsonReader, typeToConvert As Type, options As JsonSerializerOptions) As Change If reader.TokenType <> JsonTokenType.StartObject Then Throw New JsonException("JSON格式错误:预期对象起始标记") End If Dim change As New Change() Dim currentPropName As String = Nothing While reader.Read() If reader.TokenType = JsonTokenType.EndObject Then Return change End If If reader.TokenType = JsonTokenType.PropertyName Then currentPropName = reader.GetString() reader.Read() Select Case currentPropName Case "PropertyName" change.PropertyName = reader.GetString() Case "OriginalValue" change.OriginalValue = DeserializeTargetValue(reader, change.PropertyName, options) Case "NewValue" change.NewValue = DeserializeTargetValue(reader, change.PropertyName, options) Case Else reader.Skip() ' 跳过未知属性 End Select End If End While Throw New JsonException("JSON格式错误:未找到对象结束标记") End Function ' 根据PropertyName映射目标类型并反序列化 Private Function DeserializeTargetValue(ByVal reader As Utf8JsonReader, propName As String, options As JsonSerializerOptions) As Object Dim targetType As Type = propName.ToLower() switch { "demand" => GetType(Demand), "location" => GetType(Location), ' 在此添加其他需要处理的类型映射 Else => GetType(Object) } Return JsonSerializer.Deserialize(reader, targetType, options) End Function Public Overrides Sub Write(ByVal writer As Utf8JsonWriter, value As Change, options As JsonSerializerOptions) JsonSerializer.Serialize(writer, value, options) End Sub End Class
- 反序列化时注册转换器
Dim options As New JsonSerializerOptions() options.Converters.Add(New ChangeConverter()) ' 执行反序列化 Dim model = JsonSerializer.Deserialize(Of Model)(jsonContent, options)
三、替代方案:开启类型信息自动识别
若不想编写自定义转换器,可在序列化时开启类型信息保留,让System.Text.Json自动识别目标类型:
Dim serializeOptions As New JsonSerializerOptions() serializeOptions.WriteIndented = True serializeOptions.TypeInfoResolver = New DefaultJsonTypeInfoResolver() With { .Modifiers = { Sub(typeInfo) If typeInfo.Type = GetType(Object) Then typeInfo.PolymorphismOptions = New JsonPolymorphismOptions() With { .TypeDiscriminatorPropertyName = "$type", .IgnoreUnrecognizedTypeDiscriminators = True, .DerivedTypes = { New JsonDerivedType(GetType(Demand), "Demand"), New JsonDerivedType(GetType(Location), "Location") ' 添加其他需要识别的类型 } } End If End Sub } } ' 序列化 Dim jsonString = JsonSerializer.Serialize(model, serializeOptions) ' 反序列化使用相同配置 Dim deserializedModel = JsonSerializer.Deserialize(Of Model)(jsonString, serializeOptions)
关于VB.Net支持Json转换的说明
网传“VB.Net不再支持Json转换”的说法完全错误,VB.Net完全兼容System.Text.Json的所有功能,包括自定义转换器。你遇到的废弃提示仅为API版本更新导致的参数写法变更,调整签名即可解决。
内容的提问来源于stack exchange,提问作者Chiel
相关产品推荐
相关产品推荐

