Imports System.IO
Imports Newtonsoft.Json
Imports Pilz.GeneralEventArgs
Public Module ConfigurationSerializer
Public Event GettingJsonSerializer(instance As Object, e As GetValueEventArgs(Of JsonSerializer))
Private Function GetJsonSerializer(instance As SimpleConfiguration) As JsonSerializer
Dim args As New GetValueEventArgs(Of JsonSerializer)(JsonSerializer.CreateDefault)
RaiseEvent GettingJsonSerializer(instance, args)
Return args.Value
End Function
'''
''' Writes the given instance to a string and return it.
'''
''' The configuration instance that should be serialized.
''' The content of the configuration instance as string.
Public Function WriteToString(instance As SimpleConfiguration) As String
Dim tw As New StringWriter
GetJsonSerializer(instance).Serialize(tw, instance)
Dim txt = tw.ToString
tw.Close()
Return txt
End Function
'''
''' Write the given instance to a given stream.
'''
''' The configuration instance that should be serialized.
''' The stream where the content should be written to.
Public Sub WriteToStream(instance As SimpleConfiguration, stream As Stream)
Dim sr As New StreamWriter(stream)
sr.Write(WriteToString(instance))
End Sub
'''
''' Writes the given instance to the given filePath as text file.
'''
''' The configuration instance that should be serialized.
''' The file path where the content should be written to. The file will be created or overwritten.
Public Sub WriteToFile(instance As SimpleConfiguration, filePath As String)
Dim fs As New FileStream(filePath, FileMode.Create, FileAccess.ReadWrite)
WriteToStream(instance, fs)
fs.Close()
End Sub
'''
''' Reads a configuratin from the given string and returns an instance of it.
'''
''' The type of the configuration class to instance.
''' The content of the configuration as string.
'''
Public Function ReadFromString(Of T As SimpleConfiguration)(content As String) As T
Dim sr As New StringReader(content)
Dim instance As T = GetJsonSerializer(Nothing).Deserialize(sr, GetType(T))
sr.Close()
Return instance
End Function
'''
''' Read a configuration from the given string and put them to the given instance.
'''
''' The instance to populate with the configuration.
''' The content of the configuration as string.
Public Sub ReadFromString(instance As SimpleConfiguration, content As String)
Dim sr As New StringReader(content)
GetJsonSerializer(Nothing).Populate(sr, content)
sr.Close()
End Sub
'''
''' Reads a configuratin from the given string and returns an instance of it.
'''
''' The type of the configuration class to instance.
''' The stream with the content of the configuration.
'''
Public Function ReadFromStream(Of T As SimpleConfiguration)(stream As Stream) As T
Return ReadFromString(Of T)(GetContentOfStream(stream))
End Function
'''
''' Read a configuration from the given string and put them to the given instance.
'''
''' The instance to populate with the configuration.
''' The stream with the content of the configuration.
Public Sub ReadFromStream(instance As SimpleConfiguration, stream As Stream)
ReadFromString(instance, GetContentOfStream(stream))
End Sub
'''
''' Reads a configuratin from the given string and returns an instance of it.
'''
''' The type of the configuration class to instance.
''' The path to the file with the content of the configuration.
'''
Public Function ReadFromFile(Of T As SimpleConfiguration)(filePath As String) As T
Return ReadFromString(Of T)(GetContentOfFile(filePath))
End Function
'''
''' Read a configuration from the given string and put them to the given instance.
'''
''' The instance to populate with the configuration.
''' The path to the file with the content of the configuration.
Public Sub ReadFromFile(instance As SimpleConfiguration, filePath As String)
ReadFromString(instance, GetContentOfFile(filePath))
End Sub
Private Function GetContentOfStream(stream As Stream) As String
Dim sr As New StreamReader(stream)
Return sr.ReadToEnd
End Function
Private Function GetContentOfFile(filePath As String) As String
Dim fs As New FileStream(filePath, FileMode.Open, FileAccess.Read)
Dim content = GetContentOfStream(fs)
fs.Close()
Return content
End Function
End Module