c# - How does a Tuple serialize to and deserialize from JSON? -
i curious how tuple<t1, t2, t3, ...> serializes , deserializes. searched using keywords "json" , "tuple" not find want.
i test unittest , json.net, , test codes following. results shows tuple<t1,t2,t3,...> serializable , deserializable. can use them in application.
test codes
public class foo { public list<tuple<string, string, bool>> items { get; set; } public foo() { items = new list<tuple<string, string, bool>>(); } public override string tostring() { stringbuilder sb = new stringbuilder(); foreach (var in items) { sb.append(a.item1 + ", " + a.item2 + ", " + a.item3.tostring() + "\r\n"); } return sb.tostring(); } } [testclass] public class normaltests { [testmethod] public void tupleserialization() { foo tests = new foo(); tests.items.add(tuple.create("one", "hehe", true)); tests.items.add(tuple.create("two", "hoho", false)); tests.items.add(tuple.create("three", "ohoh", true)); string json = jsonconvert.serializeobject(tests); console.writeline(json); var obj = jsonconvert.deserializeobject<foo>(json); string objstr = obj.tostring(); console.writeline(objstr); } } comments
the tuple.create("own","hehe",true) serialize string {"item1":"one","item2":"hehe","item3":true}, , can deserialized tuple<string,string,bool> type.
Comments
Post a Comment