ByteDev.Json.SystemTextJson 1.4.0

dotnet add package ByteDev.Json.SystemTextJson --version 1.4.0                
NuGet\Install-Package ByteDev.Json.SystemTextJson -Version 1.4.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ByteDev.Json.SystemTextJson" Version="1.4.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ByteDev.Json.SystemTextJson --version 1.4.0                
#r "nuget: ByteDev.Json.SystemTextJson, 1.4.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ByteDev.Json.SystemTextJson as a Cake Addin
#addin nuget:?package=ByteDev.Json.SystemTextJson&version=1.4.0

// Install ByteDev.Json.SystemTextJson as a Cake Tool
#tool nuget:?package=ByteDev.Json.SystemTextJson&version=1.4.0                

ByteDev.Json.SystemTextJson

.NET Standard library of functionality built on System.Text.Json.

Installation

ByteDev.Json.SystemTextJson is hosted as a package on nuget.org. To install from the Package Manager Console in Visual Studio run:

Install-Package ByteDev.Json.SystemTextJson

Further details can be found on the nuget page.

Release Notes

Releases follow semantic versioning.

Full details of the release notes can be viewed on GitHub.

Usage

All custom JsonConverter types are in namespace ByteDev.Json.SystemTextJson.Serialization.


StringToDateTimeJsonConverter

Converter allows a JSON string representation of a date time to be automatically converted to a DateTime and back again using the supplied date time format string.

public class TestEntity
{
    [JsonPropertyName("datetime")]
    public DateTime MyDateTime { get; set; }
}
var converter = new StringToDateTimeJsonConverter("yyyy-MM-dd HH:mm:ss");

var options = new JsonSerializerOptions();
options.Converters.Add(converter);

string json = "{\"datetime\":\"2022-01-12 09:59:20\"}";

var obj = JsonSerializer.Deserialize<TestEntity>(json, options);

var newJson = JsonSerializer.Serialize(obj, options);

// newJson == json

UnixEpochTimeToDateTimeJsonConverter

Converter allows a JSON number representation of a Unix epoch time to be automatically converted to a DateTime and back again.

Both Unix epoch times as seconds or milliseconds are supported.

The converter assumes that the DateTime is UTC.

public class TestEntity
{
    [JsonPropertyName("datetime")]
    public DateTime MyDateTime { get; set; }
}
var converter = new UnixEpochTimeToDateTimeJsonConverter
{
    Precision = UnixEpochTimePrecision.Seconds
};

var options = new JsonSerializerOptions();
options.Converters.Add(converter);

string json = "{\"datetime\":1641974376}";

var obj = JsonSerializer.Deserialize<TestEntity>(json, options);

var newJson = JsonSerializer.Serialize(obj, options);

// newJson == json

EnumJsonConverter

Converter allows a JSON number or string representation of an enum to be automatically converted to a specified Enum.

As well as supporting both enum value (number) and name (string) alias string names can also be specified through use of JsonEnumStringValueAttribute.

public enum LightSwitch
{
    On = 1,
        
    [JsonEnumStringValue("switchOff")]
    Off = 2,

    Faulty = 3
}

public class MyHouse
{
    [JsonPropertyName("lights")]
    public LightSwitch BedroomLights { get; set; }
}
var converter = new EnumJsonConverter<LightSwitch>
{
    // false => Enum number value used
    // true => Enum string name used 
    // (or JsonEnumStringValueAttribute value if defined)
    WriteEnumName = true
};

var options = new JsonSerializerOptions();
options.Converters.Add(converter);

// Example JSON representations:
// - Using enum value => {"lights":1}
// - Using enum name => {"lights":"On"}
// - Using attribute name => {"lights":"switchOff"}

string json = "{\"lights\":\"switchOff\"}";

var obj = JsonSerializer.Deserialize<MyHouse>(json, options);

var newJson = JsonSerializer.Serialize(obj, options);

// newJson == json

NumberToBoolJsonConverter

Converter allows a JSON number (integer) to be automatically converted to a .NET Boolean.

public class BoolEntity
{
    [JsonPropertyName("myBool")]
    public bool MyBool { get; set; }
}
// Optional true/false integer representations params
var converter = new NumberToBoolJsonConverter();

var options = new JsonSerializerOptions();
options.Converters.Add(converter);

string json = "{\"myBool\":1}";

var obj = JsonSerializer.Deserialize<BoolEntity>(json, options);

// obj.MyBool == true

var newJson = JsonSerializer.Serialize(obj, options);

// newJson == json

StringToBoolJsonConverter & StringToNullableBoolJsonConverter

Converter allows a JSON string to be automatically converted to a .NET Boolean.

public class BoolEntity
{
    [JsonPropertyName("myBool")]
    public bool MyBool { get; set; }
}
var converter = new StringToBoolJsonConverter("N", "Y");

var options = new JsonSerializerOptions();
options.Converters.Add(converter);

string json = "{\"myBool\":\"Y\"}";

var obj = JsonSerializer.Deserialize<BoolEntity>(json, options);

// obj.MyBool == true

var newJson = JsonSerializer.Serialize(obj, options);

// newJson == json

The package also contains the similar converter StringToNullableBoolJsonConverter.

This works in the same way to StringToBoolJsonConverter but converts to a .NET Nullable Boolean type.

The converter's constructor takes an additional third string parameter to represent the null value in JSON.


StringToGuidJsonConverter

Converter allows a JSON string to be automatically converted to a .NET System.Guid.

Supports reading (deserializing) many different representations of Guid strings as well as specifying a write format to use when writing (serializing) to JSON.

public class GuidEntity
{
    [JsonPropertyName("myGuid")]
    public Guid MyGuid { get; set; }
}
var converter = new StringToGuidJsonConverter("N");

var options = new JsonSerializerOptions();
options.Converters.Add(converter);

string json = "{\"myGuid\":\"c62a82b35e0d47bf9a12b027ff56d3e3\"}";

var obj = JsonSerializer.Deserialize<GuidEntity>(json, options);

// obj.MyGuid == Guid.Parse("c62a82b3-5e0d-47bf-9a12-b027ff56d3e3")

var newJson = JsonSerializer.Serialize(obj, options) ;

// newJson == json

JSON string write formats currently supported:

"D" = "c62a82b3-5e0d-47bf-9a12-b027ff56d3e3"
"N" = "c62a82b35e0d47bf9a12b027ff56d3e3"
"B" = "{c62a82b3-5e0d-47bf-9a12-b027ff56d3e3}"
"P" = "(c62a82b3-5e0d-47bf-9a12-b027ff56d3e3)"
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on ByteDev.Json.SystemTextJson:

Package Downloads
ByteDev.AirVpn

.NET Standard library SDK to help communicate with the AirVPN API.

ByteDev.AirVpn.IpLeak

.NET Standard library to help communicate with the IP Leak API.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.4.0 462 5/27/2022
1.3.0 445 2/11/2022
1.2.0 300 12/7/2021
1.1.0 722 8/4/2021
1.0.0 357 8/4/2021