2013-01-08 29 views
0

我有一個數據模型組成的大型複雜類。在這裏你可以看到我的課的總體結構如下(最小的班級我的模型中的一個),例如:如何提高Json.net序列化程序的性能爲我自己的模型

[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.Text,fastJson等,但它們對我沒有用處。例如ServiceStack.Text不會序列化專用字段,FastJson在序列化複雜實體時會出現問題,您可以在庫的討論頁面上看到...

簡而言之,我下載了Json.net的源代碼以自定義圖書館爲我自己的實體,但我迷失在圖書館。 :)你對我有什麼建議來完成這類實體的性能改進嗎?我只在我的實體中序列化私有字段,所以我認爲這可能是重點,但我不知道如何開始。

感謝,

+0

「更高效」是什麼意思?何時何地不是json.net高效?我很抱歉,但我看到有三個屬性的課程。這可能不是你的模型有多複雜的最好例子。 –

+0

我可以問一下,究竟是什麼讓你認爲需要性能優化?也許問題在別的地方。 –

+0

@Wim Ombelets我不是說json.net效率不高。我剛剛讀到,至少在某些情況下,ServiceStack.Text或fastJson的工作速度更快。我想知道是否有一種方法可以提高Json.net的性能。 – anilca

回答

1

看你的代碼,我想知道如果問題出在實際的序列化/反序列化或在大量的if-then-else的結構和onpropertychanged你使用。我首先會嘗試使用布爾代數而不是這些if-then-else構造來優化它,或嘗試將它們全部刪除。

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

您還可以引入新的(公共)屬性而不是私有屬性,並用[JsonIgnore]標記現有屬性。

如果你真的不關心JSON或其他什麼的話,我會考慮看protobuf-net。

+0

我需要json作爲輸出,但Protobuf-net是一個二進制串行器。 – anilca