0

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)?
1
  • 1
    Don't use JavaScriptSerializer. It's obsolete. Even ASP.NET Web API and all ASP.NET Core versions up to 2.2 use Json.NET. It's the defacto standard JSON library in .NET. Beyond that, you need to specify the type during deserialization Commented Dec 16, 2019 at 10:40

2 Answers 2

0

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);
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, I didn't want to use Newtonsoft.Json
Then you might have to look into DataContractJsonSerializer, as I am afraid JavaScriptSerializer won't work in this case.
Thnx for the answer
0

Try specifying the exact type when deserializing

JavaScriptSerializer js = new JavaScriptSerializer();
return js.Deserialize<List<Tuple<long, string, int>>>(strObject);

2 Comments

This still suffers from the same problem.
I tried that, I got the same exception. (Thnx for the answer)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.