Skip to content

Serializer Class

Common serialization tasks for SadConsole.

C#
public static class Serializer

Inheritance object

The settings to use during Save<T>(T, string, bool, JsonSerializerSettings) and Load<T>(string, bool, JsonSerializerSettings).

C#
public static JsonSerializerSettings Settings { get; set; }

A shortcut for serialization that uses SerializeObject(object, Type, JsonSerializerSettings) with the Settings property defined by this class.

C#
public static string Serialize<T>(T instance)

instance T
The object to serialize.

string
A json encoded string.

A shortcut for serialization that uses DeserializeObject(string, Type, JsonSerializerSettings) with the Settings property defined by this class.

C#
public static T Deserialize<T>(string json)

json string
The json string to create an object from.

T
An object created from the json parameter.

Save<T>(T, string, bool, JsonSerializerSettings?)

Section titled “Save<T>(T, string, bool, JsonSerializerSettings?)”

Serializes the instance to the specified file.

C#
public static void Save<T>(T instance, string file, bool compress, JsonSerializerSettings? settings = null)

instance T
The object to serialize.

file string
The file to save the object to.

compress bool
When true, uses GZIP compression on the json string saved to the file

settings Newtonsoft.Json.JsonSerializerSettings
Optional settings to use during serialization. If null, uses the Settings property.

Load<T>(string, bool, JsonSerializerSettings?)

Section titled “Load<T>(string, bool, JsonSerializerSettings?)”

Deserializes a new instance of T from the specified file.

C#
public static T Load<T>(string file, bool isCompressed, JsonSerializerSettings? settings = null)

file string
The file to load from.

isCompressed bool
When true, indicates that the json file should be decompressed with GZIP compression.

settings Newtonsoft.Json.JsonSerializerSettings
Optional settings to use during deserialization. If null, uses the Settings property.

T
A new object instance.

TryLoad<T>(string, bool, out T?, JsonSerializerSettings?)

Section titled “TryLoad<T>(string, bool, out T?, JsonSerializerSettings?)”

Tries to load the file, returning it as the specified tyupe.

C#
public static bool TryLoad<T>(string file, bool isCompressed, out T? obj, JsonSerializerSettings? settings = null)

file string
The file to load from.

isCompressed bool
When true, indicates that the json file should be decompressed with GZIP compression.

obj T
The loaded object.

settings Newtonsoft.Json.JsonSerializerSettings
Optional settings to use during deserialization. If null, uses the Settings property.

bool