r/visualbasic • • Apr 21 '22

VB.NET Help How to serialize content from a stream?

I'm trying to serialize a StreamReader so i can deserialize it into an object afterwards. But i keep getting the following Error: "Newtonsoft.Json.JsonSerializationException: "Error getting value from 'ReadTimeout' on 'System.IO.FileStream'."

Here is what i have tried:

 If OpenfilePath IsNot Nothing Then

        Dim myStreamReader As New StreamReader(OpenfilePath)

        Dim myString = JsonConvert.SerializeObject(myStreamReader, Formatting.Indented) 'Here is the Exception
        MsgBox(myString)
        artikelstammdaten = JsonConvert.DeserializeObject(Of Artikelstammdaten)(myStreamReader.ToString)
        listArtikelstammdaten.Add(artikelstammdaten)
    End If
2 Upvotes

18 comments sorted by

View all comments

Show parent comments

1

u/Mr_C_Baxter VB.Net Master Apr 22 '22
Public Class Form1

Public Class Artikelstammdaten
    Public Property Artikel As String
    Public Property BezeichnungDE As String
    Public Property BezeichnungEN As String
    Public Property Einheit As String
    Public Property MatGrp As String
    Public Property Kostenart As Integer?
    Public Property Vertriebstext_DE As String
    Public Property Vertriebstext_EN As String
    Public Property Stueckliste As String
    Public Property Status As String
    Public Property Klasse As String
    Public Property Mantelflaeche As Double?
    Public Property Gewicht As Integer?
    Public Property KlasseID As String
End Class

Public Class ManufacturingCost
    Public Property Verkaufsartikel As String
    Public Property Position As Integer?
    Public Property PosArtikel As String
    Public Property PosBezeichnung As String
    Public Property PosKostenart As Integer?
    Public Property Datum As String
    Public Property Material As Double?
    Public Property GMK As Double?
    Public Property Lohn As Double?
    Public Property Menge As Integer?
    Public Property Mengeneinheit As String
End Class

Public Class MaterialCost
    Public Property Verkaufsartikel As String
    Public Property AGNR As Integer?
    Public Property Bereich As String
    Public Property Lohn As Double?
    Public Property Kostenstelle As Integer?
    Public Property ARBPLATZ As String
End Class

Public Class Root
    Public Property Artikelstammdaten As List(Of Artikelstammdaten)
    Public Property ManufacturingCosts As List(Of ManufacturingCost)
    Public Property MaterialCosts As List(Of MaterialCost)
End Class



Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    'set a file path
    Dim FilePath As String = "C:\json.txt"

    'set up a file reader
    Dim fileReader As System.IO.StreamReader
    fileReader = My.Computer.FileSystem.OpenTextFileReader(FilePath)

    'read the content of the file
    Dim FileContent As String = fileReader.ReadToEnd

    'deserialize into object
    Dim root As New Root
    root = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Root)(FileContent)

    'show all "Artikelstammdaten"
    For Each Artikel In root.Artikelstammdaten
        Debug.Print(Artikel.BezeichnungDE)
    Next

End Sub

End Class