Contact: numbworks@gmail.com
Date | Author | Description |
---|---|---|
2024-02-13 | numbworks | Created. |
NW.Shared.Serialization
is a library that provides a facade over System.Text.Json.JsonSerializer in order to simplify serialization and deserialization activities.
In order to use the library:
- download and install the library in your project via NuGet - for ex. by using the Visual Studio UI or the .NET CLI:
dotnet add package NW.Shared.Serialization --version 1.0.0
- reference and start using the library:
using System;
using NW.Shared.Files;
using NW.Shared.Serialization;
/* ... */
private void Save<T>(T obj, IFileInfoAdapter jsonFile)
{
_componentBag.LoggingAction(Forecasts.MessageCollection.AttemptingToSaveObjectAs(typeof(T), jsonFile));
try
{
ISerializer<T> serializer = _componentBag.SerializerFactory.Create<T>();
string content = serializer.Serialize(obj);
_componentBag.FileManager.WriteAllText(jsonFile, content);
_componentBag.LoggingAction(Forecasts.MessageCollection.ObjectSuccessfullySaved(typeof(T)));
}
catch
{
_componentBag.LoggingAction(Forecasts.MessageCollection.ObjectFailedToSave(typeof(T)));
}
}
/* ... */
- Done!
Suggested toolset to view and edit this Markdown file: