Guest User

Untitled

a guest
Jul 19th, 2018
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. public class Foo {
  2. public List<Tuple<string, string, bool>> Items { get; set; }
  3.  
  4. public Foo()
  5. {
  6. Items = new List<Tuple<string, string, bool>>();
  7. }
  8.  
  9. public override string ToString()
  10. {
  11. StringBuilder sb = new StringBuilder();
  12. foreach (var a in Items)
  13. {
  14. sb.Append(a.Item1 + ", " + a.Item2 + ", " + a.Item3.ToString() + "rn");
  15. }
  16. return sb.ToString();
  17. }
  18. }
  19.  
  20. [TestClass]
  21. public class NormalTests
  22. {
  23. [TestMethod]
  24. public void TupleSerialization()
  25. {
  26. Foo tests = new Foo();
  27. tests.Items.Add(Tuple.Create("one", "hehe", true));
  28. tests.Items.Add(Tuple.Create("two", "hoho", false));
  29. tests.Items.Add(Tuple.Create("three", "ohoh", true));
  30.  
  31. string json = JsonConvert.SerializeObject(tests);
  32. Console.WriteLine(json);
  33.  
  34. var obj = JsonConvert.DeserializeObject<Foo>(json);
  35. string objStr = obj.ToString();
  36. Console.WriteLine(objStr);
  37. }
  38. }
  39.  
  40. var testTuple = Tuple.Create(1234, "foo", true);
  41. var serialized = JsonConvert.SerializeObject(testTuple);
  42.  
  43. Console.WriteLine(serialized);
  44. // prints: {"Item1":1234,"Item2":"foo","Item3":true}
Add Comment
Please, Sign In to add comment