Harvzor.Optional.SystemTextJson
0.0.5
See the version list below for details.
dotnet add package Harvzor.Optional.SystemTextJson --version 0.0.5
NuGet\Install-Package Harvzor.Optional.SystemTextJson -Version 0.0.5
<PackageReference Include="Harvzor.Optional.SystemTextJson" Version="0.0.5" />
paket add Harvzor.Optional.SystemTextJson --version 0.0.5
#r "nuget: Harvzor.Optional.SystemTextJson, 0.0.5"
// Install Harvzor.Optional.SystemTextJson as a Cake Addin #addin nuget:?package=Harvzor.Optional.SystemTextJson&version=0.0.5 // Install Harvzor.Optional.SystemTextJson as a Cake Tool #tool nuget:?package=Harvzor.Optional.SystemTextJson&version=0.0.5
Harvzor.Optional
NuGet Package | Version |
---|---|
Harvzor.Optional | |
Harvzor.Optional.SystemTextJson | |
Harvzor.Optional.NewtonsoftJson |
The problem
// The JSON would normally come from some external data source:
string json = "{}";
Foo foo = JsonSerializer.Deserialize<Foo>(json);
// This will print an empty string because C# will hydrate the model with the default value.
// There's no way to check if the model used a default value.
Console.WriteLine(foo.MyProperty); // ""
public class Foo
{
public string MyProperty { get; set; }
}
In the above example, you could make the string nullable (with string?
), but now you're explicitly saying that null is value you want to accept.
And then you might actually want to explicitly allow null values, but then run into the same issue where you're not sure if the original JSON had
the value as null
, or if it was just undefined.
The solution: Optional<T>
Basic example
You can use Optional<T>
to know if a property or variable has been explicitly instantiated:
using Harvzor.Optional;
Foo foo = new Foo
{
DefinedProperty = "Bar"
};
Console.WriteLine(foo.DefinedProperty.IsDefined); // True
Console.WriteLine(foo.UndefinedProperty.IsDefined); // False
// Now I can check if a value was explicitly instantiated:
if (foo.DefinedProperty.IsDefined)
Console.WriteLine(foo.DefinedProperty.Value); // "Bar"
if (foo.UndefinedProperty.IsDefined)
Console.WriteLine(foo.UndefinedProperty.Value); // Won't print as the value wasn't explicitly instantiated.
Console.WriteLine(foo.UndefinedProperty.Value); // Will print the default value.
public class Foo
{
public Optional<string> DefinedProperty { get; set; }
public Optional<string> UndefinedProperty { get; set; }
}
Use it with JSON
This example uses System.Text.Json, but it's pretty similar to Newtonsoft:
using System.Text.Json;
using Harvzor.Optional;
JsonSerializerOptions jsonSerializerOptions = new JsonSerializerOptions();
jsonSerializerOptions.Converters.Add(new Harvzor.Optional.SystemTextJson.OptionalJsonConverter());
// The JSON would normally come from some external data source:
string json = "{\"DefinedProperty\":\"Bar\"}";
Foo foo = JsonSerializer.Deserialize<Foo>(json, jsonSerializerOptions)!;
Console.WriteLine(foo.DefinedProperty.IsDefined); // True
Console.WriteLine(foo.UndefinedProperty.IsDefined); // False
// Now I can check if a value was defined before I try using it:
if (foo.DefinedProperty.IsDefined)
Console.WriteLine(foo.DefinedProperty.Value); // "Bar"
if (foo.UndefinedProperty.IsDefined)
Console.WriteLine(foo.UndefinedProperty.Value); // Won't print as the value wasn't defined.
public class Foo
{
public Optional<string> DefinedProperty { get; set; }
public Optional<string> UndefinedProperty { get; set; }
}
Harvzor.Optional.SystemTextJson
Use in an API
To use it in your controller models, simply register in your startup:
services
.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new Harvzor.Optional.SystemTextJson.OptionalJsonConverter());
});
Swagger support
If you're using Swashbuckle Swagger, you'll also need to tell it how your types should look:
services.AddSwaggerGen(options =>
{
options.MapType<Optional<string>>(() => new OpenApiSchema
{
Type = "string"
});
options.MapType<Optional<bool>>(() => new OpenApiSchema
{
Type = "boolean"
});
});
You can see what basic types are available here: https://swagger.io/docs/specification/data-models/data-types/
Harvzor.Optional.NewtonsoftJson
... docs to come ...
Releasing
Manual release
- Get an API key from https://www.nuget.org/account/apikeys
docker-compose build --build-arg version="{version}" push-nuget
docker-compose run --rm push-nuget --api-key {key}
Product | Versions 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. |
-
.NETStandard 2.0
- Harvzor.Optional (>= 0.0.5)
- System.Text.Json (>= 4.7.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.