0

I have a wrapper class to help deserialize incoming json messages. At the moment I am using it like this.

Dim oXMessage As XMessage = JsonConvert.DeserializeObject(Of XMessage)(message)

I wanted to create a constructor for the class so I can instantiate it more cleanly. Like this

Dim oXMessage as XMessage = New XMessage(message)        

I have tried the below constructor but it does not work. Can someone point me in the right direction please?

Public Class XMessage
    <JsonProperty("e")> Public MessageType As String
    <JsonProperty("data")> Public MessageData As Object
    <JsonProperty("ok")> Public MessageOk As String

    Public Sub New(message As String)
        Me = JsonConvert.DeserializeObject(Of Me)(message)
    End Sub
End Class
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88

2 Answers2

0

The Me keyword refers to the current instance and cannot be the target of assignment.

Instead what you may want to do is to create a new object, deserialize it, and copy the new objects member variables to the current instance.

Edit: without the helper functions

Public Class XMessage

    <JsonProperty("e")> Public MessageType As String
    <JsonProperty("data")> Public MessageData As Object
    <JsonProperty("ok")> Public MessageOk As String

    Public Sub New(message As String)
        Dim other = JsonConvert.DeserializeObject(Of XMessage)(message)
        Me.MessageType = other.MessageType
        Me.MessageData = other.MessageData
        Me.MessageOk = other.MessageOk
    End Sub

End Class

Original:

Public Class XMessage

    <JsonProperty("e")> Public MessageType As String
    <JsonProperty("data")> Public MessageData As Object
    <JsonProperty("ok")> Public MessageOk As String


    Public Sub New(message As String)
        Dim other as XMessage= Deserialize(message)
        Me.MessageType = other.MessageType
        Me.MessageData = other.MessageData
        Me.MessageOk = other.MessageOk
    End Sub


    Public shared Function Deserialize(message as String) as XMessage
        return JsonConvert.DeserializeObject(Of XMessage)(message)
    End Function

    Public Shared Function Serialize(message As XMessage) As String 
        return JsonConvert.SerializeObject(message)
    End Function

End Class
Stokke
  • 1,871
  • 2
  • 13
  • 18
  • I was hoping there was a clever syntax that would avoid the creation of another instance. This solution works perfectly though - thanks. – Lance Tyler Nov 11 '17 at 10:25
  • Actually now I test it properly, I can see that this approach creates an infinite recursion. – Lance Tyler Nov 11 '17 at 12:03
  • There should be no recursion here if you copy the code as-is. – Stokke Nov 11 '17 at 12:15
  • added a simplified version without the helper methods – Stokke Nov 11 '17 at 12:24
  • thanks for your time, maybe I am describing it wrong. When I create the objext with "{""e"":""connected""}" and step through. 1.The New is called with the message="{""e"":""connected""}". 2. Then the Dim line is executed. 3. next the New sub is called again with message=nothing. 4. the Dim line is executed and errors due to message=nothing. – Lance Tyler Nov 12 '17 at 00:18
  • just realised that an overload New() will solve that - sorry I'm a noob. – Lance Tyler Nov 12 '17 at 00:52
0

You cannot reassign the value of Me in the constructor since that is the object currently being constructed. But you can call JsonConvert.PopulateObject() to initialize its contents:

Public Class XMessage
    <JsonProperty("e")> Public MessageType As String
    <JsonProperty("data")> Public MessageData As Object
    <JsonProperty("ok")> Public MessageOk As String

    Public Sub New(message As String)
        JsonConvert.PopulateObject(message, Me)
    End Sub

    Public Sub New()
    End Sub
End Class

Sample fiddle.

dbc
  • 104,963
  • 20
  • 228
  • 340