Skip to content

Commit 594b533

Browse files
author
Bart Koelman
committed
Review feedback: use base class instead of static helper
1 parent e655fa6 commit 594b533

6 files changed

+63
-68
lines changed

src/JsonApiDotNetCore/Serialization/JsonConverterSupport.cs

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/JsonApiDotNetCore/Serialization/JsonConverters/ResourceObjectConverter.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Collections.Generic;
33
using System.Reflection;
44
using System.Text.Json;
5-
using System.Text.Json.Serialization;
65
using JetBrains.Annotations;
76
using JsonApiDotNetCore.Configuration;
87
using JsonApiDotNetCore.Resources;
@@ -15,7 +14,7 @@ namespace JsonApiDotNetCore.Serialization.JsonConverters
1514
/// Converts <see cref="ResourceObject" /> to/from JSON.
1615
/// </summary>
1716
[UsedImplicitly(ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature)]
18-
public sealed class ResourceObjectConverter : JsonConverter<ResourceObject>
17+
public sealed class ResourceObjectConverter : JsonObjectConverter<ResourceObject>
1918
{
2019
private static readonly JsonEncodedText TypeText = JsonEncodedText.Encode("type");
2120
private static readonly JsonEncodedText IdText = JsonEncodedText.Encode("id");
@@ -72,7 +71,7 @@ public override ResourceObject Read(ref Utf8JsonReader reader, Type typeToConver
7271
{
7372
// Newtonsoft.Json used to auto-convert number to strings, while System.Text.Json does not. This is so likely
7473
// to hit users during upgrade that we special-case for this and produce a helpful error message.
75-
var jsonElement = JsonConverterSupport.ReadSubTree<JsonElement>(ref reader, options);
74+
var jsonElement = ReadSubTree<JsonElement>(ref reader, options);
7675
throw new JsonException($"Failed to convert ID '{jsonElement}' of type '{jsonElement.ValueKind}' to type 'String'.");
7776
}
7877

@@ -99,17 +98,17 @@ public override ResourceObject Read(ref Utf8JsonReader reader, Type typeToConver
9998
}
10099
case "relationships":
101100
{
102-
resourceObject.Relationships = JsonConverterSupport.ReadSubTree<IDictionary<string, RelationshipObject>>(ref reader, options);
101+
resourceObject.Relationships = ReadSubTree<IDictionary<string, RelationshipObject>>(ref reader, options);
103102
break;
104103
}
105104
case "links":
106105
{
107-
resourceObject.Links = JsonConverterSupport.ReadSubTree<ResourceLinks>(ref reader, options);
106+
resourceObject.Links = ReadSubTree<ResourceLinks>(ref reader, options);
108107
break;
109108
}
110109
case "meta":
111110
{
112-
resourceObject.Meta = JsonConverterSupport.ReadSubTree<IDictionary<string, object>>(ref reader, options);
111+
resourceObject.Meta = ReadSubTree<IDictionary<string, object>>(ref reader, options);
113112
break;
114113
}
115114
default:
@@ -124,7 +123,7 @@ public override ResourceObject Read(ref Utf8JsonReader reader, Type typeToConver
124123
}
125124
}
126125

127-
throw JsonConverterSupport.GetEndOfStreamError();
126+
throw GetEndOfStreamError();
128127
}
129128

130129
private static string TryPeekType(ref Utf8JsonReader reader)
@@ -196,7 +195,7 @@ private static IDictionary<string, object> ReadAttributes(ref Utf8JsonReader rea
196195
// Inside a JsonConverter there is no way to know where in the JSON object tree we are. And the serializer
197196
// is unable to provide the correct position either. So we avoid an exception and postpone producing an error
198197
// response to the post-processing phase, by setting a sentinel value.
199-
var jsonElement = JsonConverterSupport.ReadSubTree<JsonElement>(ref reader, options);
198+
var jsonElement = ReadSubTree<JsonElement>(ref reader, options);
200199

201200
attributeValue = new JsonInvalidAttributeInfo(attributeName, property.PropertyType, jsonElement.ToString(),
202201
jsonElement.ValueKind);
@@ -215,7 +214,7 @@ private static IDictionary<string, object> ReadAttributes(ref Utf8JsonReader rea
215214
}
216215
}
217216

218-
throw JsonConverterSupport.GetEndOfStreamError();
217+
throw GetEndOfStreamError();
219218
}
220219

221220
/// <summary>
@@ -240,25 +239,25 @@ public override void Write(Utf8JsonWriter writer, ResourceObject value, JsonSeri
240239
if (!value.Attributes.IsNullOrEmpty())
241240
{
242241
writer.WritePropertyName(AttributesText);
243-
JsonConverterSupport.WriteSubTree(writer, value.Attributes, options);
242+
WriteSubTree(writer, value.Attributes, options);
244243
}
245244

246245
if (!value.Relationships.IsNullOrEmpty())
247246
{
248247
writer.WritePropertyName(RelationshipsText);
249-
JsonConverterSupport.WriteSubTree(writer, value.Relationships, options);
248+
WriteSubTree(writer, value.Relationships, options);
250249
}
251250

252251
if (value.Links != null && value.Links.HasValue())
253252
{
254253
writer.WritePropertyName(LinksText);
255-
JsonConverterSupport.WriteSubTree(writer, value.Links, options);
254+
WriteSubTree(writer, value.Links, options);
256255
}
257256

258257
if (!value.Meta.IsNullOrEmpty())
259258
{
260259
writer.WritePropertyName(MetaText);
261-
JsonConverterSupport.WriteSubTree(writer, value.Meta, options);
260+
WriteSubTree(writer, value.Meta, options);
262261
}
263262

264263
writer.WriteEndObject();

src/JsonApiDotNetCore/Serialization/JsonConverters/SingleOrManyDataConverterFactory.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializer
2828
return (JsonConverter)Activator.CreateInstance(converterType, BindingFlags.Instance | BindingFlags.Public, null, null, null);
2929
}
3030

31-
private sealed class SingleOrManyDataConverter<T> : JsonConverter<SingleOrManyData<T>>
31+
private sealed class SingleOrManyDataConverter<T> : JsonObjectConverter<SingleOrManyData<T>>
3232
where T : class, IResourceIdentity
3333
{
3434
public override SingleOrManyData<T> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions serializerOptions)
@@ -48,7 +48,7 @@ public override SingleOrManyData<T> Read(ref Utf8JsonReader reader, Type typeToC
4848
}
4949
case JsonTokenType.StartObject:
5050
{
51-
var resourceObject = JsonConverterSupport.ReadSubTree<T>(ref reader, serializerOptions);
51+
var resourceObject = ReadSubTree<T>(ref reader, serializerOptions);
5252
objects.Add(resourceObject);
5353
break;
5454
}
@@ -67,7 +67,7 @@ public override SingleOrManyData<T> Read(ref Utf8JsonReader reader, Type typeToC
6767

6868
public override void Write(Utf8JsonWriter writer, SingleOrManyData<T> value, JsonSerializerOptions options)
6969
{
70-
JsonConverterSupport.WriteSubTree(writer, value.Value, options);
70+
WriteSubTree(writer, value.Value, options);
7171
}
7272
}
7373
}

src/JsonApiDotNetCore/Serialization/JsonConverters/WriteOnlyDocumentConverter.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Text.Json;
3-
using System.Text.Json.Serialization;
43
using JetBrains.Annotations;
54
using JsonApiDotNetCore.Serialization.Objects;
65

@@ -10,7 +9,7 @@ namespace JsonApiDotNetCore.Serialization.JsonConverters
109
/// Converts <see cref="Document" /> to JSON.
1110
/// </summary>
1211
[UsedImplicitly(ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature)]
13-
public sealed class WriteOnlyDocumentConverter : JsonConverter<Document>
12+
public sealed class WriteOnlyDocumentConverter : JsonObjectConverter<Document>
1413
{
1514
private static readonly JsonEncodedText JsonApiText = JsonEncodedText.Encode("jsonapi");
1615
private static readonly JsonEncodedText LinksText = JsonEncodedText.Encode("links");
@@ -36,49 +35,49 @@ public override void Write(Utf8JsonWriter writer, Document value, JsonSerializer
3635
if (value.JsonApi != null)
3736
{
3837
writer.WritePropertyName(JsonApiText);
39-
JsonConverterSupport.WriteSubTree(writer, value.JsonApi, options);
38+
WriteSubTree(writer, value.JsonApi, options);
4039
}
4140

4241
if (value.Links != null && value.Links.HasValue())
4342
{
4443
writer.WritePropertyName(LinksText);
45-
JsonConverterSupport.WriteSubTree(writer, value.Links, options);
44+
WriteSubTree(writer, value.Links, options);
4645
}
4746

4847
if (value.Data.IsAssigned)
4948
{
5049
writer.WritePropertyName(DataText);
51-
JsonConverterSupport.WriteSubTree(writer, value.Data, options);
50+
WriteSubTree(writer, value.Data, options);
5251
}
5352

5453
if (!value.Operations.IsNullOrEmpty())
5554
{
5655
writer.WritePropertyName(AtomicOperationsText);
57-
JsonConverterSupport.WriteSubTree(writer, value.Operations, options);
56+
WriteSubTree(writer, value.Operations, options);
5857
}
5958

6059
if (!value.Results.IsNullOrEmpty())
6160
{
6261
writer.WritePropertyName(AtomicResultsText);
63-
JsonConverterSupport.WriteSubTree(writer, value.Results, options);
62+
WriteSubTree(writer, value.Results, options);
6463
}
6564

6665
if (!value.Errors.IsNullOrEmpty())
6766
{
6867
writer.WritePropertyName(ErrorsText);
69-
JsonConverterSupport.WriteSubTree(writer, value.Errors, options);
68+
WriteSubTree(writer, value.Errors, options);
7069
}
7170

7271
if (value.Included != null)
7372
{
7473
writer.WritePropertyName(IncludedText);
75-
JsonConverterSupport.WriteSubTree(writer, value.Included, options);
74+
WriteSubTree(writer, value.Included, options);
7675
}
7776

7877
if (!value.Meta.IsNullOrEmpty())
7978
{
8079
writer.WritePropertyName(MetaText);
81-
JsonConverterSupport.WriteSubTree(writer, value.Meta, options);
80+
WriteSubTree(writer, value.Meta, options);
8281
}
8382

8483
writer.WriteEndObject();

src/JsonApiDotNetCore/Serialization/JsonConverters/WriteOnlyRelationshipObjectConverter.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Text.Json;
3-
using System.Text.Json.Serialization;
43
using JetBrains.Annotations;
54
using JsonApiDotNetCore.Serialization.Objects;
65

@@ -10,7 +9,7 @@ namespace JsonApiDotNetCore.Serialization.JsonConverters
109
/// Converts <see cref="RelationshipObject" /> to JSON.
1110
/// </summary>
1211
[UsedImplicitly(ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature)]
13-
public sealed class WriteOnlyRelationshipObjectConverter : JsonConverter<RelationshipObject>
12+
public sealed class WriteOnlyRelationshipObjectConverter : JsonObjectConverter<RelationshipObject>
1413
{
1514
private static readonly JsonEncodedText DataText = JsonEncodedText.Encode("data");
1615
private static readonly JsonEncodedText LinksText = JsonEncodedText.Encode("links");
@@ -31,19 +30,19 @@ public override void Write(Utf8JsonWriter writer, RelationshipObject value, Json
3130
if (value.Links != null && value.Links.HasValue())
3231
{
3332
writer.WritePropertyName(LinksText);
34-
JsonConverterSupport.WriteSubTree(writer, value.Links, options);
33+
WriteSubTree(writer, value.Links, options);
3534
}
3635

3736
if (value.Data.IsAssigned)
3837
{
3938
writer.WritePropertyName(DataText);
40-
JsonConverterSupport.WriteSubTree(writer, value.Data, options);
39+
WriteSubTree(writer, value.Data, options);
4140
}
4241

4342
if (!value.Meta.IsNullOrEmpty())
4443
{
4544
writer.WritePropertyName(MetaText);
46-
JsonConverterSupport.WriteSubTree(writer, value.Meta, options);
45+
WriteSubTree(writer, value.Meta, options);
4746
}
4847

4948
writer.WriteEndObject();
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Text.Json;
2+
using System.Text.Json.Serialization;
3+
4+
namespace JsonApiDotNetCore.Serialization
5+
{
6+
public abstract class JsonObjectConverter<TObject> : JsonConverter<TObject>
7+
{
8+
protected static TValue ReadSubTree<TValue>(ref Utf8JsonReader reader, JsonSerializerOptions options)
9+
{
10+
if (typeof(TValue) != typeof(object) && options?.GetConverter(typeof(TValue)) is JsonConverter<TValue> converter)
11+
{
12+
return converter.Read(ref reader, typeof(TValue), options);
13+
}
14+
15+
return JsonSerializer.Deserialize<TValue>(ref reader, options);
16+
}
17+
18+
protected static void WriteSubTree<TValue>(Utf8JsonWriter writer, TValue value, JsonSerializerOptions options)
19+
{
20+
if (typeof(TValue) != typeof(object) && options?.GetConverter(typeof(TValue)) is JsonConverter<TValue> converter)
21+
{
22+
converter.Write(writer, value, options);
23+
}
24+
else
25+
{
26+
JsonSerializer.Serialize(writer, value, options);
27+
}
28+
}
29+
30+
protected static JsonException GetEndOfStreamError()
31+
{
32+
return new JsonException("Unexpected end of JSON stream.");
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)