Steffen
Steffen

Reputation: 45

Json deserialize issue with tuple

I created a silverlight project for Windows Phone 8 a tuple list with an GeoCoordinate and a short value inside. For doing so I created a TupleList class:

public class TupleList<T1, T2> : List<Tuple<T1, T2>>
{
    public void Add(T1 item, T2 item2)
    {
        Add(new Tuple<T1, T2>(item, item2));
    }
}

and so I am able to create my tuple like this:

new Tuple<GeoCoordinate, short> TupleName;

In next step I want to write in in a txt/json file and this works also fine:

string Json = JsonConvert.SerializeObject(TupleName);
...
System.Text.Encoding.UTF8.GetBytes(Json.ToCharArray());

but now my problem is to load this file again and deserialize it again and i am searching for a solution:

string TestString = streamReader.ReadLine();
Tuple<GeoCoordinate, short> TestTuple;
TestTuple = JsonConvert.DeserializeObject<Tuple<GeoCoordinate, short>>(TestString);
ListBox_WayPoints.Items.Add(TestTuple);

Until the ReadLine() it is working just as expected and I get a string like "Item1: 'GeoCoordinate stuff', Item2: 'short value'" but the JsonConvert.DeserializeObject<Tuple<GeoCoordinate, short>> method is always crashing and I don't know why because the debugger is just jumping to a Debugger Breakpoint and the whole Error Message is:

Ausnahme ausgelöst: "Newtonsoft.Json.JsonSerializationException" in Newtonsoft.Json.DLL
Ausnahme ausgelöst: "Newtonsoft.Json.JsonSerializationException" in mscorlib.ni.dll

So I am searching now for examples/help with this issue (unfortunately i wasn't successful yet) how it is possible to deserialize the string correctly.

Upvotes: 2

Views: 815

Answers (1)

Steffen
Steffen

Reputation: 45

Ok after 2 hours I found the answer :D

the whole issue is that i expected a Tuple BUT what i got was a already a TupleList so the whole magic is:

string TestString = streamReader.ReadLine();
NewRoute = JsonConvert.DeserializeObject<TupleList<GeoCoordinate, short>>(TestString);

Upvotes: 2

Related Questions