views:

234

answers:

2

I have a class:

[Serializable]
    public class KVPair<TKey, TValue>
    {
        public TKey Key { get; set; }
        public TValue Value { get; set; }

        public KVPair(TKey k, TValue v)
        {
            Key = k;
            Value = v;
        }
    }    

that I create:

List<KVPair<string,string>> kvPairs;

Using the JSON.Net library I can serialize the list and yield:

"[{\"Key\":\"Two\",\"Value\":\"2\"},{\"Key\":\"One\",\"Value\":\"1\"}]"

When I de-serialize this string back to List> I get the correct count of objects but they are null. Any suggestions would be great help.

+4  A: 

I'm guessing that you might need to add a parameterless constructor:

public KVPair() {
}

Perhaps JSON.net doesn't know how to construct your object so it silently fails.

Eilon
AWESOME!!! Added the new constructor and it worked. You rock!!
David Robbins
A: 

Any chance of you posting a little demo of how you got that working. I am using a List collection of your KVPair class and it is coming up with the null issue. I have included the above constructor.

Many thanks in advanced!! AJ

JonesyBoy