Table of Contents

Class JsonStorage

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

Provides easy serialization to JSON files.

public static class JsonStorage
Inheritance
object
JsonStorage

Remarks

On .NET, serialization uses System.Text.Json. On .NET Framework, it uses Newtonsoft.Json.

Methods

FromJsonString<T>(string)

Loads an object from an JSON string.

[SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "The type parameter is used to determine the type of returned object")]
[RequiresUnreferencedCode("JSON serialization uses reflection, which is not compatible with trimming. Use an overload taking a JsonTypeInfo<T> instead.")]
[RequiresDynamicCode("JSON serialization uses reflection, which requires runtime code generation. Use an overload taking a JsonTypeInfo<T> instead.")]
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.

[RequiresUnreferencedCode("JSON serialization uses reflection, which is not compatible with trimming. Use an overload taking a JsonTypeInfo<T> instead.")]
[RequiresDynamicCode("JSON serialization uses reflection, which requires runtime code generation. Use an overload taking a JsonTypeInfo<T> instead.")]
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.

Exceptions

InvalidDataException

A problem occurred while deserializing the JSON data.

LoadJson<T>(Stream)

Loads an object from an JSON file.

[SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "The type parameter is used to determine the type of returned object")]
[RequiresUnreferencedCode("JSON serialization uses reflection, which is not compatible with trimming. Use an overload taking a JsonTypeInfo<T> instead.")]
[RequiresDynamicCode("JSON serialization uses reflection, which requires runtime code generation. Use an overload taking a JsonTypeInfo<T> instead.")]
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.

[SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "The type parameter is used to determine the type of returned object")]
[RequiresUnreferencedCode("JSON serialization uses reflection, which is not compatible with trimming. Use an overload taking a JsonTypeInfo<T> instead.")]
[RequiresDynamicCode("JSON serialization uses reflection, which requires runtime code generation. Use an overload taking a JsonTypeInfo<T> instead.")]
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.

[SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "The type parameter is used to determine the type of returned object")]
[RequiresUnreferencedCode("JSON serialization uses reflection, which is not compatible with trimming. Use an overload taking a JsonTypeInfo<T> instead.")]
[RequiresDynamicCode("JSON serialization uses reflection, which requires runtime code generation. Use an overload taking a JsonTypeInfo<T> instead.")]
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.

[RequiresUnreferencedCode("JSON serialization uses reflection, which is not compatible with trimming. Use an overload taking a JsonTypeInfo<T> instead.")]
[RequiresDynamicCode("JSON serialization uses reflection, which requires runtime code generation. Use an overload taking a JsonTypeInfo<T> instead.")]
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.

[RequiresUnreferencedCode("JSON serialization uses reflection, which is not compatible with trimming. Use an overload taking a JsonTypeInfo<T> instead.")]
[RequiresDynamicCode("JSON serialization uses reflection, which requires runtime code generation. Use an overload taking a JsonTypeInfo<T> instead.")]
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.

[RequiresUnreferencedCode("JSON serialization uses reflection, which is not compatible with trimming. Use an overload taking a JsonTypeInfo<T> instead.")]
[RequiresDynamicCode("JSON serialization uses reflection, which requires runtime code generation. Use an overload taking a JsonTypeInfo<T> instead.")]
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.

[RequiresUnreferencedCode("JSON serialization uses reflection, which is not compatible with trimming. Use an overload taking a JsonTypeInfo<T> instead.")]
[RequiresDynamicCode("JSON serialization uses reflection, which requires runtime code generation. Use an overload taking a JsonTypeInfo<T> instead.")]
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.