Subversion Repository Public Repository

Nextrek

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
´╗┐Imports System.Runtime.CompilerServices

Module Estensioni

    <Extension()> Public Function ToJSON(Of T As Class)(ByVal obj As T) As String
        Dim serializer As System.Runtime.Serialization.Json.DataContractJsonSerializer = New System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType())
        Using stream As System.IO.MemoryStream = New System.IO.MemoryStream()
            serializer.WriteObject(stream, obj)
            Return (System.Text.Encoding.UTF8.GetString(stream.ToArray))
        End Using
    End Function

    <Extension()> Public Function FromJSON(Of T As Class)(ByVal obj As T, ByVal json As String) As T
        Using stream As System.IO.MemoryStream = New System.IO.MemoryStream(System.Text.Encoding.Unicode.GetBytes(json))
            Dim serializer As System.Runtime.Serialization.Json.DataContractJsonSerializer = New System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType())
            Return DirectCast(serializer.ReadObject(stream), T)
        End Using
    End Function

End Module

Commits for Nextrek/minstrek/GameEditorBKP - Copia/GameEditor/Estensioni.vb

Diff revisions: vs.
Revision Author Commited Message
58 Diff Diff DTocci picture DTocci Thu 19 Dec, 2013 17:36:41 +0000
53 DTocci picture DTocci Thu 12 Dec, 2013 10:51:17 +0000