Skip to content

Instantly share code, notes, and snippets.

@trcio
Last active August 29, 2015 14:05
Show Gist options
  • Save trcio/e6df196d4a76343824ee to your computer and use it in GitHub Desktop.
Save trcio/e6df196d4a76343824ee to your computer and use it in GitHub Desktop.
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