version | downloads | package |
---|---|---|
Qowaiv.Json.Newtonsoft | ||
Qowaiv.Text.Json.Serialization | ||
Qowaiv.Bson.MongoDB |
Serializing data using JSON is common practice. However, .NET has no generic interface in the standard library to implement.
The solution provided here, is a (naming) convention based one, to serialize Single Value Objects: Value Objects that can be represented by a single scalar.
- There should be a public static factory method
FromJson(string)
returning a new instance of the Single Value Object (SVO). - Optional factory methods for
double
,long
, andbool
can be provided. If not, thestring
factory method is used. - If a none
void
methodToJson()
is provided, this one is used for serialization, otherwiseobject.ToString()
.
public struct /* or class */ SingleValueObject
{
// Required.
public static SingleValueObject FromJson(string json);
// Optional, otherwise FromJson(json.ToString(CultureInfo.Invariant)) is called.
public static SingleValueObject FromJson(double json);
// Optional, otherwise FromJson(json.ToString(CultureInfo.Invariant)) is called.
public static SingleValueObject FromJson(long json);
// Optional, otherwise FromJson(json ? "true": "false") is called.
public static SingleValueObject FromJson(bool json);
// Optional, otherwise ToString()
public object /* or string, bool, int, long, double, decimal */ToJson();
}
Since .NET Core 3.0, Microsoft provides a built-in JSON serialization. To use
the Qowaiv.Text.Json.Serialization
package the following code can be used:
using Qowaiv.Text.Json.Serialization;
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services
.AddMvc()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new QowaivJsonConverter());
});
}
}
Newtonsoft's converter was the .NET de facto default, until .NET Core 3.0. To use
the Qowaiv.Json.Newtonsoft
package the following code can be used:
QowaivJsonConverter.Register();
Or, if you work with .NET core Web API:
NuGet:
Install-Package Microsoft.AspNetCore.Mvc.NewtonsoftJson
Startup.cs:
using Qowaiv.Json.Newtonsoft;
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services
.AddMvc()
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.Converters.Add(new QowaivJsonConverter());
});
}
}
MongoDB is a cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses BSON documents with a schema. The .NET library provides a mechanism to convert objects from and to BSON (..)