Skip to content

Commit 356475d

Browse files
author
Oren (electricessence)
committed
Improved Nullable support. Release 1.4.0.
1 parent 48cc94f commit 356475d

40 files changed

Lines changed: 288 additions & 120 deletions
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[*.cs]
2+
3+
# CA1303: Do not pass literals as localized parameters
4+
dotnet_diagnostic.CA1303.severity = silent
5+
6+
# CA1062: Validate arguments of public methods
7+
dotnet_diagnostic.CA1062.severity = suggestion
8+
9+
# CA1305: Specify IFormatProvider
10+
dotnet_diagnostic.CA1305.severity = silent
11+
12+
# CA1307: Specify StringComparison
13+
dotnet_diagnostic.CA1307.severity = suggestion

Open.Serialization.Json.Newtonsoft/Converters/JsonDecimalConverter.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Newtonsoft.Json;
22
using System;
3+
using System.Diagnostics.Contracts;
34

45
namespace Open.Serialization.Json.Newtonsoft.Converters
56
{
@@ -17,13 +18,24 @@ public static readonly JsonDecimalConverter Instance
1718
=> Normalize(value?.ToString());
1819

1920
public static string? Normalize(string? decimalString)
20-
=> decimalString?.IndexOf('.') == -1 ? decimalString
21+
=> decimalString == null || decimalString.IndexOf('.') == -1
22+
? decimalString
2123
: decimalString?.TrimEnd('0').TrimEnd('.');
2224

2325
public override decimal ReadJson(JsonReader reader, Type objectType, decimal existingValue, bool hasExistingValue, JsonSerializer serializer)
24-
=> Convert.ToDecimal(reader.Value);
26+
{
27+
if (reader is null) throw new ArgumentNullException(nameof(reader));
28+
Contract.EndContractBlock();
29+
30+
return Convert.ToDecimal(reader.Value);
31+
}
2532

2633
public override void WriteJson(JsonWriter writer, decimal value, JsonSerializer serializer)
27-
=> writer.WriteRawValue(Normalize(value));
34+
{
35+
if (writer is null) throw new ArgumentNullException(nameof(writer));
36+
Contract.EndContractBlock();
37+
38+
writer.WriteRawValue(Normalize(value));
39+
}
2840
}
2941
}

Open.Serialization.Json.Newtonsoft/Converters/JsonDecimalRoundingConverter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
using System;
2-
using Newtonsoft.Json;
1+
using Newtonsoft.Json;
2+
using System;
33

44
namespace Open.Serialization.Json.Newtonsoft.Converters
55
{
66
public class JsonDecimalRoundingConverter : JsonDecimalConverter
77
{
8-
public readonly int Maximum;
8+
public int Maximum { get; }
99
public JsonDecimalRoundingConverter(int maximum)
1010
{
1111
if (maximum < 0)
Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
using System;
2-
using Newtonsoft.Json;
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Diagnostics.Contracts;
34

45
namespace Open.Serialization.Json.Newtonsoft.Converters
56
{
67
public class JsonDoubleRoundingConverter : JsonValueConverterBase<double>
78
{
8-
public readonly int Maximum;
9+
public int Maximum { get; }
910
public JsonDoubleRoundingConverter(int maximum)
1011
{
1112
if (maximum < 0)
@@ -14,11 +15,21 @@ public JsonDoubleRoundingConverter(int maximum)
1415
}
1516

1617
public override double ReadJson(JsonReader reader, Type objectType, double existingValue, bool hasExistingValue, JsonSerializer serializer)
17-
=> reader.Value is decimal d
18+
{
19+
if (reader is null) throw new ArgumentNullException(nameof(reader));
20+
Contract.EndContractBlock();
21+
22+
return reader.Value is decimal d
1823
? Convert.ToDouble(Math.Round(d, Maximum))
1924
: Math.Round(Convert.ToDouble(reader.Value), Maximum);
25+
}
2026

2127
public override void WriteJson(JsonWriter writer, double value, JsonSerializer serializer)
22-
=> writer.WriteValue(Math.Round(value, Maximum));
28+
{
29+
if (writer is null) throw new ArgumentNullException(nameof(writer));
30+
Contract.EndContractBlock();
31+
32+
writer.WriteValue(Math.Round(value, Maximum));
33+
}
2334
}
2435
}

Open.Serialization.Json.Newtonsoft/Converters/JsonNullableDecimalConverter.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
using System;
2-
using Newtonsoft.Json;
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Diagnostics.Contracts;
34

45
namespace Open.Serialization.Json.Newtonsoft.Converters
56
{
@@ -14,14 +15,24 @@ public static readonly JsonNullableDecimalConverter Instance
1415
= new JsonNullableDecimalConverter();
1516

1617
public override decimal? ReadJson(JsonReader reader, Type objectType, decimal? existingValue, bool hasExistingValue, JsonSerializer serializer)
17-
=> reader.TokenType switch
18+
{
19+
if (reader is null) throw new ArgumentNullException(nameof(reader));
20+
Contract.EndContractBlock();
21+
22+
return reader.TokenType switch
1823
{
1924
JsonToken.Null => default,
2025
JsonToken.Undefined => default,
2126
_ => Convert.ToDecimal(reader.Value),
2227
};
28+
}
2329

24-
public override void WriteJson(JsonWriter writer, decimal? value, JsonSerializer serializer)
25-
=> writer.WriteRawValue(JsonDecimalConverter.Normalize(value));
30+
public override void WriteJson(JsonWriter writer, decimal? value, JsonSerializer serializer)
31+
{
32+
if (writer is null) throw new ArgumentNullException(nameof(writer));
33+
Contract.EndContractBlock();
34+
35+
writer.WriteRawValue(JsonDecimalConverter.Normalize(value));
36+
}
2637
}
2738
}

Open.Serialization.Json.Newtonsoft/Converters/JsonNullableDecimalRoundingConverter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
using System;
2-
using Newtonsoft.Json;
1+
using Newtonsoft.Json;
2+
using System;
33

44
namespace Open.Serialization.Json.Newtonsoft.Converters
55
{
66
public class JsonNullableDecimalRoundingConverter : JsonNullableDecimalConverter
77
{
8-
public readonly int Maximum;
8+
public int Maximum { get; }
99
public JsonNullableDecimalRoundingConverter(int maximum)
1010
{
1111
if (maximum < 0)

Open.Serialization.Json.Newtonsoft/Converters/JsonNullableDoubleConverter.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
using System;
2-
using Newtonsoft.Json;
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Diagnostics.Contracts;
34

45
namespace Open.Serialization.Json.Newtonsoft.Converters
56
{
@@ -15,15 +16,23 @@ public static readonly JsonNullableDoubleConverter Instance
1516

1617

1718
public override double? ReadJson(JsonReader reader, Type objectType, double? existingValue, bool hasExistingValue, JsonSerializer serializer)
18-
=> reader.TokenType switch
19+
{
20+
if (reader is null) throw new ArgumentNullException(nameof(reader));
21+
Contract.EndContractBlock();
22+
23+
return reader.TokenType switch
1924
{
2025
JsonToken.Null => default,
2126
JsonToken.Undefined => default,
2227
_ => Convert.ToDouble(reader.Value)
2328
};
29+
}
2430

2531
public override void WriteJson(JsonWriter writer, double? value, JsonSerializer serializer)
2632
{
33+
if (writer is null) throw new ArgumentNullException(nameof(writer));
34+
Contract.EndContractBlock();
35+
2736
if (value.HasValue) writer.WriteValue(value.Value);
2837
else writer.WriteRawValue(null);
2938
}

Open.Serialization.Json.Newtonsoft/Converters/JsonNullableDoubleRoundingConverter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
using System;
2-
using Newtonsoft.Json;
1+
using Newtonsoft.Json;
2+
using System;
33

44
namespace Open.Serialization.Json.Newtonsoft.Converters
55
{
66
public class JsonNullableDoubleRoundingConverter : JsonNullableDoubleConverter
77
{
8-
public readonly int Maximum;
8+
public int Maximum { get; }
99
public JsonNullableDoubleRoundingConverter(int maximum)
1010
{
1111
if (maximum < 0)

Open.Serialization.Json.Newtonsoft/Converters/JsonValueConverterBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
using System;
2-
using Newtonsoft.Json;
1+
using Newtonsoft.Json;
2+
using System;
33

44
namespace Open.Serialization.Json.Newtonsoft.Converters
55
{

Open.Serialization.Json.Newtonsoft/Extensions.cs

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
using System;
1+
using Newtonsoft.Json;
2+
using Open.Serialization.Json.Newtonsoft.Converters;
3+
using System;
24
using System.Collections.Generic;
5+
using System.Diagnostics.Contracts;
36
using System.Linq;
4-
using Newtonsoft.Json;
5-
using Open.Serialization.Json.Newtonsoft.Converters;
67

78
namespace Open.Serialization.Json.Newtonsoft
89
{
910
public static class Extensions
1011
{
11-
public static Func<string, T> GetDeserialize<T>(this JsonSerializerSettings settings)
12+
public static Func<string?, T> GetDeserialize<T>(this JsonSerializerSettings settings)
1213
=> json => JsonConvert.DeserializeObject<T>(json, settings);
1314

14-
public static Func<T, string> GetSerialize<T>(this JsonSerializerSettings settings)
15+
public static Func<T, string?> GetSerialize<T>(this JsonSerializerSettings settings)
1516
=> item => JsonConvert.SerializeObject(item, settings);
1617

17-
public static Func<object, string> GetSerialize(this JsonSerializerSettings settings)
18+
public static Func<object?, string?> GetSerialize(this JsonSerializerSettings settings)
1819
=> item => JsonConvert.SerializeObject(item, settings);
1920

2021
public static IJsonSerializer GetSerializer(this JsonSerializerSettings settings)
@@ -26,15 +27,18 @@ public static IJsonSerializer<T> GetSerializer<T>(this JsonSerializerSettings se
2627
public static IJsonSerializerFactory GetSerializerFactory(this JsonSerializerSettings settings)
2728
=> new JsonSerializerFactory(settings);
2829

29-
public static string Serialize<TValue>(this JsonSerializerSettings settings, TValue value)
30+
public static string? Serialize<TValue>(this JsonSerializerSettings settings, TValue value)
3031
=> JsonConvert.SerializeObject(value, settings);
31-
public static string Serialize(this JsonSerializerSettings settings, object value)
32+
public static string? Serialize(this JsonSerializerSettings settings, object? value)
3233
=> JsonConvert.SerializeObject(value, settings);
33-
public static TValue Deserialize<TValue>(this JsonSerializerSettings settings, string value)
34+
public static TValue Deserialize<TValue>(this JsonSerializerSettings settings, string? value)
3435
=> JsonConvert.DeserializeObject<TValue>(value, settings);
3536

3637
public static JsonSerializerSettings Clone(this JsonSerializerSettings settings)
3738
{
39+
if (settings == null) throw new ArgumentNullException(nameof(settings));
40+
Contract.EndContractBlock();
41+
3842
var clone = new JsonSerializerSettings
3943
{
4044
StringEscapeHandling = settings.StringEscapeHandling,
@@ -76,12 +80,18 @@ public static JsonSerializerSettings Clone(this JsonSerializerSettings settings)
7680

7781
public static JsonSerializerSettings SetNullValueHandling(this JsonSerializerSettings settings, NullValueHandling value)
7882
{
83+
if (settings == null) throw new ArgumentNullException(nameof(settings));
84+
Contract.EndContractBlock();
85+
7986
settings.NullValueHandling = value;
8087
return settings;
8188
}
8289

8390
public static JsonSerializerSettings AddConverter(this JsonSerializerSettings settings, JsonConverter converter)
8491
{
92+
if (settings == null) throw new ArgumentNullException(nameof(settings));
93+
Contract.EndContractBlock();
94+
8595
settings.Converters.Add(converter);
8696
return settings;
8797
}
@@ -112,12 +122,20 @@ static JsonSerializerSettings RoundNullableDoublesCore(this JsonSerializerSettin
112122
}
113123

114124
public static JsonSerializerSettings RoundDoubles(this JsonSerializerSettings settings, int maxDecimals)
115-
=> settings
125+
{
126+
if (settings == null) throw new ArgumentNullException(nameof(settings));
127+
Contract.EndContractBlock();
128+
129+
return settings
116130
.RoundDoublesCore(maxDecimals)
117131
.RoundNullableDoublesCore(maxDecimals);
132+
}
118133

119134
public static JsonSerializerSettings NormalizeDecimals(this JsonSerializerSettings settings)
120135
{
136+
if (settings == null) throw new ArgumentNullException(nameof(settings));
137+
Contract.EndContractBlock();
138+
121139
JsonConverter? existing = settings.Converters.FirstOrDefault(c => c is JsonConverter<decimal>);
122140
var existingNullable = settings.Converters.FirstOrDefault(c => c is JsonConverter<decimal?>);
123141

@@ -166,8 +184,13 @@ static JsonSerializerSettings RoundNullableDecimalsCore(this JsonSerializerSetti
166184
}
167185

168186
public static JsonSerializerSettings RoundDecimals(this JsonSerializerSettings settings, int maxDecimals)
169-
=> settings
187+
{
188+
if (settings == null) throw new ArgumentNullException(nameof(settings));
189+
Contract.EndContractBlock();
190+
191+
return settings
170192
.RoundDecimalsCore(maxDecimals)
171193
.RoundNullableDecimalsCore(maxDecimals);
194+
}
172195
}
173196
}

0 commit comments

Comments
 (0)