Results 1 to 7 of 7

Thread: Newtonsoft 13.0.01 - Invalid Cast Exception

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,154

    Newtonsoft 13.0.01 - Invalid Cast Exception

    I have the following JSON:
    Code:
    {
      "action": "login",
      "model": {
        "Username": "-retracted-",
        "Password": "-retracted-",
        "Domain": "-retracted-"
      }
    }
    And the following POCO:
    Code:
    Option Strict On
    Option Explicit On
    
    Public Class UserViewModel
    
        Private _domain As String
        Public Property Domain As String
            Get
                Return _password
            End Get
            Set(value As String)
                If (_domain <> value) Then
                    _domain = value
                    OnDomainChanged()
                End If
            End Set
        End Property
    
        Private _password As String
        Public Property Password As String
            Get
                Return _password
            End Get
            Set(value As String)
                If (_password <> value) Then
                    _password = value
                    OnPasswordChanged()
                End If
            End Set
        End Property
    
        Private _username As String
        Public Property Username As String
            Get
                Return _username
            End Get
            Set(value As String)
                If (_username <> value) Then
                    _username = value
                    OnUsernameChanged()
                End If
            End Set
        End Property
    
        Protected Overridable Sub OnDomainChanged()
            RaiseEvent DomainChanged(Me, EventArgs.Empty)
        End Sub
    
        Protected Overridable Sub OnPasswordChanged()
            RaiseEvent PasswordChanged(Me, EventArgs.Empty)
        End Sub
    
        Protected Overridable Sub OnUsernameChanged()
            RaiseEvent UsernameChanged(Me, EventArgs.Empty)
        End Sub
    
        Public Event DomainChanged(sender As Object, e As EventArgs)
    
        Public Event PasswordChanged(sender As Object, e As EventArgs)
    
        Public Event UsernameChanged(sender As Object, e As EventArgs)
    
    End Class
    But when I go to convert the model property to my POCO I'm getting an invalid cast exception when calling the .Value(Of T) method using the following:
    Code:
    Dim literal = e.WebMessageAsJson
    Dim conversion = JsonConvert.DeserializeObject(Of JObject)(literal)
    Dim action = conversion.Value(Of String)("action")
    Select Case action
        Case "login"
            Dim model = conversion.Value(Of UserViewModel)("model")
            LoginPost(model)
        Case Else
            ' ...
        End Case
    End Select
    Why is it doing this?

    What is odd is that doing the following yields the expected object:
    Code:
    conversion.GetValue("model")
    I have a feeling I could call ToString on GetValue then call DeserializeObject to get it to work, but I feel like calling the Value method should work just as fine.
    "Code is like humor. When you have to explain it, itís bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109,079

    Re: Newtonsoft 13.0.01 - Invalid Cast Exception

    I think you mean "redacted" rather than "retracted".

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109,079

    Re: Newtonsoft 13.0.01 - Invalid Cast Exception

    Not sure whether this is your real code but there seems to be a mistake here:
    Code:
        Private _domain As String
        Public Property Domain As String
            Get
                Return _password
            End Get
            Set(value As String)
                If (_domain <> value) Then
                    _domain = value
                    OnDomainChanged()
                End If
            End Set
        End Property

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109,079

    Re: Newtonsoft 13.0.01 - Invalid Cast Exception

    This is not wrong:
    vb.net Code:
    1. Public Event DomainChanged(sender As Object, e As EventArgs)
    2.  
    3. Public Event PasswordChanged(sender As Object, e As EventArgs)
    4.  
    5. Public Event UsernameChanged(sender As Object, e As EventArgs)
    but it would be more correct to do this:
    vb.net Code:
    1. Public Event DomainChanged As EventHandler
    2.  
    3. Public Event PasswordChanged As EventHandler
    4.  
    5. Public Event UsernameChanged As EventHandler

  5. #5

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,154

    Re: Newtonsoft 13.0.01 - Invalid Cast Exception

    Quote Originally Posted by jmcilhinney View Post
    I think you mean "redacted" rather than "retracted".
    English may be first language, but that doesn't mean I try to butcher it every chance I get

    Quote Originally Posted by jmcilhinney View Post
    Not sure whether this is your real code but there seems to be a mistake here:
    Code:
        Private _domain As String
        Public Property Domain As String
            Get
                Return _password
            End Get
            Set(value As String)
                If (_domain <> value) Then
                    _domain = value
                    OnDomainChanged()
                End If
            End Set
        End Property
    Yes, that was a typo. I have a snippet uploaded that handles property creation for me, but I don't have it setup on this machine. So I was free-typing it and made a mistake.

    Quote Originally Posted by jmcilhinney View Post
    This is not wrong:
    vb.net Code:
    1. Public Event DomainChanged(sender As Object, e As EventArgs)
    2.  
    3. Public Event PasswordChanged(sender As Object, e As EventArgs)
    4.  
    5. Public Event UsernameChanged(sender As Object, e As EventArgs)
    but it would be more correct to do this:
    vb.net Code:
    1. Public Event DomainChanged As EventHandler
    2.  
    3. Public Event PasswordChanged As EventHandler
    4.  
    5. Public Event UsernameChanged As EventHandler
    I was not aware of that. I'll definitely make that change.

    ----

    However, with all that being said it does not resolve me issue. Check out this sample for a minimal reproducible example: https://dotnetfiddle.net/y6CV65

    I think this might actually be a bug with the library.

    ----

    Edit - it appears that this works:
    Code:
    conversion.Item("model").ToObject(Of UserViewModel)
    But I still don't understand why the Value(Of T) method is not working.
    Last edited by dday9; Oct 4th, 2022 at 11:04 AM.
    "Code is like humor. When you have to explain it, itís bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109,079

    Re: Newtonsoft 13.0.01 - Invalid Cast Exception

    Quote Originally Posted by dday9 View Post
    with all that being said it does not resolve me issue.
    No, I posted those as I noticed them but, when I got to the end, I was unable to address the actual issue. Don't manipulate much JSON myself.

  7. #7
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,263

    Re: Newtonsoft 13.0.01 - Invalid Cast Exception

    It's not a bug, you are trying to deserialize a subset of the Json, I don't know how will this be handled by the converted, so either get rid of the extra Json or deserialize the Json string you get at it's enterty.

    An oversimplified version of your Json will work with this:

    Code:
       Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim json As String = "{
      'action': 'login',
      'model': {
        'Username': '-retracted-',
        'Password': '-retracted-',
        'Domain': '-retracted-'
      }
    }"
            Dim jsonObject = JsonConvert.DeserializeObject(Of MainClass)(json)
    
        End Sub
    
    Public Class Model
        Public Property Username As String
        Public Property Password As String
        Public Property Domain As String
    End Class
    
    Public Class MainClass
        Public Property action As String
        Public Property model As Model
    End Class
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width