Class JsonStorage
Provides easy serialization to JSON files.
Namespace: NanoByte.Common.Storage
Assembly: NanoByte.Common.dll
Syntax
public static class JsonStorage : Object
Methods
FromJsonString<T>(String)
Loads an object from an JSON string.
Declaration
public static T FromJsonString<T>(string data)
Parameters
Type | Name | Description |
---|---|---|
String | data | The JSON string to be parsed. |
Returns
Type | Description |
---|---|
T | The loaded object. |
Type Parameters
Name | Description |
---|---|
T | The type of object the JSON string shall be converted into. |
Exceptions
Type | Condition |
---|---|
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.
Declaration
public static T FromJsonString<T>(string data, T anonymousType)
Parameters
Type | Name | Description |
---|---|---|
String | data | The JSON string to be parsed. |
T | anonymousType | An instance of the anonymous type to parse to. |
Returns
Type | Description |
---|---|
T | The deserialized object. |
Type Parameters
Name | Description |
---|---|
T | The type of object the JSON string shall be converted into. |
LoadJson<T>(Stream)
Loads an object from an JSON file.
Declaration
public static T LoadJson<T>(Stream stream)
Parameters
Type | Name | Description |
---|---|---|
Stream | stream | The stream to read the encoded JSON data from. |
Returns
Type | Description |
---|---|
T | The loaded object. |
Type Parameters
Name | Description |
---|---|
T | The type of object the JSON stream shall be converted into. |
Exceptions
Type | Condition |
---|---|
InvalidDataException | A problem occurred while deserializing the JSON data. |
LoadJson<T>(String)
Loads an object from an JSON file.
Declaration
public static T LoadJson<T>(string path)
Parameters
Type | Name | Description |
---|---|---|
String | path | The path of the file to load. |
Returns
Type | Description |
---|---|
T | The loaded object. |
Type Parameters
Name | Description |
---|---|
T | The type of object the JSON stream shall be converted into. |
Remarks
Uses AtomicRead internally.
Exceptions
Type | Condition |
---|---|
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.
Declaration
public static T ReparseAsJson<T>(this object data)
Parameters
Type | Name | Description |
---|---|---|
Object | data | The object to be parsed again. |
Returns
Type | Description |
---|---|
T | The deserialized object. |
Type Parameters
Name | Description |
---|---|
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.
Declaration
public static T ReparseAsJson<T>(this object data, T anonymousType)
Parameters
Type | Name | Description |
---|---|---|
Object | data | The object to be parsed again. |
T | anonymousType | An instance of the anonymous type to parse to. |
Returns
Type | Description |
---|---|
T | The deserialized object. |
Type Parameters
Name | Description |
---|---|
T | The type of object the data shall be converted into. |
SaveJson<T>(T, Stream)
Saves an object in an JSON stream.
Declaration
public static void SaveJson<T>(this T data, Stream stream)
Parameters
Type | Name | Description |
---|---|---|
T | data | The object to be stored. |
Stream | stream | The stream to write the encoded JSON data to. |
Type Parameters
Name | Description |
---|---|
T | The type of object to be saved in an JSON stream. |
SaveJson<T>(T, String)
Saves an object in an JSON file.
Declaration
public static void SaveJson<T>(this T data, string path)
Parameters
Type | Name | Description |
---|---|---|
T | data | The object to be stored. |
String | path | The path of the file to write. |
Type Parameters
Name | Description |
---|---|
T | The type of object to be saved in an JSON stream. |
Remarks
This method performs an atomic write operation when possible.
Exceptions
Type | Condition |
---|---|
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.
Declaration
public static string ToJsonString<T>(this T data)
Parameters
Type | Name | Description |
---|---|---|
T | data | The object to be stored. |
Returns
Type | Description |
---|---|
String | A string containing the JSON code. |
Type Parameters
Name | Description |
---|---|
T | The type of object to be saved in an JSON string. |