jsonvb.netobjectserializationdatacontractjsonserializer

VB.NET DataContractJsonSerializer Nested Object Formatting


I have a problem with the outcome of a serialization. The class i serialize looks like this:

 <Runtime.Serialization.DataContract()>
    Public Class ResponseJSON
        <Runtime.Serialization.DataMember(Name:="success")>
        Public Property Success As Boolean

        <Runtime.Serialization.DataMember(Name:="message")>
        Public Property Message As String

        <Runtime.Serialization.DataMember(Name:="data", isRequired:=False)>
        Public Property Data As Object
           

        Public Sub New()
        End Sub

        Public Sub New(success As Boolean)
            Me.Success = success
        End Sub

        Public Sub New(message As String)
            Me.Message = message
        End Sub

        Public Sub New(message As String, success As Boolean)
            Me.Message = message
            Me.Success = success
        End Sub

    End Class

The Data-Property can be anything and in my case it is a List(Of Dictionary(Of String, Object)).

The issuse: If i put the List into the property and serialize it the outcome is something like:

{"success":true,"message":"",data:[[{"key":"name", value:1}, {"key":"foo", value:"bar"}],[{"key":"foo", value:"bar"}]]}

You can see that every dictionary in the List is serialized as an array of { "key":"foo", "value":"bar" } and that is strange because if i serialize the pure List (not nested in the ResponseJSON class) it will serialize as { "foo":"bar" } which is what i always want

Those are the settings for the serializer:

mvoSettings = New Runtime.Serialization.Json.DataContractJsonSerializerSettings
mvoSettings.SerializeReadOnlyTypes = True
mvoSettings.MaxItemsInObjectGraph = Integer.MaxValue / 2
mvoSettings.EmitTypeInformation = Runtime.Serialization.EmitTypeInformation.Never
mvoSettings.UseSimpleDictionaryFormat = True
mvoSettings.IgnoreExtensionDataObject = True
mvoSettings.DateTimeFormat = New Runtime.Serialization.DateTimeFormat("dd.MM.yyyy HH:mm:ss")

This is the serialization:

Public Function ToJSON(obj As Object) As IO.MemoryStream
   Dim serializer As New Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType(), mvoSettings)
   Dim stream As New IO.MemoryStream

   serializer.WriteObject(stream, obj)

   Return stream
End Function

Wanted Result:

{"success":true,"message":"",data:[{"foo":"bar", "name":"Jon"},{"Jon":"Doe"}]}

Solution

  • I think i know the Problem by now... i changed the type of the Data-Property from Object to List(Of Dictionary(Of String, Object)) and it worked. I assume that the serializer needs explicit declaration to serialize as wanted.

    Here are the changes:

    1. Removed Data-Property from ResponseJSON
        <Runtime.Serialization.DataContract()>
        Public Class ResponseJSON
            <Runtime.Serialization.DataMember(Name:="success")>
            Public Property Success As Boolean
    
            <Runtime.Serialization.DataMember(Name:="message")>
            Public Property Message As String
        End Class
    
    1. Created derived class with Generic Data-Property
    <Runtime.Serialization.DataContract()>
        Public Class ResponseJSON(Of T)
            Inherits ResponseJSON
    
            <Runtime.Serialization.DataMember(Name:="data", isRequired:=False)>
            Public Property Data As T
        End Class