Moving away from BinaryFormaters, I started to use Text.Json.
I'm trying to serialize/deserialize a simple class containing a dynamic object, named 'PropertyValue'. ' PropertyType' is set by the application. In this sample, PropertyValue is a System.Drawing.Point
Public Class FormSetting
Public Property FormName As String
Public Property ControlName As String
Public Property PropertyName As String
Public Property PropertyType As String
Public Property PropertyValue As Object
End Class
Serializer:
Using sw As New IO.StreamWriter(SettingLocation, IO.FileMode.Create)
Dim jsonString = JsonSerializer.Serialize(SettingList, JsonOptions)
sw.Write(jsonString)
End Using
Deserialize
Using fs As New IO.FileStream(SettingLocation, IO.FileMode.Open)
Dim FormSettingResult As FormSetting()
FormSettingResult = JsonSerializer.Deserialize(Of FormSetting())(fs,LoadSettings.AddRange(FormSettingResult)
End Using
The serialized json looks like:
{
"FormName": "UnlockForm",
"ControlName": "UnlockForm",
"PropertyName": "Location",
"PropertyType": "Point",
"PropertyValue": {
"X": 304,
"Y": 243
}
}
note that the serialized data doesn't save the original type (point in this case), the deserialized PropertyValue value is:
RootElement = ValueKind = Object : "{
"X": 304,
"Y": 243
}"
and not the expected type Drawing.Point, with valued Point(304,243).
Is there a JSON method to convert ValueKind to a the desired object? for example using something like (a pseudo) json.ConvertObject:
dim PointLocation = json.ConvertObject(of Drawing.Point)(item.PropertyValue)