Json.Net Compact Framework反序列化性能

时间:2012-09-12 10:37:27

标签: .net vb.net serialization json.net

我正在测试Json.Net的最新版本,我正在服务器上序列化一个集合,并使用WCF代理传递字符串。

服务器调用返回数据的时间不到一秒,但在PDA客户端,反序列化JsonConvert.DeserializeObject(Of IList(Of BusinessUnitModel))(results)需要20-30秒。

这种性能通常与Json.Net有关,还是我对对象或序列化/反序列化做错了?

我已经尝试降级到3.5x版本的Json.Net并在服务器上使用适当的版本和客户端上的Compact版本,但性能再次差。

共享模式

Imports System.ComponentModel.DataAnnotations

Namespace Models

    Public Class BusinessUnitModel
        <Required>
        Public Property ID As Int32
        <Required>
        <StringLength(50)>
        Public Property Name As String
        <Required>
        Public Property IsInternalSupplier As Boolean
    End Class

End Namespace

服务器端序列化

Imports Qsmart.DataProvider.Contracts
Imports System.ComponentModel.Composition
Imports Newtonsoft.Json

Namespace Serialiser

    <Export(GetType(IModelSerialiser))>
    Public Class JSonSerialiser
        Implements IModelSerialiser

        <Import(GetType(ILoggerHelper))>
        Private Property ExHelper As ILoggerHelper

        Friend Function Deserailse(Of TModel)(serialised As String) As TModel Implements IModelSerialiser.Deserailse

            If serialised Is Nothing Then Throw New ArgumentNullException("serialised")

            Try
                Return JsonConvert.DeserializeObject(serialised)
            Catch ex As JsonSerializationException
                ExHelper.Process(ex)
                Throw New ApplicationException("Could Not Deserialse The Model", ex)
            End Try

        End Function

        Friend Function Serailse(Of TModel)(model As TModel) As String Implements IModelSerialiser.Serailse

            If model Is Nothing Then Throw New ArgumentNullException("model")

            Try
                Return JsonConvert.SerializeObject(model)
            Catch ex As JsonSerializationException
                ExHelper.Process(ex)
                Throw New ApplicationException("Could Not Serialse The Model", ex)
            End Try

        End Function

    End Class

End Namespace

服务器端测试数据创建

Public Function QSmartBusinessUnits() As String Implements IQSmartDataProvider.QSmartBusinessUnits

    Dim serialsedResult As String = String.Empty

    Try
        Dim tester As New List(Of BusinessUnitModel)
        tester.Add(New BusinessUnitModel With {.ID = 1, .Name = "Test1", .IsInternalSupplier = True})
        tester.Add(New BusinessUnitModel With {.ID = 2, .Name = "Test2", .IsInternalSupplier = True})
        tester.Add(New BusinessUnitModel With {.ID = 3, .Name = "Test3", .IsInternalSupplier = True})
        tester.Add(New BusinessUnitModel With {.ID = 4, .Name = "Test4", .IsInternalSupplier = True})
        tester.Add(New BusinessUnitModel With {.ID = 5, .Name = "Test5", .IsInternalSupplier = True})
        tester.Add(New BusinessUnitModel With {.ID = 6, .Name = "Test6", .IsInternalSupplier = True})

        serialsedResult = Serialiser.Serailse(tester)

    Catch ex As Exception
        ExHelper.Process(ex)
    End Try

    Return serialsedResult

End Function

紧凑框架反序列化

Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    Try
        Dim results As String = _proxy.QSmartBusinessUnits()
        Dim deserialise = JsonConvert.DeserializeObject(Of IList(Of BusinessUnitModel))(results)
        BusinessUnitModelBindingSource.DataSource = deserialise
    Catch ex As Exception
        Stop
    End Try

End Sub

更新

如果我从模型中删除DataAnnotations,性能会好得多,它会下降到大约2秒......任何想法为什么会这样?

Imports System.ComponentModel.DataAnnotations

    Namespace Models

        Public Class BusinessUnitModel
            Public Property ID As Int32
            Public Property Name As String
            Public Property IsInternalSupplier As Boolean
        End Class

    End Namespace

0 个答案:

没有答案