CSharp · Console
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters;
using System.IO;
using System.Data;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Reflection;
using System.Collections.ObjectModel;
using System.Text.Json;
using System.Text.Json.Serialization;
using NUnit.Framework; // Throws NUnit.Framework.AssertionException
//https://stackoverflow.com/questions/76937386/c-sharp-polymorphic-deserialization-of-bookmarks-file
#nullable enable
public abstract class BookmarkElement
{
public BookmarkElement() {}
[JsonPropertyName("date_added")]
public string DateAdded { get; set; }
[JsonPropertyName("date_last_used")]
public string DateLastUsed { get; set; }
[JsonPropertyName("guid")]
public string Guid { get; set; }
[JsonPropertyName("id")]
public string Id { get; set; }
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("type")]
public string Type { get; set; }
}
public class Bookmark : BookmarkElement
{
public Bookmark() { }
[JsonPropertyName("url")]
public string Url {get; set;}
}
public class Folder : BookmarkElement
{
public Folder() {}
[JsonPropertyName("date_modified")]
public string DateModified { get; set; }
[JsonPropertyName("children")]
public List<BookmarkElement> FolderElements {get; set;}
}
public class Root : BookmarkElement
{
public Root() {}
[JsonPropertyName("date_modified")]
public string DateModified { get; set; }
[JsonPropertyName("children")]
public List<BookmarkElement> RootFolder { get; set; }
}
public class BookmarkModel
{
public BookmarkModel() {}
[JsonPropertyName("checksum")]
public string Checksum { get; set; }
[JsonPropertyName("roots")]
public Dictionary<string, Root> Roots { get; set; }
[JsonPropertyName("version")]
public int Version { get; set; }
}
class TestClass
{
public static void Test()
{
var json = GetJson();
var options = new JsonSerializerOptions
{
Converters = { },
WriteIndented = true,
};
var model = JsonSerializer.Deserialize<BookmarkModel>(json, options);
var json2 = JsonSerializer.Serialize(model, options);
Console.WriteLine("Re-serialized {0}", model);
Console.WriteLine(json2);
}
static string GetJson() =>
"""
{
"checksum": "cc1f5c62ec7814f7928e2befab26c311",
"roots": {
"bookmark_bar": {
"children": [ {
"children": [ ],
"date_added": "13335767383821356",
"date_last_used": "0",
"date_modified": "13335767383821356",
"guid": "efeb5549-612d-4656-8982-a17069075213",
"id": "13",
"name": "test",
"type": "folder"
}, {
"date_added": "13335767548044529",
"date_last_used": "0",
"guid": "df7a482b-c1c5-4aa2-af8e-8cee539513d9",
"id": "15",
"name": "DuckDuckGo — Privacy, simplified.",
"type": "url",
"url": "https://duckduckgo.com/"
} ],
"date_added": "13335764354355200",
"date_last_used": "0",
"date_modified": "13335767548044529",
"guid": "0bc5d13f-2cba-5d74-951f-3f233fe6c908",
"id": "1",
"name": "Bookmarks bar",
"type": "folder"
},
"other": {
"children": [ ],
"date_added": "13335764354355201",
"date_last_used": "0",
"date_modified": "0",
"guid": "82b081ec-3dd3-529c-8475-ab6c344590dd",
"id": "2",
"name": "Other bookmarks",
"type": "folder"
},
"synced": {
"children": [ ],
"date_added": "13335764354355202",
"date_last_used": "0",
"date_modified": "0",
"guid": "4cf2e351-0e85-532b-bb37-df045d8f8d0f",
"id": "3",
"name": "Mobile bookmarks",
"type": "folder"
}
},
"version": 1
}
""";
}
public class Program
{
public static void Main()
{
Console.WriteLine("Environment version: {0} ({1}), {2}", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , Environment.Version, Environment.OSVersion);
Console.WriteLine("System.Text.Json version: " + typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine();
try
{
TestClass.Test();
}
catch (Exception ex)
{
Console.WriteLine("Failed with unhandled exception: ");
Console.WriteLine(ex);
throw;
}
}
}