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.
Python tuples are JSON serializable, just like lists or dictionaries. The JSONEncoder class supports the following objects and types by default. The process of converting a tuple (or any other native Python object) to a JSON string is called serialization.
The results shows Tuple<T1,T2,T3,...> is serializable and deserializable.
JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object).
In Deserialization, it does the opposite of Serialization which means it converts JSON string to custom . Net object. In the following code, it creates a JavaScriptSerializer instance and calls Deserialize() by passing JSON data. It returns a custom object (BlogSites) from JSON data.
I test by UnitTest
and 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.
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); } }
Tuple.Create("own","hehe",true)
serializes to {"Item1":"one","Item2":"hehe","Item3":true}
{"Item1":"one","Item2":"hehe","Item3":true}
can be deserialized back to Tuple<string,string, bool>
Class Foo
with Tuple
data, can be serialized to json string, and the string can be deserialized back to Class Foo
.
If you are looking for a short answer. I am using 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.
With .NET5 and soon .NET6 it's now recommended to use System.Text.Json
over NewtonSoft. The important thing for this serializer with regard to tuples is to set the JsonSerializerOptions
option IncludeFields
, as otherwise tuple values are excluded by default.
Further, named tuples are just syntactic sugar which are replaced by standard Item1
, Item2
notation by the compiler. To include names the simplest way is to use an anonymous object.
Below is a minimal example. (can paste into .NET fiddle with the .NET5 compiler)
using System;
using System.Collections.Generic;
using System.Text.Json;
public class Program
{
public static void Main()
{
JsonSerializerOptions options = new() { IncludeFields = true };
var testTuple = ("test" , "test1", 1324, false);
var serializedTuple = JsonSerializer.Serialize(testTuple, options);
Console.WriteLine(serializedTuple);
var testTuple2 = (NamedItem1: "test" , NamedItemTwo: "test1", TheIntegersName: 1324, ThisBoolHasAFirstNameIts: false);
var serializedTuple2 = JsonSerializer.Serialize(new {testTuple2.NamedItem1, testTuple2.NamedItemTwo, testTuple2.TheIntegersName, testTuple2.ThisBoolHasAFirstNameIts }, options);
Console.WriteLine(serializedTuple2);
}
}
output:
{"Item1":"test","Item2":"test1","Item3":1324,"Item4":false}
{"NamedItem1":"test","NamedItemTwo":"test1","TheIntegersName":1324,"ThisBoolHasAFirstNameIts":false}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With