Table of Contents

Class SerializerHelperJson

Namespace
SunamoJson
Assembly
SunamoJson.dll

Helper class for JSON serialization and deserialization.

public static class SerializerHelperJson
Inheritance
SerializerHelperJson
Inherited Members

Methods

ReadFromJsonFile<T>(ILogger, string, ReadFromJsonFileArgs)

Reads an object instance from a JSON file.

Object type must have a parameterless constructor.

public static Task<T?> ReadFromJsonFile<T>(ILogger logger, string path, ReadFromJsonFileArgs args) where T : new()

Parameters

logger ILogger

The logger instance for error logging.

path string

The file path to read the object instance from.

args ReadFromJsonFileArgs

Arguments for controlling the read behavior.

Returns

Task<T>

Returns a new instance of the object read from the JSON file, or null if deserialization fails.

Type Parameters

T

The type of object to read from the file.

WriteToJsonFile<T>(ILogger, string, T, WriteToJsonFileArgs?)

Writes the given object instance to a JSON file.

Object type must have a parameterless constructor.

Only public properties and variables will be written to the file. These can be any type though, even other classes.

If there are public properties/variables that you do not want written to the file, decorate them with the [JsonIgnore] attribute.

public static Task<bool> WriteToJsonFile<T>(ILogger logger, string path, T objectToWrite, WriteToJsonFileArgs? args = null) where T : new()

Parameters

logger ILogger

The logger instance for error logging.

path string

The file path to write the object instance to.

objectToWrite T

The object instance to write to the file.

args WriteToJsonFileArgs

Optional arguments for controlling the write behavior.

Returns

Task<bool>

True if the write was successful; otherwise, false.

Type Parameters

T

The type of object being written to the file.