Skip to content

Commit

Permalink
Phase 2 cleanup and update.
Browse files Browse the repository at this point in the history
  • Loading branch information
electricessence committed May 21, 2022
1 parent 3c7f5b0 commit fc9822c
Show file tree
Hide file tree
Showing 24 changed files with 424 additions and 275 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,26 @@
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<PackageIcon>logo.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
<None Include="..\logo.png">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
<None Include="..\README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.6" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.1" />
<PackageReference Include="Open.Serialization.Json" Version="2.3.1" />
<PackageReference Include="System.Memory" Version="4.5.4" />
Expand Down
3 changes: 3 additions & 0 deletions Open.Serialization.Json.Newtonsoft/SerializationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

namespace Open.Serialization.Json.Newtonsoft;

/// <summary>
/// Extensions for Newtonsof.Json serialization with Open.Serialization.Json.
/// </summary>
public static class SerializationExtensions
{
/// <summary>
Expand Down
15 changes: 10 additions & 5 deletions Open.Serialization.Json.System/CamelCaseJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@

namespace Open.Serialization.Json.System;

/// <summary>
/// Provides default 'camel case' serialization configurations.
/// </summary>
public static class CamelCaseJson
{
/// <summary>
/// Default relaxed serializations.
/// </summary>
public static JsonSerializerOptions Default(bool indent = false)
{
var options = RelaxedJson.Options(indent);
options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
return options;
}

/// <summary>
/// Default relaxed serializations but also ignores null values.
/// </summary>
public static JsonSerializerOptions Minimal(bool indent = false)
{
var options = Default(indent);
options.IgnoreNullValues = true;
return options;
}
=> Default(indent).SetIgnoreNullValues();
}
15 changes: 10 additions & 5 deletions Open.Serialization.Json.System/CaseSensitiveJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@

namespace Open.Serialization.Json.System;

/// <summary>
/// Provides default 'case sensitive' serialization configurations.
/// </summary>
public static class CaseSensitiveJson
{
/// <summary>
/// Default relaxed serializations.
/// </summary>
public static JsonSerializerOptions Default(bool indent = false)
=> RelaxedJson.Options(indent, true);

/// <summary>
/// Default relaxed serializations but also ignores null values.
/// </summary>
public static JsonSerializerOptions Minimal(bool indent = false)
{
var options = Default(indent);
options.IgnoreNullValues = true;
return options;
}
=> Default(indent).SetIgnoreNullValues();
}
11 changes: 11 additions & 0 deletions Open.Serialization.Json.System/Converters/JsonDecimalConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,30 @@

namespace Open.Serialization.Json.System.Converters;

/// <summary>
/// Converter for decimals.
/// </summary>
public class JsonDecimalConverter : JsonValueConverterBase<decimal>
{
/// <summary>
/// Constructs a <see cref="JsonDecimalConverter"/>.
/// </summary>
protected JsonDecimalConverter()
{
// Prevent unnecessary replication.
}

/// <summary>
/// The shared instance of a <see cref="JsonDecimalConverter"/>.
/// </summary>
public static readonly JsonDecimalConverter Instance
= new();

/// <inheritdoc />
public override decimal Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> reader.GetDecimal();

/// <inheritdoc />
public override void Write(Utf8JsonWriter writer, decimal value, JsonSerializerOptions options)
{
if (writer is null) throw new ArgumentNullException(nameof(writer));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,33 @@

namespace Open.Serialization.Json.System.Converters;

/// <summary>
/// Converter for decimals that rounds to a maximum number of digits after the decimal.
/// </summary>
public class JsonDecimalRoundingConverter : JsonDecimalConverter
{
/// <summary>
/// The maximum number of digits after the decimal.
/// </summary>
public int Maximum { get; }

/// <summary>
/// Constructs a <see cref="JsonDecimalRoundingConverter"/>.
/// </summary>
/// <param name="maximum">The maximum number of digits after the decimal.</param>
/// <exception cref="ArgumentOutOfRangeException">If the maximum is less than zero.</exception>
public JsonDecimalRoundingConverter(int maximum)
{
if (maximum < 0)
throw new ArgumentOutOfRangeException(nameof(maximum), maximum, "Must be at least zero.");
Maximum = maximum;
}

/// <inheritdoc />
public override decimal Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> Math.Round(reader.GetDecimal(), Maximum);

/// <inheritdoc />
public override void Write(Utf8JsonWriter writer, decimal value, JsonSerializerOptions options)
=> base.Write(writer, Math.Round(value, Maximum), options);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ public JsonDoubleRoundingConverter(int maximum)
Maximum = maximum;
}

/// <inheritdoc />
public override double Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> Math.Round(reader.GetDouble(), Maximum);

/// <inheritdoc />
public override void Write(Utf8JsonWriter writer, double value, JsonSerializerOptions options)
{
if (writer is null) throw new ArgumentNullException(nameof(writer));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ protected JsonNullableDecimalConverter()
public static readonly JsonNullableDecimalConverter Instance
= new();

/// <inheritdoc />
public override bool CanConvert(Type objectType)
=> objectType == typeof(decimal?) || objectType == typeof(decimal);

/// <inheritdoc />
public override decimal? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> reader.TokenType switch
{
Expand All @@ -26,6 +28,7 @@ public override bool CanConvert(Type objectType)
_ => throw new JsonException("Unexpected token type."),
};

/// <inheritdoc />
public override void Write(Utf8JsonWriter writer, decimal? value, JsonSerializerOptions options)
{
if (writer is null) throw new ArgumentNullException(nameof(writer));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ public JsonNullableDecimalRoundingConverter(int maximum)
Maximum = maximum;
}

/// <inheritdoc />
public override decimal? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> reader.TokenType == JsonTokenType.Number
? Math.Round(reader.GetDecimal(), Maximum)
: base.Read(ref reader, typeToConvert, options);

/// <inheritdoc />
public override void Write(Utf8JsonWriter writer, decimal? value, JsonSerializerOptions options)
{
if (value.HasValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ public JsonNullableDoubleRoundingConverter(int maximum)
Maximum = maximum;
}

/// <inheritdoc />
public override double? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> reader.TokenType == JsonTokenType.Number
? Math.Round(reader.GetDouble(), Maximum)
: base.Read(ref reader, typeToConvert, options);

/// <inheritdoc />
public override void Write(Utf8JsonWriter writer, double? value, JsonSerializerOptions options)
{
if (value.HasValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@

namespace Open.Serialization.Json.System.Converters;

/// <summary>Base class for other json value converters.</summary>
/// <inheritdoc />
public abstract class JsonValueConverterBase<T> : JsonConverter<T>
{
/// <inheritdoc />
public override bool CanConvert(Type objectType)
=> objectType == typeof(T);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
Expand All @@ -44,9 +44,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.6" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Open.Serialization.Json" Version="2.3.1" />
<PackageReference Include="System.Text.Json" Version="5.0.2" />
<PackageReference Include="System.Text.Json" Version="6.0.4" />
</ItemGroup>

</Project>
3 changes: 3 additions & 0 deletions Open.Serialization.Json.System/RelaxedJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ static readonly JsonSerializerOptions DeserializerOptions

public static IJsonDeserialize<TValue> GetDeserializer<TValue>()
=> DeserializerOptions.GetSerializer<TValue>();

public static IJsonDeserialize GetDeserializer()
=> DeserializerOptions.GetSerializer();

public static TValue Deserialize<TValue>(string? value)
=> DeserializerOptions.Deserialize<TValue>(value!);

public static TValue Deserialize<TValue>(ReadOnlySpan<byte> value)
=> DeserializerOptions.Deserialize<TValue>(value);
}
Loading

0 comments on commit fc9822c

Please sign in to comment.