【发布时间】: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.Text、fastJson 等。但它们对我没有用。例如 ServiceStack.Text 不序列化私有字段,fastJson 在序列化复杂实体方面存在问题,您可以在库的讨论页面上看到...
简单地说,我下载了 Json.net 的源代码来为自己的实体定制库,但我在库中迷路了。 :) 您对我完成此类实体的性能改进有什么建议吗?我只序列化我的实体中的私有字段,所以我认为这可能是一个重点,但我不知道如何开始。
谢谢,
【问题讨论】:
-
“更高效”是什么意思? json.net 何时何地效率不高?抱歉,我看到一个具有三个属性的类。这可能不是模型复杂程度的最佳示例。
-
请问,究竟是什么让您认为需要进行性能优化?也许问题出在其他地方。
-
@Wim Ombelets 我并不是说 json.net 效率不高。我刚刚读到至少在某些情况下 ServiceStack.Text 或 fastJson 工作得更快。而且我想知道是否有办法提高 Json.net 在我的案例中的性能。
-
This 的帖子似乎很有趣,these 和 these benchmarks 也很有趣,似乎表明了 Stefan 的建议:认真研究 Proto-Buf.Net
-
Proto-Buf.Net 是一个二进制序列化器,但我需要 json 作为输出。
标签: c# json serialization json.net deserialization