Skip to content

arogozine/TupleAsJsonArray

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TupleAsJsonArray

Convert C# Tuple to/from JSON Array

Nuget Nuget

About

  1. Serialize Tuple and ValueTuples to JSON Array
  2. Deserialize JSON Arrays as Tuples and ValueTuples
  3. Bridges the gap between JS Destructuring assignment and C# ValueTuples.

Using

.NET C#

// 1. Import Library
using TupleAsJsonArray;

// 2. Set Up Json Serializer Options
var options = new JsonSerializerOptions
{
    Converters =
    {
        new TupleConverterFactory(),
    }
};

// 3. Serialize Tuples to Array
var jsonArray = JsonSerializer.Serialize((1, 2, 3, 4), options);

// 4. Deserialize Arrays to Tuples
var (a, b, c, d) = JsonSerializer.Deserialize<(int, int, int, int)>("[1, 2, 3, 4]", options);

JavaScript

// Tuples in Models will now show up as arrays
// that you can destructure
const [a, b, c, d] = modelFromServer.TupleParam;