Class JsonStorage
Provides easy serialization to JSON files.
public static class JsonStorage
- Inheritance
-
objectJsonStorage
Methods
FromJsonString<T>(string)
Loads an object from an JSON string.
public static T FromJsonString<T>(string data)
Parameters
datastringThe JSON string to be parsed.
Returns
- T
The loaded object.
Type Parameters
TThe 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
datastringThe JSON string to be parsed.
anonymousTypeTAn instance of the anonymous type to parse to.
Returns
- T
The deserialized object.
Type Parameters
TThe 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
streamStreamThe stream to read the encoded JSON data from.
Returns
- T
The loaded object.
Type Parameters
TThe 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
pathstringThe path of the file to load.
Returns
- T
The loaded object.
Type Parameters
TThe 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
dataobjectThe object to be parsed again.
Returns
- T
The deserialized object.
Type Parameters
TThe 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
dataobjectThe object to be parsed again.
anonymousTypeTAn instance of the anonymous type to parse to.
Returns
- T
The deserialized object.
Type Parameters
TThe 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
dataTThe object to be stored.
streamStreamThe stream to write the encoded JSON data to.
Type Parameters
TThe 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
dataTThe object to be stored.
pathstringThe path of the file to write.
Type Parameters
TThe 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
dataTThe object to be stored.
Returns
- string
A string containing the JSON code.
Type Parameters
TThe type of object to be saved in an JSON string.