Serialization an object type to/from a string
- Generics support
- URI safe format
var str = TypeSerializer.Serialize(typeof(Dictionary<int,string>), Formats.UriSafe);
Console.WriteLine(str);
// Console output:
// Dictionary(Int32-String)var str = TypeSerializer.Serialize(typeof(Dictionary<int,string>), Formats.CodeLike);
Console.WriteLine(str);
// Console output:
// Dictionary<Int32,MyCustomClass>var deserializer = new TypeDeserializer(/* add your possible types for resolving */);
var type = deserializer.Deserialize("Dictionary(Int32-String)");
Console.WriteLine(type);
// Console output:
// System.Collections.Generic.Dictionary`2[System.Int32,System.String]var jsonOptions = new JsonSerializerOptions();
jsonOptions.Converters.Add(new JsonTypeConverter(deserializer));
var obj = new MyCustomClass() { MyTypeProperty = typeof(List<int>) };
var json = JsonSerializer.Serialize(obj, jsonOptions);
Console.WriteLine(json);
// Console output:
// {"MyTypeProperty":"List(Int32)"}var objFromJson = JsonSerializer.Deserialize<MyCustomClass>(json, jsonOptions);
Console.WriteLine(objFromJson.MyTypeProperty);
// Console output:
// System.Collections.Generic.List`1[System.Int32]