C# 元组如何序列化为 JSON 和反序列化 JSON?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16101115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:00:18  来源:igfitidea点击:

How does a Tuple serialize to and deserialize from JSON?

c#json

提问by AechoLiu

I am curious about how the Tuple<T1, T2, T3, ...>serializes and deserializes. I searched using keywords "json" and "tuple" but I could not find what I want.

我很好奇Tuple<T1, T2, T3, ...>序列化和反序列化的方式。我使用关键字“json”和“tuple”进行搜索,但找不到我想要的。

采纳答案by AechoLiu

I test by UnitTestand Json.net, and the test codes is as following. The results shows Tuple<T1,T2,T3,...>is serializable and deserializable. So I can use them in my application.

我通过UnitTestJson.net测试,测试代码如下。结果显示Tuple<T1,T2,T3,...>可序列化和反序列化。所以我可以在我的应用程序中使用它们。

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 a 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)will be serialize to string {"Item1":"one","Item2":"hehe","Item3":true}, and it can be deserialized back to Tuple<string,string,bool>type.

Tuple.Create("own","hehe",true)将序列化到字符串{“项目1”:“一”,“项目2”:“嘻嘻”,“项目3”:真正},并且它可以被反序列化回Tuple<string,string,bool>类型。

回答by Hinrich

If you are looking for a short answer. I am using JsonConvert.

如果您正在寻找简短的答案。我正在使用JsonConvert

var testTuple = Tuple.Create(1234, "foo", true);
var serialized = JsonConvert.SerializeObject(testTuple);

Console.WriteLine(serialized);
// prints: {"Item1":1234,"Item2":"foo","Item3":true}

I made a minimal fiddle.

我做了一个最小的小提琴

回答by user13230141

Thank you Hinrich to the dotnetfiddle link above.

感谢 Hinrich 到上面的 dotnetfiddle 链接。

i used the same link, and got to know how Conversion works between Json objects and Tuples. Below is the code :

我使用了相同的链接,并了解了 Json 对象和元组之间的转换是如何工作的。下面是代码:

using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

public class Program
{
    public static void Main()
    {

        var testTuple = Tuple.Create<int, string, bool>(1234, "foo", true);
        var serialized = JsonConvert.SerializeObject(testTuple);
        Console.WriteLine(serialized);
        JObject test = ((JObject)JsonConvert.DeserializeObject(serialized));
        string strSerialized = JsonConvert.SerializeObject(test);
        //Tuple<int, string, bool> testTuple1 = JsonConvert.DeserializeObject<Tuple<int, string, bool>>(serialized); // WORKs
        Tuple<int, string, bool> testTuple1 = JsonConvert.DeserializeObject<Tuple<int, string, bool>>(strSerialized); // WORKs
        Console.WriteLine(testTuple1.Item1.ToString());
    }
}

Hope someone finds this helpful.

希望有人觉得这有帮助。