Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.net anonymous type has incorrect property casing from AJAX call

Tags:

json

ajax

vb.net

We have noticed that sometimes from the results of an AJAX call to a controller action that the case of the JSON result is incorrect. The returned case will actually change if we rebuild our solution and try the exact same call. In the following case, the key's case has been correct for over a year until now when it has decided to start randomly changing depending on some seemingly random circumstances.

Casing issue

As you can see in the picture above, the key for the JSON result is lowercase "success". However when I view the results in Chrome's console, it is an uppercase "Success". This is causing our JavaScript to fail since it is checking for the lowercase version.

What is causing this? And more importantly, how do we stop this?

like image 857
Jason Kaczmarsky Avatar asked Jan 14 '15 16:01

Jason Kaczmarsky


1 Answers

vb.net is case-insensitive as opposed to C# which is case-sensitive. This means that the compiler will generate only one class (from the first instance) for each of the following anonymous types:

Dim a = New With {.success = True} 'Compiler generate a class based on this type
Dim b = New With {.Success = True} 'Same type as `a`
Dim c = New With {.sUcCeSs = True} 'Same type as `a`

Debug.WriteLine(a.GetType().Name)
Debug.WriteLine(b.GetType().Name)
Debug.WriteLine(c.GetType().Name)

VB$AnonymousType_0'1
VB$AnonymousType_0'1
VB$AnonymousType_0'1

Here's how the complied code looks like when compiled back to vb.net:

<DebuggerDisplay("success={success}"), CompilerGenerated> _
Friend NotInheritable Class VB$AnonymousType_0(Of T0)
    ' Methods
    <DebuggerNonUserCode> _
    Public Sub New(ByVal success As T0)
        Me.$success = success
    End Sub

    <DebuggerNonUserCode> _
    Public Overrides Function ToString() As String
        Dim builder As New StringBuilder
        builder.Append("{ ")
        builder.AppendFormat("{0} = {1} ", "success", Me.$success)
        builder.Append("}")
        Return builder.ToString
    End Function

    Public Property success As T0
        <DebuggerNonUserCode> _
        Get
            Return Me.$success
        End Get
        <DebuggerNonUserCode> _
        Set(ByVal Value As T0)
            Me.$success = Value
        End Set
    End Property

    Private $success As T0

End Class
like image 87
Bjørn-Roger Kringsjå Avatar answered Oct 23 '22 08:10

Bjørn-Roger Kringsjå