【问题标题】:How to improve performance of Json.net Serializer for my own model如何为我自己的模型提高 Json.net Serializer 的性能
【发布时间】:2013-01-08 13:42:33
【问题描述】:

我有一个由大型复杂类组成的数据模型。在这里,您可以在以下示例中看到我的类的一般结构(我的模型中最小的类之一):

[DataContract(IsReference = true)]
public partial class Name : PresentationBase
{
    [DataMember]
    private SmartProperty<string> _first;
    public SmartProperty<string> First
    {
        get { return this._first ?? (this._first = new SmartProperty<string>()); }
        set
        {
            bool isModified = false;

            if (this._first == null)
            {
                if (value != null)
                {
                    isModified = true;
                }
            }
            else if (value == null)
            {
                isModified = true;
            }
            else if (this._first.UnderlyingValue == null)
            {
                if (value.UnderlyingValue != null)
                {
                    isModified = true;
                }
            }
            else if (value.UnderlyingValue == null)
            {
                isModified = true;
            }
            else if (!value.UnderlyingValue.Equals(this._first.UnderlyingValue))
            {
                isModified = true;
            }

            this._first = value;

            if (isModified)
            {
                this._first.IsModified = isModified;
                this.OnPropertyChanged("First");
            }
        }
    }

    [DataMember]
    private SmartProperty<string> _last;
    public SmartProperty<string> Last
    {
        get { return this._last ?? (this._last = new SmartProperty<string>()); }
        set
        {
            bool isModified = false;

            if (this._last == null)
            {
                if (value != null)
                {
                    isModified = true;
                }
            }
            else if (value == null)
            {
                isModified = true;
            }
            else if (this._last.UnderlyingValue == null)
            {
                if (value.UnderlyingValue != null)
                {
                    isModified = true;
                }
            }
            else if (value.UnderlyingValue == null)
            {
                isModified = true;
            }
            else if (!value.UnderlyingValue.Equals(this._last.UnderlyingValue))
            {
                isModified = true;
            }

            this._last = value;

            if (isModified)
            {
                this._last.IsModified = isModified;
                this.OnPropertyChanged("Last");
            }
        }
    }

    [DataMember]
    private SmartProperty<string> _maiden;
    public SmartProperty<string> Maiden
    {
        get { return this._maiden ?? (this._maiden = new SmartProperty<string>()); }
        set
        {
            bool isModified = false;

            if (this._maiden == null)
            {
                if (value != null)
                {
                    isModified = true;
                }
            }
            else if (value == null)
            {
                isModified = true;
            }
            else if (this._maiden.UnderlyingValue == null)
            {
                if (value.UnderlyingValue != null)
                {
                    isModified = true;
                }
            }
            else if (value.UnderlyingValue == null)
            {
                isModified = true;
            }
            else if (!value.UnderlyingValue.Equals(this._maiden.UnderlyingValue))
            {
                isModified = true;
            }

            this._maiden = value;

            if (isModified)
            {
                this._maiden.IsModified = isModified;
                this.OnPropertyChanged("Maiden");
            }
        }
    }
}

我使用Json.net 进行序列化/反序列化操作。 由于我的模型很复杂,我想知道是否可以个性化 Json.net 库以更有效地处理我的实体。无法修改我的模型,因为它确实是一个大项目,任何更改都会造成很大的影响。

我尝试了除 Json.net 之外的一些 Json 序列化库,包括 ServiceStack.TextfastJson 等。但它们对我没有用。例如 ServiceStack.Text 不序列化私有字段,fastJson 在序列化复杂实体方面存在问题,您可以在库的讨论页面上看到...

简单地说,我下载了 Json.net 的源代码来为自己的实体定制库,但我在库中迷路了。 :) 您对我完成此类实体的性能改进有什么建议吗?我只序列化我的实体中的私有字段,所以我认为这可能是一个重点,但我不知道如何开始。

谢谢,

【问题讨论】:

  • “更高效”是什么意思? json.net 何时何地效率不高?抱歉,我看到一个具有三个属性的类。这可能不是模型复杂程度的最佳示例。
  • 请问,究竟是什么让您认为需要进行性能优化?也许问题出在其他地方。
  • @Wim Ombelets 我并不是说 json.net 效率不高。我刚刚读到至少在某些情况下 ServiceStack.Text 或 fastJson 工作得更快。而且我想知道是否有办法提高 Json.net 在我的案例中的性能。
  • This 的帖子似乎很有趣,thesethese benchmarks 也很有趣,似乎表明了 Stefan 的建议:认真研究 Proto-Buf.Net
  • Proto-Buf.Net 是一个二进制序列化器,但我需要 json 作为输出。

标签: c# json serialization json.net deserialization


【解决方案1】:

查看您的代码,我想知道问题出在实际的序列化/反序列化上,还是出在您正在使用的大量 if-then-else 构造和 onpropertychanged 上。我会首先尝试使用布尔代数而不是这些 if-then-else 结构来优化它……或者尝试将它们全部删除。

例如isModified = (值 == null) || (value.UnderlyingValue == null) || (_maiden==null&&value==null) || [...]

您还可以引入新的(公共)属性而不是私有属性,并使用 [JsonIgnore] 标记现有属性。

最后,如果您不关心 JSON 或其他内容,我会考虑查看 protobuf-net。

【讨论】:

  • 我需要 json 作为输出,但是 Protobuf-net 是一个二进制序列化器。
猜你喜欢
  • 1970-01-01
  • 2017-01-05
  • 1970-01-01
  • 2019-04-24
  • 2018-09-20
  • 2019-11-27
  • 1970-01-01
  • 2021-06-06
  • 1970-01-01
相关资源
最近更新 更多