0

I am switching over my object serialization in my app because it said it was depreciated (https://docs.microsoft.com/en-us/dotnet/standard/serialization/binaryformatter-security-guide). I am now attempting to use System.Text.Json API's as described in the article as a replacement. Everything works great when I serialize my objects:

 Function Serialize(ByVal objData As Object) As Byte()
    If objData Is Nothing Then Return Nothing
    Return Encoding.UTF8.GetBytes(JsonSerializer.Serialize(objData, GetJsonSerializerOptions()))
End Function

Function Deserialize(Of T)(ByVal byteArray As Byte()) As T
    If byteArray Is Nothing OrElse Not byteArray.Any() Then Return Nothing
    Return JsonSerializer.Deserialize(Of T)(byteArray, GetJsonSerializerOptions())
End Function
Private Function GetJsonSerializerOptions() As JsonSerializerOptions
    Return New JsonSerializerOptions() With {
        .PropertyNamingPolicy = Nothing,
        .WriteIndented = True,
        .AllowTrailingCommas = True,
        .DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
    }
End Function

The issue arises in one of my properties where I am using a dynamic object type. When I try to deserialize it I get this error: "System.InvalidCastException: Conversion from type JsonElement to type String is not valid."

 Private Property Answer as Object

I get this I am assuming because the properties type is not strictly defined so when it tries to deserialize it again it doesn't know it's type. The issue is I can't strictly define this type because it could contain data that's either string, boolean, etc.

How do I handle something like this? Not really sure what to do as I am fairly new to JSON.

Thanks

dbc
  • 104,963
  • 20
  • 228
  • 340
Willy
  • 137
  • 13
  • What is included in that `etc`? Do you know in advance the types of all the possible object values? – dbc Nov 28 '22 at 22:25
  • Yes I will know in advance all the types. For now lets just assume I will always have a string and boolean. – Willy Nov 28 '22 at 22:30
  • 1
    If it's just primitives, grab `ObjectToInferredTypesConverter` from [the documentation](https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/converters-how-to?pivots=dotnet-7-0#deserialize-inferred-types-to-object-properties), translate to vb.net, and you should be good to go. – dbc Nov 28 '22 at 22:32
  • That looks promising. I will give it a shot and report back. – Willy Nov 28 '22 at 22:36
  • Related: [Deserializing a data member that could be int or string c#](https://stackoverflow.com/q/74607069/3744182). – dbc Nov 28 '22 at 22:39
  • 1
    Receiving an error that utf8jsonreader is obsolete?? Uggh fml...https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/visual-basic-support – Willy Nov 29 '22 at 15:36
  • 1
    That's an unpleasant surprise. You may need to either 1) Add a small c# project to your solution with the required converter, see [Using C# and VB.NET in one solution](https://stackoverflow.com/q/2903430) or [Mixing C# & VB In The Same Project](https://stackoverflow.com/q/1278024) or 2) Explicitly handle `JsonElement` in the setter. Any preference? – dbc Nov 29 '22 at 16:05
  • I think I might try to just explicitly use a string since it's so basic ... i can try to cast the string into a boolean etc from within my code. If I need to expand outside of those primitives I will go the other route. Thank you for your assitance! – Willy Nov 29 '22 at 16:24
  • That could work, but be sure to use [`CultureInfo.InvariantCulture`](https://learn.microsoft.com/en-us/dotnet/api/system.globalization.cultureinfo.invariantculture) if you do to ensure that the decimal separator and negative sign are consistent in all locales. – dbc Nov 29 '22 at 16:27
  • Also, if you do go that way, you might add a [self answer](https://stackoverflow.com/help/self-answer) that mentions the utf8jsonreader vb.net-only error since the inability to write a converter in vb.net is so surprising (to me at least). – dbc Nov 29 '22 at 16:30

0 Answers0