Skip to content

Commit 7751d0c

Browse files
committed
reafactor: change Newtonsoft.Json by System.Text.Json
Remove generic exception encapsulation
1 parent 303f853 commit 7751d0c

File tree

4 files changed

+15
-24
lines changed

4 files changed

+15
-24
lines changed

src/Voxel.MiddyNet.HttpJsonBodyParserMiddleware/HttpJsonBodyParserMiddleware.cs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Text;
3+
using System.Text.Json;
34
using Amazon.Lambda.APIGatewayEvents;
4-
using Newtonsoft.Json;
55
using System.Threading.Tasks;
66

77
namespace Voxel.MiddyNet.HttpJsonBodyParserMiddleware
@@ -20,16 +20,9 @@ public Task Before(APIGatewayProxyRequest lambdaEvent, MiddyNetContext context)
2020
{
2121
lambdaEvent.Body = Encoding.UTF8.GetString(Convert.FromBase64String(lambdaEvent.Body));
2222
}
23-
24-
T source;
25-
try
26-
{
27-
source = JsonConvert.DeserializeObject<T>(lambdaEvent.Body);
28-
}
29-
catch (JsonReaderException)
30-
{
31-
throw new Exception("Content type defined as JSON but an invalid JSON was provided");
32-
}
23+
24+
var source = JsonSerializer.Deserialize<T>(lambdaEvent.Body);
25+
3326

3427
context.AdditionalContext.Add("Body", source);
3528
return Task.CompletedTask;

src/Voxel.MiddyNet.HttpJsonBodyParserMiddleware/Voxel.MiddyNet.HttpJsonBodyParserMiddleware.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
<ItemGroup>
88
<PackageReference Include="Amazon.Lambda.APIGatewayEvents" Version="2.4.0" />
9-
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
109
</ItemGroup>
1110

1211
<ItemGroup>

test/Voxel.MiddyNet.HttpJsonBodyParserMiddleware.Tests/HttpJsonBodyParserMiddlewareShould.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Text;
4+
using System.Text.Json;
45
using System.Threading.Tasks;
56
using Amazon.Lambda.APIGatewayEvents;
67
using Amazon.Lambda.Core;
78
using FluentAssertions;
8-
using Newtonsoft.Json;
99
using NSubstitute;
1010
using Xunit;
1111

@@ -20,8 +20,11 @@ public class HttpJsonBodyParserMiddlewareShould
2020
public HttpJsonBodyParserMiddlewareShould()
2121
{
2222
context = new MiddyNetContext(Substitute.For<ILambdaContext>());
23-
expectation = new TestObject("bar");
24-
serializedExpectation = JsonConvert.SerializeObject(expectation);
23+
expectation = new TestObject
24+
{
25+
foo = "bar"
26+
};
27+
serializedExpectation = JsonSerializer.Serialize(expectation);
2528
}
2629

2730
[Fact]
@@ -51,7 +54,7 @@ public void ErrorWhenJsonNotMapsToObject()
5154
var middleware = new HttpJsonBodyParserMiddleware<TestObject>();
5255
Action action = () => middleware.Before(request, context);
5356

54-
action.Should().Throw<Exception>().WithMessage("Content type defined as JSON but an invalid JSON was provided");
57+
action.Should().Throw<Exception>().WithMessage("'M' is an invalid start of a value.*");
5558
}
5659

5760
[Fact]
@@ -72,7 +75,7 @@ public async Task NotProcessTheBodyIfNoHeaderIsPassed()
7275
public async Task HandleABase64Body()
7376
{
7477
string base64Serialized = Convert.ToBase64String(Encoding.UTF8.GetBytes(serializedExpectation));
75-
78+
7679
var request = new APIGatewayProxyRequest()
7780
{
7881
Headers = new Dictionary<string, string> { { "Content-Type", "application/json" } },
@@ -103,7 +106,7 @@ public void HandleInvalidBase64Body()
103106
var middleware = new HttpJsonBodyParserMiddleware<TestObject>();
104107
Action action = () => middleware.Before(request, context);
105108

106-
action.Should().Throw<Exception>().WithMessage("Content type defined as JSON but an invalid JSON was provided");
109+
action.Should().Throw<Exception>().WithMessage("'M' is an invalid start of a value.*");
107110
}
108111
}
109112
}

test/Voxel.MiddyNet.HttpJsonBodyParserMiddleware.Tests/TestObject.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@ namespace Voxel.MiddyNet.HttpJsonBodyParserMiddleware.Tests
22
{
33
public class TestObject
44
{
5-
public string foo { get; }
6-
7-
public TestObject(string foo)
8-
{
9-
this.foo = foo;
10-
}
5+
public string foo { get; set; }
6+
117
}
128
}

0 commit comments

Comments
 (0)