Skip to content

Commit 4a8b8c4

Browse files
Add Microsoft.CodeAnalysis.FxCopAnalyzers
1 parent a297cae commit 4a8b8c4

6 files changed

Lines changed: 35 additions & 5 deletions

System.Text.Json.Extensions/Converters/KeyValuePairCollectionConverter.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ public override TCollection Read(ref Utf8JsonReader reader, Type typeToConvert,
4141

4242
public override void Write(Utf8JsonWriter writer, TCollection collection, JsonSerializerOptions options)
4343
{
44+
if (writer == null)
45+
{
46+
throw new ArgumentNullException(nameof(writer));
47+
}
48+
4449
if (collection == null)
4550
{
4651
writer.WriteNullValue();

System.Text.Json.Extensions/Converters/KeyValuePairCollectionConverterFactory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ namespace System.Text.Json.Extensions.Converters
55
{
66
public class KeyValuePairCollectionConverterFactory : JsonConverterFactory
77
{
8-
public Type GetValueType(Type typeToConvert)
8+
public static Type GetValueType(Type typeToConvert)
99
{
10-
if (typeToConvert.GetConstructor(new Type[0]) == null)
10+
if (typeToConvert?.GetConstructor(Array.Empty<Type>()) == null)
1111
{
1212
return null;
1313
}

System.Text.Json.Extensions/JsonSerializerOptionsExtensions.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,27 @@ public static class JsonSerializerOptionsExtensions
44
{
55
public static JsonReaderOptions GetReaderOptions(this JsonSerializerOptions jsonSerializerOptions)
66
{
7+
if (jsonSerializerOptions == null)
8+
{
9+
throw new ArgumentNullException(nameof(jsonSerializerOptions));
10+
}
11+
712
return new JsonReaderOptions
813
{
914
CommentHandling = jsonSerializerOptions.ReadCommentHandling,
1015
AllowTrailingCommas = jsonSerializerOptions.AllowTrailingCommas,
1116
MaxDepth = jsonSerializerOptions.MaxDepth
1217
};
1318
}
14-
15-
public static JsonWriterOptions GetWriterOptions(this JsonSerializerOptions jsonSerializerOptions, bool skipValidation = true)
19+
20+
public static JsonWriterOptions GetWriterOptions(this JsonSerializerOptions jsonSerializerOptions,
21+
bool skipValidation = true)
1622
{
23+
if (jsonSerializerOptions == null)
24+
{
25+
throw new ArgumentNullException(nameof(jsonSerializerOptions));
26+
}
27+
1728
return new JsonWriterOptions
1829
{
1930
Encoder = jsonSerializerOptions.Encoder,

System.Text.Json.Extensions/System.Text.Json.Extensions.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8+
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.3.0">
9+
<PrivateAssets>all</PrivateAssets>
10+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
11+
</PackageReference>
812
<PackageReference Include="System.Text.Json" Version="4.7.2" />
913
</ItemGroup>
1014

System.Text.Json.Extensions/Utf8JsonReaderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static T ReadObject<T>(this ref Utf8JsonReader reader, JsonSerializerOpti
1616
{
1717
reader.Read();
1818
} while (reader.TokenType == JsonTokenType.Comment
19-
&& options.ReadCommentHandling == JsonCommentHandling.Skip);
19+
&& options?.ReadCommentHandling == JsonCommentHandling.Skip);
2020

2121
return GetObject<T>(ref reader, options);
2222
}

System.Text.Json.Extensions/Utf8JsonWriterExtensions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ public static class Utf8JsonWriterExtensions
1212
/// </summary>
1313
public static void WriteObject<T>(this Utf8JsonWriter writer, string propertyName, T value, JsonSerializerOptions options)
1414
{
15+
if (writer == null)
16+
{
17+
throw new ArgumentNullException(nameof(writer));
18+
}
19+
1520
writer.WritePropertyName(propertyName);
1621
writer.WriteObject(value, options);
1722
}
@@ -23,6 +28,11 @@ public static void WriteObject<T>(this Utf8JsonWriter writer, string propertyNam
2328
/// </summary>
2429
public static void WriteObject<T>(this Utf8JsonWriter writer, T value, JsonSerializerOptions options)
2530
{
31+
if (writer == null)
32+
{
33+
throw new ArgumentNullException(nameof(writer));
34+
}
35+
2636
var typeToConvert = typeof(T);
2737

2838
// Attempt to use existing converter first before re-entering through JsonSerializer.Serialize().

0 commit comments

Comments
 (0)