Skip to content

Commit 3069341

Browse files
authored
Merge pull request #1170 from json-api-dotnet/json-sourcegen
Minor serialization tweaks
2 parents 0185311 + f760e8f commit 3069341

24 files changed

+254
-253
lines changed

benchmarks/Benchmarks.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<OutputType>Exe</OutputType>
44
<TargetFramework>$(TargetFrameworkName)</TargetFramework>
5+
<ServerGarbageCollection>true</ServerGarbageCollection>
56
</PropertyGroup>
67

78
<ItemGroup>
@@ -10,6 +11,5 @@
1011

1112
<ItemGroup>
1213
<PackageReference Include="BenchmarkDotNet" Version="0.13.1" />
13-
<PackageReference Include="Moq" Version="$(MoqVersion)" />
1414
</ItemGroup>
1515
</Project>

benchmarks/Deserialization/OperationsDeserializationBenchmarks.cs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Benchmarks.Deserialization;
88

99
[MarkdownExporter]
10+
[MemoryDiagnoser]
1011
// ReSharper disable once ClassCanBeSealed.Global
1112
public class OperationsDeserializationBenchmarks : DeserializationBenchmarkBase
1213
{

benchmarks/Deserialization/ResourceDeserializationBenchmarks.cs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Benchmarks.Deserialization;
88

99
[MarkdownExporter]
10+
[MemoryDiagnoser]
1011
// ReSharper disable once ClassCanBeSealed.Global
1112
public class ResourceDeserializationBenchmarks : DeserializationBenchmarkBase
1213
{

benchmarks/QueryString/QueryStringParserBenchmarks.cs

+4-27
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
using System.ComponentModel.Design;
22
using BenchmarkDotNet.Attributes;
3+
using Benchmarks.Tools;
34
using JsonApiDotNetCore;
45
using JsonApiDotNetCore.Configuration;
56
using JsonApiDotNetCore.Middleware;
67
using JsonApiDotNetCore.QueryStrings;
78
using JsonApiDotNetCore.QueryStrings.Internal;
89
using JsonApiDotNetCore.Resources;
9-
using Microsoft.AspNetCore.Http;
10-
using Microsoft.AspNetCore.WebUtilities;
1110
using Microsoft.Extensions.Logging.Abstractions;
1211

1312
namespace Benchmarks.QueryString;
@@ -71,31 +70,9 @@ public void DescendingSort()
7170
[Benchmark]
7271
public void ComplexQuery()
7372
{
74-
Run(100, () =>
75-
{
76-
const string queryString =
77-
"?filter[alt-attr-name]=abc,eq:abc&sort=-alt-attr-name&include=child&page[size]=1&fields[alt-resource-name]=alt-attr-name";
78-
79-
_queryStringAccessor.SetQueryString(queryString);
80-
_queryStringReader.ReadAll(null);
81-
});
82-
}
83-
84-
private void Run(int iterations, Action action)
85-
{
86-
for (int index = 0; index < iterations; index++)
87-
{
88-
action();
89-
}
90-
}
91-
92-
private sealed class FakeRequestQueryStringAccessor : IRequestQueryStringAccessor
93-
{
94-
public IQueryCollection Query { get; private set; } = new QueryCollection();
73+
const string queryString = "?filter[alt-attr-name]=abc,eq:abc&sort=-alt-attr-name&include=child&page[size]=1&fields[alt-resource-name]=alt-attr-name";
9574

96-
public void SetQueryString(string queryString)
97-
{
98-
Query = new QueryCollection(QueryHelpers.ParseQuery(queryString));
99-
}
75+
_queryStringAccessor.SetQueryString(queryString);
76+
_queryStringReader.ReadAll(null);
10077
}
10178
}

benchmarks/Serialization/OperationsSerializationBenchmarks.cs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace Benchmarks.Serialization;
1010

1111
[MarkdownExporter]
12+
[MemoryDiagnoser]
1213
// ReSharper disable once ClassCanBeSealed.Global
1314
public class OperationsSerializationBenchmarks : SerializationBenchmarkBase
1415
{

benchmarks/Serialization/ResourceSerializationBenchmarks.cs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Benchmarks.Serialization;
1313

1414
[MarkdownExporter]
15+
[MemoryDiagnoser]
1516
// ReSharper disable once ClassCanBeSealed.Global
1617
public class ResourceSerializationBenchmarks : SerializationBenchmarkBase
1718
{
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1-
using System.Collections.Immutable;
21
using System.Text.Json;
32
using System.Text.Json.Serialization;
3+
using Benchmarks.Tools;
44
using JetBrains.Annotations;
55
using JsonApiDotNetCore.Configuration;
66
using JsonApiDotNetCore.Middleware;
77
using JsonApiDotNetCore.Queries;
8-
using JsonApiDotNetCore.Queries.Expressions;
98
using JsonApiDotNetCore.Queries.Internal;
10-
using JsonApiDotNetCore.QueryStrings;
119
using JsonApiDotNetCore.Resources;
1210
using JsonApiDotNetCore.Resources.Annotations;
13-
using JsonApiDotNetCore.Serialization.Objects;
1411
using JsonApiDotNetCore.Serialization.Response;
15-
using Microsoft.AspNetCore.Http;
1612
using Microsoft.Extensions.Logging.Abstractions;
1713

1814
namespace Benchmarks.Serialization;
@@ -45,9 +41,9 @@ protected SerializationBenchmarkBase()
4541
// ReSharper restore VirtualMemberCallInConstructor
4642

4743
var linkBuilder = new FakeLinkBuilder();
48-
var metaBuilder = new FakeMetaBuilder();
44+
var metaBuilder = new NoMetaBuilder();
4945
IQueryConstraintProvider[] constraintProviders = Array.Empty<IQueryConstraintProvider>();
50-
var resourceDefinitionAccessor = new FakeResourceDefinitionAccessor();
46+
var resourceDefinitionAccessor = new NeverResourceDefinitionAccessor();
5147
var sparseFieldSetCache = new SparseFieldSetCache(constraintProviders, resourceDefinitionAccessor);
5248
var requestQueryStringAccessor = new FakeRequestQueryStringAccessor();
5349

@@ -122,141 +118,4 @@ public sealed class OutgoingResource : Identifiable<int>
122118
[HasMany]
123119
public ISet<OutgoingResource> Multi5 { get; set; } = null!;
124120
}
125-
126-
private sealed class FakeResourceDefinitionAccessor : IResourceDefinitionAccessor
127-
{
128-
public IImmutableSet<IncludeElementExpression> OnApplyIncludes(ResourceType resourceType, IImmutableSet<IncludeElementExpression> existingIncludes)
129-
{
130-
return existingIncludes;
131-
}
132-
133-
public FilterExpression? OnApplyFilter(ResourceType resourceType, FilterExpression? existingFilter)
134-
{
135-
return existingFilter;
136-
}
137-
138-
public SortExpression? OnApplySort(ResourceType resourceType, SortExpression? existingSort)
139-
{
140-
return existingSort;
141-
}
142-
143-
public PaginationExpression? OnApplyPagination(ResourceType resourceType, PaginationExpression? existingPagination)
144-
{
145-
return existingPagination;
146-
}
147-
148-
public SparseFieldSetExpression? OnApplySparseFieldSet(ResourceType resourceType, SparseFieldSetExpression? existingSparseFieldSet)
149-
{
150-
return existingSparseFieldSet;
151-
}
152-
153-
public object? GetQueryableHandlerForQueryStringParameter(Type resourceClrType, string parameterName)
154-
{
155-
return null;
156-
}
157-
158-
public IDictionary<string, object?>? GetMeta(ResourceType resourceType, IIdentifiable resourceInstance)
159-
{
160-
return null;
161-
}
162-
163-
public Task OnPrepareWriteAsync<TResource>(TResource resource, WriteOperationKind writeOperation, CancellationToken cancellationToken)
164-
where TResource : class, IIdentifiable
165-
{
166-
return Task.CompletedTask;
167-
}
168-
169-
public Task<IIdentifiable?> OnSetToOneRelationshipAsync<TResource>(TResource leftResource, HasOneAttribute hasOneRelationship,
170-
IIdentifiable? rightResourceId, WriteOperationKind writeOperation, CancellationToken cancellationToken)
171-
where TResource : class, IIdentifiable
172-
{
173-
return Task.FromResult(rightResourceId);
174-
}
175-
176-
public Task OnSetToManyRelationshipAsync<TResource>(TResource leftResource, HasManyAttribute hasManyRelationship, ISet<IIdentifiable> rightResourceIds,
177-
WriteOperationKind writeOperation, CancellationToken cancellationToken)
178-
where TResource : class, IIdentifiable
179-
{
180-
return Task.CompletedTask;
181-
}
182-
183-
public Task OnAddToRelationshipAsync<TResource>(TResource leftResource, HasManyAttribute hasManyRelationship, ISet<IIdentifiable> rightResourceIds,
184-
CancellationToken cancellationToken)
185-
where TResource : class, IIdentifiable
186-
{
187-
return Task.CompletedTask;
188-
}
189-
190-
public Task OnRemoveFromRelationshipAsync<TResource>(TResource leftResource, HasManyAttribute hasManyRelationship, ISet<IIdentifiable> rightResourceIds,
191-
CancellationToken cancellationToken)
192-
where TResource : class, IIdentifiable
193-
{
194-
return Task.CompletedTask;
195-
}
196-
197-
public Task OnWritingAsync<TResource>(TResource resource, WriteOperationKind writeOperation, CancellationToken cancellationToken)
198-
where TResource : class, IIdentifiable
199-
{
200-
return Task.CompletedTask;
201-
}
202-
203-
public Task OnWriteSucceededAsync<TResource>(TResource resource, WriteOperationKind writeOperation, CancellationToken cancellationToken)
204-
where TResource : class, IIdentifiable
205-
{
206-
return Task.CompletedTask;
207-
}
208-
209-
public void OnDeserialize(IIdentifiable resource)
210-
{
211-
}
212-
213-
public void OnSerialize(IIdentifiable resource)
214-
{
215-
}
216-
}
217-
218-
private sealed class FakeLinkBuilder : ILinkBuilder
219-
{
220-
public TopLevelLinks GetTopLevelLinks()
221-
{
222-
return new TopLevelLinks
223-
{
224-
Self = "TopLevel:Self"
225-
};
226-
}
227-
228-
public ResourceLinks GetResourceLinks(ResourceType resourceType, IIdentifiable resource)
229-
{
230-
return new ResourceLinks
231-
{
232-
Self = "Resource:Self"
233-
};
234-
}
235-
236-
public RelationshipLinks GetRelationshipLinks(RelationshipAttribute relationship, IIdentifiable leftResource)
237-
{
238-
return new RelationshipLinks
239-
{
240-
Self = "Relationship:Self",
241-
Related = "Relationship:Related"
242-
};
243-
}
244-
}
245-
246-
private sealed class FakeMetaBuilder : IMetaBuilder
247-
{
248-
public void Add(IDictionary<string, object?> values)
249-
{
250-
}
251-
252-
public IDictionary<string, object?>? Build()
253-
{
254-
return null;
255-
}
256-
}
257-
258-
private sealed class FakeRequestQueryStringAccessor : IRequestQueryStringAccessor
259-
{
260-
public IQueryCollection Query { get; } = new QueryCollection(0);
261-
}
262121
}

benchmarks/Tools/FakeLinkBuilder.cs

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using JsonApiDotNetCore.Configuration;
2+
using JsonApiDotNetCore.Resources;
3+
using JsonApiDotNetCore.Resources.Annotations;
4+
using JsonApiDotNetCore.Serialization.Objects;
5+
using JsonApiDotNetCore.Serialization.Response;
6+
using Microsoft.AspNetCore.Http;
7+
8+
namespace Benchmarks.Tools;
9+
10+
/// <summary>
11+
/// Renders hard-coded fake links, without depending on <see cref="HttpContext" />.
12+
/// </summary>
13+
internal sealed class FakeLinkBuilder : ILinkBuilder
14+
{
15+
public TopLevelLinks GetTopLevelLinks()
16+
{
17+
return new TopLevelLinks
18+
{
19+
Self = "TopLevel:Self"
20+
};
21+
}
22+
23+
public ResourceLinks GetResourceLinks(ResourceType resourceType, IIdentifiable resource)
24+
{
25+
return new ResourceLinks
26+
{
27+
Self = "Resource:Self"
28+
};
29+
}
30+
31+
public RelationshipLinks GetRelationshipLinks(RelationshipAttribute relationship, IIdentifiable leftResource)
32+
{
33+
return new RelationshipLinks
34+
{
35+
Self = "Relationship:Self",
36+
Related = "Relationship:Related"
37+
};
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using JsonApiDotNetCore.QueryStrings;
2+
using Microsoft.AspNetCore.Http;
3+
using Microsoft.AspNetCore.WebUtilities;
4+
5+
namespace Benchmarks.Tools;
6+
7+
/// <summary>
8+
/// Enables to inject a query string, instead of obtaining it from <see cref="HttpContext" />.
9+
/// </summary>
10+
internal sealed class FakeRequestQueryStringAccessor : IRequestQueryStringAccessor
11+
{
12+
public IQueryCollection Query { get; private set; } = new QueryCollection();
13+
14+
public void SetQueryString(string queryString)
15+
{
16+
Query = new QueryCollection(QueryHelpers.ParseQuery(queryString));
17+
}
18+
}

0 commit comments

Comments
 (0)