TheSharperDev

Posts about C# and F#

How Different C# Data Structures Serialize to Json

My latest project is a Json Serializer/Deserializer (Ferris.Json).

What does a Dictionary, LinkedList or SortedSet look like when serialized to Json?

I didn’t really know, but explored how existing libraries serialized the following data structures.

TLDR - The List

Every data structure I tested falls into one of two groups:

  • ["one","two","three"]
    • The following datastructures all serialize to the above.
    • List<string>
    • HashSet<string>
    • Queue<string>
    • Stack<string>
    • LinkedList<string>
    • SortedSet<string>
  • {"one":1,"two":2,"three":3}
    • The following datastructures all serialize to the above.
    • Dictionary<string, int>
    • SortedDictionary<string, int>
    • SortedList<string, int>

Both Newtonsoft and System.Text.Json follow the above conventions. So I will be doing the same.