0

I'am trying to deserialize a json object that has a virtual property. Please see this minimal sample to illustrate my issue :

Dim jsonInput As String = "{""props"": {""dogOnlyProperty"":42} }"
Dim jss As New JavaScriptSerializer()
Dim stuffObj As Stuff = jss.Deserialize(Of Stuff)(jsonInput)


Public Class Stuff
    Public props As Animal.AnimalProperties
End Class

Public MustInherit Class Animal
    Public MustInherit Class AnimalProperties
    End Class

    Public name As String
    Public MustOverride Function GetCustomProperties() As AnimalProperties
End Class

Public Class Dog
    Inherits Animal

    Public Class DogProperties
        Inherits AnimalProperties
        Public dogOnlyProperty As Integer
    End Class

    Public Overrides Function GetCustomProperties() As Animal.AnimalProperties
        Return New DogProperties()
    End Function
End Class

Public Class Cat
    Inherits Animal

    Public Class CatProperties
        Inherits AnimalProperties
        Public catOnlyProperty As String
    End Class


    Public Overrides Function GetCustomProperties() As Animal.AnimalProperties
        Return New CatProperties()
    End Function
End Class

The code above thows an exception because the serializer doesn't know the real type of AnimalProperties. How do I tell the serializer that I know AnimalProperties should be casted to DogProperties ?

(If someone wants the C# version, I can write it).

KVM
  • 878
  • 3
  • 13
  • 22

1 Answers1

0

Found the anwser here : How to implement custom JsonConverter in JSON.NET to deserialize a List of base class objects?

I changed the native serializer with JSON.NET and wrote a custom jsonConverter.

Community
  • 1
  • 1
KVM
  • 878
  • 3
  • 13
  • 22