0

I want to deserialize a few VB.Net objects. Two classes of AllgTyp inherit here and are packed in a list.

If it were not a list, but non-nested objects, a simple JSON converter (how to deserialize JSON into IEnumerable<BaseType> with Newtonsoft JSON.NET) would be a solution. But how do I resolve the whole thing so that the nesting is deserialized?

Imports Newtonsoft.Json
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'Install Newtonsoft over "View" > "Other Windows" > "Package Manager Console" -> type in:
        'Install-Package Newtonsoft.Json

        Dim output As String = JsonConvert.SerializeObject(testemal(), Formatting.Indented)
        Dim deserializedProduct As List(Of AllgTyp) = JsonConvert.DeserializeObject(Of List(Of AllgTyp))(output)

    End Sub
    Private Function testemal() As List(Of AllgTyp)
        Dim lstMarken As New List(Of AllgTyp)
        Dim textmarken1 As New Verschachtelung("vorbriefanrede", "C:\tmp2\anrede.docx", "sdfs")
        lstMarken.Add(textmarken1)
        Dim textmarken2 As New Verschachtelung("inhalt", "C:\tmp2\DatMeinInhalt.docx", "sdfsdfinInhalt")
        lstMarken.Add(textmarken2)

        Dim aa As New Textmarke("datum", "24.02.2020")
        textmarken2.Verschachtelung.Add(aa)

        Dim ab As New Textmarke("Empfaenger", "isjdfosdfjosdjf")
        textmarken2.Verschachtelung.Add(ab)

        Dim c2 As New Verschachtelung("mbnmbnmbnm", "C:\tmp2\DatMeinInhalt.docx", "sfssfachinsdfsdfhalt1")
        textmarken2.Verschachtelung.Add(c2)

        Dim c3 As New Verschachtelung("sdfsfsfsss", "fsdf")
        c2.Verschachtelung.Add(c3)

        Dim c4 As New Verschachtelung("sdfsdf", "vo_fachinhalt3")
        c3.Verschachtelung.Add(c4)

        Dim c5 As New Verschachtelung("cbvcbcbcv", "vo_fachinhalt4")
        c4.Verschachtelung.Add(c5)

        Dim c6 As New Verschachtelung("vcbcvbcbcbc", "vo_fachinhalt5")
        c4.Verschachtelung.Add(c6)

        Return lstMarken
    End Function
End Class


Public Class AllgTyp
    Sub New()

    End Sub
End Class
Public Class Textmarke
    Inherits AllgTyp
    Public textmarkenname As String
    Public wert As String
    Sub New()

    End Sub
    Sub New(textmarkenname As String, wert As String)
        Me.textmarkenname = textmarkenname
        Me.wert = wert
    End Sub

End Class
Public Class Verschachtelung
    Inherits AllgTyp
    Public quellDatei As String
    Public quelleBereich As String
    Public zielBereich As String
    Public Verschachtelung As New List(Of AllgTyp)
    Sub New()

    End Sub
    Sub New(zielBereich As String, quelleBereich As String)
        Me.New(zielBereich, Nothing, quelleBereich)
    End Sub

    Sub New(zielBereich As String, quellDatei As String, quelleBereich As String)
        Me.zielBereich = zielBereich
        Me.quellDatei = quellDatei
        Me.quelleBereich = quelleBereich
    End Sub

End Class
Josef Soe
  • 91
  • 8
  • JSON supports collections(`"property":[1,2,3,4]`). So the round-robin test you are doing there should pass, using the code provided. What is the error or problem you are facing? – Mike Makarov Mar 03 '20 at 14:18
  • You could apply a `JsonConverter` to `AllgTyp` via attributes like so: ``, then apply `NoConverter` from [this answer](https://stackoverflow.com/a/45554062/3744182) to [`How to call JsonConvert.DeserializeObject and disable a JsonConverter applied to a base type via [JsonConverter]?`](https://stackoverflow.com/q/45547123/3744182) to the derived classes. Or you could add the converter in [`JsonSerializerSettings.Converters`](https://www.newtonsoft.com/json/help/html/P_Newtonsoft_Json_JsonSerializerSettings_Converters.htm). – dbc Mar 03 '20 at 23:58
  • But your data model is quite complex. Are you asking is to write a `JsonConverter` for you, or are you asking how to apply the converter? Can you boil things down to more of a [mcve]? – dbc Mar 04 '20 at 00:00

0 Answers0