Last active
August 29, 2015 14:05
-
-
Save trcio/e6df196d4a76343824ee to your computer and use it in GitHub Desktop.
VB.NET version of https://gist.github.com/PatPositron/10076559
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Imports System.Runtime.Serialization.Json | |
Imports System.IO | |
Imports System.Text | |
Public Class JsonUtil | |
''' <summary> | |
''' Serializes an object to the respectable JSON string. | |
''' </summary> | |
Public Shared Function Serialize(Of T)(o As T) As String | |
Dim s = New DataContractJsonSerializer(GetType(T)) | |
Using ms = New MemoryStream() | |
s.WriteObject(ms, o) | |
Return Encoding.UTF8.GetString(ms.ToArray()) | |
End Using | |
End Function | |
''' <summary> | |
''' Deserializes a JSON string to the specified object. | |
''' </summary> | |
Public Shared Function Deserialize(Of T)(json As String) As T | |
Dim s = New DataContractJsonSerializer(GetType(T)) | |
Using ms = New MemoryStream(Encoding.UTF8.GetBytes(json)) | |
Return DirectCast(s.ReadObject(ms), T) | |
End Using | |
End Function | |
End Class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment