Skip to content

Commit 851070c

Browse files
committed
Add Jil JSON test
This moves the JSON.Net test to /json/newtonsoft (to match the other multi-implementation test patterns) and adds /json/jil as another JSON serialization test.
1 parent e9f829e commit 851070c

File tree

4 files changed

+65
-8
lines changed

4 files changed

+65
-8
lines changed

src/Benchmarks/JsonJilMiddleware.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.IO;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using Microsoft.AspNet.Builder;
9+
using Microsoft.AspNet.Http;
10+
using Jil;
11+
12+
namespace Benchmarks
13+
{
14+
public class JsonJilMiddleware
15+
{
16+
private static readonly Task _done = Task.FromResult(0);
17+
private static readonly PathString _path = new PathString("/json/jil");
18+
19+
private readonly RequestDelegate _next;
20+
21+
public JsonJilMiddleware(RequestDelegate next)
22+
{
23+
_next = next;
24+
}
25+
26+
public Task Invoke(HttpContext httpContext)
27+
{
28+
// We check Ordinal explicitly first because it's faster than OrdinalIgnoreCase
29+
if (httpContext.Request.Path.StartsWithSegments(_path, StringComparison.Ordinal) ||
30+
httpContext.Request.Path.StartsWithSegments(_path, StringComparison.OrdinalIgnoreCase))
31+
{
32+
httpContext.Response.StatusCode = 200;
33+
httpContext.Response.ContentType = "application/json";
34+
httpContext.Response.ContentLength = 30;
35+
36+
using (var sw = new StreamWriter(httpContext.Response.Body, Encoding.UTF8, bufferSize: 30))
37+
{
38+
JSON.Serialize(new { message = "Hello, World!" }, sw);
39+
}
40+
41+
return _done;
42+
}
43+
44+
return _next(httpContext);
45+
}
46+
}
47+
48+
public static class JsonJilMiddlewareExtensions
49+
{
50+
public static IApplicationBuilder UseJilJson(this IApplicationBuilder builder)
51+
{
52+
return builder.UseMiddleware<JsonJilMiddleware>();
53+
}
54+
}
55+
}

src/Benchmarks/JsonMiddleware.cs renamed to src/Benchmarks/JsonNewtonsoftMiddleware.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111

1212
namespace Benchmarks
1313
{
14-
public class JsonMiddleware
14+
public class JsonNewtonsoftMiddleware
1515
{
1616
private static readonly Task _done = Task.FromResult(0);
17-
private static readonly PathString _path = new PathString("/json");
17+
private static readonly PathString _path = new PathString("/json/newtonsoft");
1818
private static readonly JsonSerializer _json = new JsonSerializer();
1919

2020
private readonly RequestDelegate _next;
2121

22-
public JsonMiddleware(RequestDelegate next)
22+
public JsonNewtonsoftMiddleware(RequestDelegate next)
2323
{
2424
_next = next;
2525
}
@@ -46,11 +46,11 @@ public Task Invoke(HttpContext httpContext)
4646
}
4747
}
4848

49-
public static class JsonMiddlewareExtensions
49+
public static class JsonNewtonsoftMiddlewareExtensions
5050
{
51-
public static IApplicationBuilder UseJson(this IApplicationBuilder builder)
51+
public static IApplicationBuilder UseNewtonsoftJson(this IApplicationBuilder builder)
5252
{
53-
return builder.UseMiddleware<JsonMiddleware>();
53+
return builder.UseMiddleware<JsonNewtonsoftMiddleware>();
5454
}
5555
}
5656
}

src/Benchmarks/Startup.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ public void Configure(IApplicationBuilder app)
6666
{
6767
app.UseErrorHandler();
6868
app.UsePlainText();
69-
app.UseJson();
69+
app.UseNewtonsoftJson();
70+
app.UseJilJson();
7071

7172
if (StartupOptions.EnableDbTests)
7273
{

src/Benchmarks/project.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"Microsoft.AspNet.Server.WebListener": "1.0.0-*",
1616
"Microsoft.AspNet.StaticFiles": "1.0.0-*",
1717
"Microsoft.Extensions.WebEncoders": "1.0.0-*",
18-
"Newtonsoft.Json": "7.0.1"
18+
"Newtonsoft.Json": "7.0.1",
19+
"Jil": "2.13.0-*"
1920
},
2021

2122
"commands": {

0 commit comments

Comments
 (0)