0

I have a data model consisting of large complex classes. Here you can see the general structure of my classes in following (one of the smallest classes of my model) example:

[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");
            }
        }
    }
}

I'm using Json.net for serialization / deserialization operations. Because of the complexity of my model I'm wondering if is it possible to personalize the Json.net library to work more efficiently with my entities. It's not possible to revise my model because it's really a big project and any change will cause a big impact.

I tried a few Json serialization libraries other than Json.net including ServiceStack.Text, fastJson etc. But they were not useful for me. For example ServiceStack.Text doesn't serialize private fields, fastJson has problems on serializing complex entities as you can see on discussion page of the library...

Briefly, I downloaded the source code of Json.net to customize the library for my own entities but I got lost in the library. :) Do you have any suggestions for me to accomplish a performance improvement for this kind of entities? I'm serializing only private fields in my entities, so I think this could be a point but I don't know how to start.

Thanks,

xkcd
  • 2,538
  • 11
  • 59
  • 96
  • what do you mean with "more efficiently"? Where or when isn't json.net efficient? I'm sorry but I see a class with three properties. This might not have been the best example of how complex your model is. – Wim Ombelets Jan 08 '13 at 13:56
  • May I ask, what exactly makes you think that there is a need for performance optimization? Maybe the problem lies somewhere else. – Alex Filipovici Jan 08 '13 at 13:56
  • @Wim Ombelets I'm not saying json.net is not efficient. I just read that at least in some cases ServiceStack.Text or fastJson works faster. And I'm wondering if there is a way to improve Json.net's performance for my case. – xkcd Jan 08 '13 at 14:13
  • [This](http://stackoverflow.com/questions/626766/fastest-serializer-and-deserializer-with-lowest-memory-footprint-in-c) post seems interesting, so do [these](http://techmikael.blogspot.be/2010/01/net-serialization-performance.html) and [these benchmarks](http://www.servicestack.net/benchmarks/NorthwindDatabaseRowsSerialization.100000-times.2010-08-17.html) and seem to indicate what Stefan suggests: to take a serious look at Proto-Buf.Net – Wim Ombelets Jan 08 '13 at 14:32
  • Proto-Buf.Net is a binary serializer, but I need json as output. – xkcd Jan 09 '13 at 13:10

1 Answers1

1

Looking at your code I'm wondering if the problem lies in the actual serialization / deserialization or in the large number of if-then-else constructions and onpropertychanged you're using. I would first try to optimize that using boolean algebra rather than these if-then-else constructions... or try to remove them alltogether.

e.g. isModified = (value == null) || (value.UnderlyingValue == null) || (_maiden==null&&value==null) || [...]

You can also introduce new (public) properties instead of your private ones and mark the existing properties with [JsonIgnore].

Last if you don't really care about JSON or something else, I would consider looking at protobuf-net.

atlaste
  • 30,418
  • 3
  • 57
  • 87