Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does a Tuple serialize to and deserialize from JSON?

Tags:

json

c#

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.

like image 421
AechoLiu Avatar asked Apr 19 '13 09:04

AechoLiu


People also ask

Are tuples JSON serializable?

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.

Are tuples serializable?

The results shows Tuple<T1,T2,T3,...> is serializable and deserializable.

What is serialize and deserialize in JSON?

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).

How does JSON deserialization work?

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.


3 Answers

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.

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);     } } 

Summary

  • 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.

like image 153
AechoLiu Avatar answered Sep 25 '22 21:09

AechoLiu


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.

like image 44
Hinrich Avatar answered Sep 24 '22 21:09

Hinrich


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}

like image 21
TheAtomicOption Avatar answered Sep 21 '22 21:09

TheAtomicOption