Json.net缓慢的序列化和反序列化 [英] Json.net slow serialization and deserialization

查看:143
本文介绍了Json.net缓慢的序列化和反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题-Json.Net序列化对象真的很慢.我有一些基础课:

I have a problem - Json.Net serializing my objects realy slow. I have some basic class:

public class authenticationRequest
{
    public string userid;
    public string tid;
    public string token;
    public string platform;
    public string version;
}

我正在用它序列化

string jsonDataToSend = JsonConvert.SerializeObject(dataToSend); 

此操作大约需要1900毫秒.与 Json.net CodePlex 页中的信息相比:

This operation takes about 1900 ms. In compare to info from Json.net CodePlex page:

这需要很长时间.为了进行测试,我将类替换为简单的字符串:

It takes a really long time. For test purposes I swapped my class for a simple string:

string jsonDataToSend = JsonConvert.SerializeObject("fsdfsdfsdfs");

转换仍然需要约900毫秒.是什么原因?我可以做些什么来更快地序列化这些数据?

And it still takes ~900 ms to convert. What is the reason? What I can do to serialize this data faster?

推荐答案

我相信这里正在发生的事情是,当加载Json.Net库时,您会遇到延迟.您应该尝试在发布"模式下进行编译,以查看速度是否大大加快,因为这会阻止符号的加载(这可能会增加库的加载时间).

What I believe is happening here is that you are getting a delay when the Json.Net library is being loaded. You should try compiling in Release mode to see if things speed up considerably, as this should prevent symbols from being loaded (which can add to the library load time).

如果仍然存在问题,请在您的应用中找到一个不错的时间,您可以执行虚拟序列化(甚至在后台线程上)以强制加载库.但是,这有一些代码气味,因此也许有更好的方法来强制加载,但这是蛮力方法,应该一直有效.

If it is still an issue, find a good time in your app that you can do a dummy serialization (perhaps even on a background thread) to force the library to load. That has a bit of code-smell to it, though, so there may be a better way of forcing the load, but that's a brute force method that should work all the time.

这篇关于Json.net缓慢的序列化和反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆