-
Oct 3rd, 2022, 10:06 PM
#1
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.
-
Oct 3rd, 2022, 11:51 PM
#2
Re: Newtonsoft 13.0.01 - Invalid Cast Exception
I think you mean "redacted" rather than "retracted".
-
Oct 3rd, 2022, 11:53 PM
#3
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
-
Oct 3rd, 2022, 11:55 PM
#4
Re: Newtonsoft 13.0.01 - Invalid Cast Exception
This is not wrong:
vb.net Code:
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)
but it would be more correct to do this:
vb.net Code:
Public Event DomainChanged As EventHandler
Public Event PasswordChanged As EventHandler
Public Event UsernameChanged As EventHandler
-
Oct 4th, 2022, 10:53 AM
#5
Re: Newtonsoft 13.0.01 - Invalid Cast Exception
Originally Posted by jmcilhinney
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
Originally Posted by jmcilhinney
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.
Originally Posted by jmcilhinney
This is not wrong:
vb.net Code:
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)
but it would be more correct to do this:
vb.net Code:
Public Event DomainChanged As EventHandler
Public Event PasswordChanged As EventHandler
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.
-
Oct 4th, 2022, 07:35 PM
#6
Re: Newtonsoft 13.0.01 - Invalid Cast Exception
Originally Posted by dday9
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.
-
Oct 5th, 2022, 02:53 AM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|