I have serialized a List<Tuple<long, string, int>> obj
using JavaScriptSerializer.
JavaScriptSerializer js = new JavaScriptSerializer();
return js.Serialize(dataToSerialize as IEnumerable);
and I got the string:
[{"Item1":0,"Item2":"UserModifiedId","Item3":1},{"Item1":-1,"Item2":"","Item3":0},{"Item1":-1,"Item2":"","Item3":0}]
When I try to deserialize it using:
JavaScriptSerializer js = new JavaScriptSerializer();
return js.Deserialize<List<T>>(strObject);
I get the following ex:
No parameterless constructor defined for type of 'System.Tuple`3[[System.Int64, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'.```
Can anyone help me with this Issue (to keep using JavaScriptSerializer)?
The JavaScriptSerializer
needs a parameterless constructor to create the objects during deserialization.
Because the class Tuple
has none, the deserialization fails.
If you don't bother you can use the NuGet
package Newtonsoft.Json
, which doesn't have his problem:
var strObject= JsonConvert.SerializeObject(dataToSerialize);
var tupleList = JsonConvert.DeserializeObject<List<Tuple<long, string, int>>>(strObject);