Skip to content

Instantly share code, notes, and snippets.


richlander/Program.cs Secret

Created Sep 11, 2020
What would you like to do?
`System.Text.Json` now supports Dictionaries with non-string keys
<Project Sdk="Microsoft.NET.Sdk">
using System;
using System.Collections.Generic;
using System.Text.Json;
Dictionary<int, string> numbers = new ()
{0, "zero"},
{1, "one"},
{2, "two"},
{3, "three"},
{5, "five"},
{8, "eight"},
{13, "thirteen"},
{21, "twenty one"},
{34, "thirty four"},
{55, "fifty five"},
var json = JsonSerializer.Serialize<Dictionary<int, string>>(numbers);
var dictionary = JsonSerializer.Deserialize<Dictionary<int, string>>(json);

This comment has been minimized.

Copy link
Owner Author

@richlander richlander commented Sep 11, 2020

Produces the following output.

rich@thundera jsondictionarykeys % dotnet run
{"0":"zero","1":"one","2":"two","3":"three","5":"five","8":"eight","13":"thirteen","21":"twenty one","34":"thirty four","55":"fifty five"}
fifty five
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment