Table of Contents

Class JsonStorage

Namespace
NanoByte.Common.Storage
Assembly
NanoByte.Common.dll

Provides easy serialization to JSON files.

public static class JsonStorage
Inheritance
JsonStorage

Methods

FromJsonString<T>(string)

Loads an object from an JSON string.

public static T FromJsonString<T>(string data)

Parameters

data string

The JSON string to be parsed.

Returns

T

The loaded object.

Type Parameters

T

The type of object the JSON string shall be converted into.

Exceptions

InvalidDataException

A problem occurred while deserializing the JSON data.

FromJsonString<T>(string, T)

Loads an object from an JSON string using an anonymous type as the target.

public static T FromJsonString<T>(string data, T anonymousType)

Parameters

data string

The JSON string to be parsed.

anonymousType T

An instance of the anonymous type to parse to.

Returns

T

The deserialized object.

Type Parameters

T

The type of object the JSON string shall be converted into.

LoadJson<T>(Stream)

Loads an object from an JSON file.

public static T LoadJson<T>(Stream stream)

Parameters

stream Stream

The stream to read the encoded JSON data from.

Returns

T

The loaded object.

Type Parameters

T

The type of object the JSON stream shall be converted into.

Exceptions

InvalidDataException

A problem occurred while deserializing the JSON data.

LoadJson<T>(string)

Loads an object from an JSON file.

public static T LoadJson<T>(string path)

Parameters

path string

The path of the file to load.

Returns

T

The loaded object.

Type Parameters

T

The type of object the JSON stream shall be converted into.

Remarks

Uses AtomicRead internally.

Exceptions

IOException

A problem occurred while reading the file.

UnauthorizedAccessException

Read access to the file is not permitted.

InvalidDataException

A problem occurred while deserializing the JSON data.

ReparseAsJson<T>(object)

Reparses an object previously deserialized from JSON into a different representation.

public static T ReparseAsJson<T>(this object data)

Parameters

data object

The object to be parsed again.

Returns

T

The deserialized object.

Type Parameters

T

The type of object the data shall be converted into.

ReparseAsJson<T>(object, T)

Reparses an object previously deserialized from JSON into a different representation using an anonymous type as the target.

public static T ReparseAsJson<T>(this object data, T anonymousType)

Parameters

data object

The object to be parsed again.

anonymousType T

An instance of the anonymous type to parse to.

Returns

T

The deserialized object.

Type Parameters

T

The type of object the data shall be converted into.

SaveJson<T>(T, Stream)

Saves an object in an JSON stream.

public static void SaveJson<T>(this T data, Stream stream)

Parameters

data T

The object to be stored.

stream Stream

The stream to write the encoded JSON data to.

Type Parameters

T

The type of object to be saved in an JSON stream.

SaveJson<T>(T, string)

Saves an object in an JSON file.

public static void SaveJson<T>(this T data, string path) where T : notnull

Parameters

data T

The object to be stored.

path string

The path of the file to write.

Type Parameters

T

The type of object to be saved in an JSON stream.

Remarks

This method performs an atomic write operation when possible.

Exceptions

IOException

A problem occurred while writing the file.

UnauthorizedAccessException

Write access to the file is not permitted.

ToJsonString<T>(T)

Returns an object as an JSON string.

public static string ToJsonString<T>(this T data) where T : notnull

Parameters

data T

The object to be stored.

Returns

string

A string containing the JSON code.

Type Parameters

T

The type of object to be saved in an JSON string.