Skip to content

Commit

Permalink
Review feedback: use base class instead of static helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Bart Koelman committed Sep 17, 2021
1 parent e655fa6 commit 2e0c811
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 68 deletions.
37 changes: 0 additions & 37 deletions src/JsonApiDotNetCore/Serialization/JsonConverterSupport.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;
using JetBrains.Annotations;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Resources;
Expand All @@ -15,7 +14,7 @@ namespace JsonApiDotNetCore.Serialization.JsonConverters
/// Converts <see cref="ResourceObject" /> to/from JSON.
/// </summary>
[UsedImplicitly(ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature)]
public sealed class ResourceObjectConverter : JsonConverter<ResourceObject>
public sealed class ResourceObjectConverter : JsonObjectConverter<ResourceObject>
{
private static readonly JsonEncodedText TypeText = JsonEncodedText.Encode("type");
private static readonly JsonEncodedText IdText = JsonEncodedText.Encode("id");
Expand Down Expand Up @@ -72,7 +71,7 @@ public override ResourceObject Read(ref Utf8JsonReader reader, Type typeToConver
{
// Newtonsoft.Json used to auto-convert number to strings, while System.Text.Json does not. This is so likely
// to hit users during upgrade that we special-case for this and produce a helpful error message.
var jsonElement = JsonConverterSupport.ReadSubTree<JsonElement>(ref reader, options);
var jsonElement = ReadSubTree<JsonElement>(ref reader, options);
throw new JsonException($"Failed to convert ID '{jsonElement}' of type '{jsonElement.ValueKind}' to type 'String'.");
}

Expand All @@ -99,17 +98,17 @@ public override ResourceObject Read(ref Utf8JsonReader reader, Type typeToConver
}
case "relationships":
{
resourceObject.Relationships = JsonConverterSupport.ReadSubTree<IDictionary<string, RelationshipObject>>(ref reader, options);
resourceObject.Relationships = ReadSubTree<IDictionary<string, RelationshipObject>>(ref reader, options);
break;
}
case "links":
{
resourceObject.Links = JsonConverterSupport.ReadSubTree<ResourceLinks>(ref reader, options);
resourceObject.Links = ReadSubTree<ResourceLinks>(ref reader, options);
break;
}
case "meta":
{
resourceObject.Meta = JsonConverterSupport.ReadSubTree<IDictionary<string, object>>(ref reader, options);
resourceObject.Meta = ReadSubTree<IDictionary<string, object>>(ref reader, options);
break;
}
default:
Expand All @@ -124,7 +123,7 @@ public override ResourceObject Read(ref Utf8JsonReader reader, Type typeToConver
}
}

throw JsonConverterSupport.GetEndOfStreamError();
throw GetEndOfStreamError();
}

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

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

throw JsonConverterSupport.GetEndOfStreamError();
throw GetEndOfStreamError();
}

/// <summary>
Expand All @@ -240,25 +239,25 @@ public override void Write(Utf8JsonWriter writer, ResourceObject value, JsonSeri
if (!value.Attributes.IsNullOrEmpty())
{
writer.WritePropertyName(AttributesText);
JsonConverterSupport.WriteSubTree(writer, value.Attributes, options);
WriteSubTree(writer, value.Attributes, options);
}

if (!value.Relationships.IsNullOrEmpty())
{
writer.WritePropertyName(RelationshipsText);
JsonConverterSupport.WriteSubTree(writer, value.Relationships, options);
WriteSubTree(writer, value.Relationships, options);
}

if (value.Links != null && value.Links.HasValue())
{
writer.WritePropertyName(LinksText);
JsonConverterSupport.WriteSubTree(writer, value.Links, options);
WriteSubTree(writer, value.Links, options);
}

if (!value.Meta.IsNullOrEmpty())
{
writer.WritePropertyName(MetaText);
JsonConverterSupport.WriteSubTree(writer, value.Meta, options);
WriteSubTree(writer, value.Meta, options);
}

writer.WriteEndObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializer
return (JsonConverter)Activator.CreateInstance(converterType, BindingFlags.Instance | BindingFlags.Public, null, null, null);
}

private sealed class SingleOrManyDataConverter<T> : JsonConverter<SingleOrManyData<T>>
private sealed class SingleOrManyDataConverter<T> : JsonObjectConverter<SingleOrManyData<T>>
where T : class, IResourceIdentity
{
public override SingleOrManyData<T> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions serializerOptions)
Expand All @@ -48,7 +48,7 @@ public override SingleOrManyData<T> Read(ref Utf8JsonReader reader, Type typeToC
}
case JsonTokenType.StartObject:
{
var resourceObject = JsonConverterSupport.ReadSubTree<T>(ref reader, serializerOptions);
var resourceObject = ReadSubTree<T>(ref reader, serializerOptions);
objects.Add(resourceObject);
break;
}
Expand All @@ -67,7 +67,7 @@ public override SingleOrManyData<T> Read(ref Utf8JsonReader reader, Type typeToC

public override void Write(Utf8JsonWriter writer, SingleOrManyData<T> value, JsonSerializerOptions options)
{
JsonConverterSupport.WriteSubTree(writer, value.Value, options);
WriteSubTree(writer, value.Value, options);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using JetBrains.Annotations;
using JsonApiDotNetCore.Serialization.Objects;

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

if (value.Links != null && value.Links.HasValue())
{
writer.WritePropertyName(LinksText);
JsonConverterSupport.WriteSubTree(writer, value.Links, options);
WriteSubTree(writer, value.Links, options);
}

if (value.Data.IsAssigned)
{
writer.WritePropertyName(DataText);
JsonConverterSupport.WriteSubTree(writer, value.Data, options);
WriteSubTree(writer, value.Data, options);
}

if (!value.Operations.IsNullOrEmpty())
{
writer.WritePropertyName(AtomicOperationsText);
JsonConverterSupport.WriteSubTree(writer, value.Operations, options);
WriteSubTree(writer, value.Operations, options);
}

if (!value.Results.IsNullOrEmpty())
{
writer.WritePropertyName(AtomicResultsText);
JsonConverterSupport.WriteSubTree(writer, value.Results, options);
WriteSubTree(writer, value.Results, options);
}

if (!value.Errors.IsNullOrEmpty())
{
writer.WritePropertyName(ErrorsText);
JsonConverterSupport.WriteSubTree(writer, value.Errors, options);
WriteSubTree(writer, value.Errors, options);
}

if (value.Included != null)
{
writer.WritePropertyName(IncludedText);
JsonConverterSupport.WriteSubTree(writer, value.Included, options);
WriteSubTree(writer, value.Included, options);
}

if (!value.Meta.IsNullOrEmpty())
{
writer.WritePropertyName(MetaText);
JsonConverterSupport.WriteSubTree(writer, value.Meta, options);
WriteSubTree(writer, value.Meta, options);
}

writer.WriteEndObject();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using JetBrains.Annotations;
using JsonApiDotNetCore.Serialization.Objects;

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

if (value.Data.IsAssigned)
{
writer.WritePropertyName(DataText);
JsonConverterSupport.WriteSubTree(writer, value.Data, options);
WriteSubTree(writer, value.Data, options);
}

if (!value.Meta.IsNullOrEmpty())
{
writer.WritePropertyName(MetaText);
JsonConverterSupport.WriteSubTree(writer, value.Meta, options);
WriteSubTree(writer, value.Meta, options);
}

writer.WriteEndObject();
Expand Down
35 changes: 35 additions & 0 deletions src/JsonApiDotNetCore/Serialization/JsonObjectConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Text.Json;
using System.Text.Json.Serialization;

namespace JsonApiDotNetCore.Serialization
{
public abstract class JsonObjectConverter<TObject> : JsonConverter<TObject>
{
protected static TValue ReadSubTree<TValue>(ref Utf8JsonReader reader, JsonSerializerOptions options)
{
if (typeof(TValue) != typeof(object) && options?.GetConverter(typeof(TValue)) is JsonConverter<TValue> converter)
{
return converter.Read(ref reader, typeof(TValue), options);
}

return JsonSerializer.Deserialize<TValue>(ref reader, options);
}

protected static void WriteSubTree<TValue>(Utf8JsonWriter writer, TValue value, JsonSerializerOptions options)
{
if (typeof(TValue) != typeof(object) && options?.GetConverter(typeof(TValue)) is JsonConverter<TValue> converter)
{
converter.Write(writer, value, options);
}
else
{
JsonSerializer.Serialize(writer, value, options);
}
}

protected static JsonException GetEndOfStreamError()
{
return new("Unexpected end of JSON stream.");
}
}
}

0 comments on commit 2e0c811

Please sign in to comment.