Skip to content

Latest commit

 

History

History
59 lines (45 loc) · 1.65 KB

File metadata and controls

59 lines (45 loc) · 1.65 KB

TypeSerialization NuGet version

Serialization an object type to/from a string

Features

  • Generics support
  • URI safe format

Example 1: Serialization into a uri-safe format

var str = TypeSerializer.Serialize(typeof(Dictionary<int,string>), Formats.UriSafe);
Console.WriteLine(str);

// Console output: 
// Dictionary(Int32-String)

Example 2: Serialization into a code-like format

var str = TypeSerializer.Serialize(typeof(Dictionary<int,string>), Formats.CodeLike);
Console.WriteLine(str);

// Console output: 
// Dictionary<Int32,MyCustomClass>

Example 3: Deserialization

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]

Example 4: JSON serialization

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)"}

Example 5: JSON deserialization

var objFromJson = JsonSerializer.Deserialize<MyCustomClass>(json, jsonOptions);
Console.WriteLine(objFromJson.MyTypeProperty);

// Console output: 
// System.Collections.Generic.List`1[System.Int32]

Program.cs