Skip to content

Commit 0704cc6

Browse files
Maurits MoeysMaurits Moeys
Maurits Moeys
authored and
Maurits Moeys
committed
feat: decoupled deserializer and serializer
1 parent d672e8e commit 0704cc6

File tree

108 files changed

+8039
-1712
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+8039
-1712
lines changed

src/JsonApiDotNetCore/Builders/IDocumentBuilderOptionsProvider.cs

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

src/JsonApiDotNetCore/Builders/ILinkBuilder.cs

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

src/JsonApiDotNetCore/Builders/LinkBuilder.cs

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

src/JsonApiDotNetCore/Builders/MetaBuilder.cs

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using JsonApiDotNetCore.Models.Links;
2+
3+
namespace JsonApiDotNetCore.Configuration
4+
{
5+
public interface IGlobalLinksConfiguration
6+
{
7+
bool RelativeLinks { get; set; }
8+
Link RelationshipLinks { get; set; }
9+
Link TopLevelLinks { get; set; }
10+
Link ResourceLinks { get; set; }
11+
}
12+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// REF: https://github.com/aspnet/Entropy/blob/dev/samples/Mvc.CustomRoutingConvention/NameSpaceRoutingConvention.cs
2+
// REF: https://github.com/aspnet/Mvc/issues/5691
3+
using System.Reflection;
4+
using JsonApiDotNetCore.Controllers;
5+
using JsonApiDotNetCore.Extensions;
6+
using Microsoft.AspNetCore.Mvc.ApplicationModels;
7+
8+
namespace JsonApiDotNetCore.Internal
9+
{
10+
public class DasherizedRoutingConvention : IApplicationModelConvention
11+
{
12+
private readonly string _namespace;
13+
public DasherizedRoutingConvention(string nspace)
14+
{
15+
_namespace = nspace;
16+
}
17+
18+
public void Apply(ApplicationModel application)
19+
{
20+
foreach (var controller in application.Controllers)
21+
{
22+
if (IsDasherizedJsonApiController(controller) == false)
23+
continue;
24+
25+
var template = $"{_namespace}/{controller.ControllerName.Dasherize()}";
26+
controller.Selectors[0].AttributeRouteModel = new AttributeRouteModel
27+
{
28+
Template = template
29+
};
30+
}
31+
}
32+
33+
private bool IsDasherizedJsonApiController(ControllerModel controller)
34+
{
35+
var type = controller.ControllerType;
36+
var notDisabled = type.GetCustomAttribute<DisableRoutingConventionAttribute>() == null;
37+
return notDisabled && type.IsSubclassOf(typeof(JsonApiControllerMixin));
38+
}
39+
}
40+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace JsonApiDotNetCore.Internal.Contracts
2+
{
3+
public interface IContextEntityProvider
4+
{
5+
}
6+
}

src/JsonApiDotNetCore/Internal/PageManager.cs

Lines changed: 0 additions & 52 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.Collections.Generic;
2+
using JsonApiDotNetCore.Models;
3+
4+
namespace JsonApiDotNetCore.Serialization
5+
{
6+
7+
public interface IUpdatedFieldsManager
8+
{
9+
List<AttrAttribute> AttributesToUpdate { get; set; }
10+
List<RelationshipAttribute> RelationshipsToUpdate { get; set; }
11+
}
12+
13+
public interface IUpdatedFieldManager_ProposalWithDictionaries
14+
{
15+
Dictionary<IIdentifiable, List<AttrAttribute>> AttributesToUpdate { get; set; }
16+
Dictionary<IIdentifiable, List<RelationshipAttribute>> RelationshipsToUpdate { get; set; }
17+
}
18+
}

src/JsonApiDotNetCore/Models/DocumentBase.cs

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

src/JsonApiDotNetCore/Models/Documents.cs

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace JsonApiDotNetCore.Models
2+
{
3+
internal interface IResourceField
4+
{
5+
}
6+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace JsonApiDotNetCore.Models
5+
{
6+
/// TODO: GetOutputAttrs is used in SERIALIATION LAYER to remove fields from
7+
/// list of attrs that will be displayed, (i.e. touches the DOCUMENT structure)
8+
/// whereas hooks is stuff to do on the MODEL in the SERVICELAYER.
9+
/// Consider (not sure yet) to move to different class because of this.
10+
/// edit: using different interfaces for this is maybe good enough to separate
11+
public interface ISerializableFields
12+
{
13+
List<AttrAttribute> GetAllowedAttributes(Type type);
14+
List<RelationshipAttribute> GetAllowedRelationships(Type type);
15+
}
16+
}

src/JsonApiDotNetCore/Models/Document.cs renamed to src/JsonApiDotNetCore/Models/JsonApi/Document.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using JsonApiDotNetCore.Models.Links;
12
using Newtonsoft.Json;
23

34
namespace JsonApiDotNetCore.Models
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System.Collections.Generic;
2+
using Newtonsoft.Json;
3+
using Newtonsoft.Json.Linq;
4+
5+
namespace JsonApiDotNetCore.Models
6+
{
7+
public class ExposableData<T>
8+
{
9+
/// <summary>
10+
/// see "primary data" in https://jsonapi.org/format/#document-top-level.
11+
/// </summary>
12+
[JsonProperty("data")]
13+
public object Data { get { return GetPrimaryData(); } set { SetPrimaryData(value); } }
14+
15+
/// <summary>
16+
/// see https://www.newtonsoft.com/json/help/html/ConditionalProperties.htm
17+
/// </summary>
18+
public bool ShouldSerializData()
19+
{
20+
return IsPopulated;
21+
}
22+
23+
/// <summary>
24+
/// Internally used for "single" primary data.
25+
/// </summary>
26+
internal T SingleData { get; private set; }
27+
28+
/// <summary>
29+
/// Internally used for "many" primary data.
30+
/// </summary>
31+
internal List<T> ManyData { get; private set; }
32+
33+
/// <summary>
34+
/// Internally used to indicate if the document's primary data is
35+
/// "single" or "many".
36+
/// </summary>
37+
internal bool IsManyData { get; private set; } = false;
38+
39+
/// <summary>
40+
/// Internally used to indicate if the document's primary data is
41+
/// should still be serialized when it's value is null. This is used when
42+
/// a single resource is requested but not present (eg /articles/1/author).
43+
/// </summary>
44+
internal bool IsPopulated { get; private set; } = false;
45+
46+
/// <summary>
47+
/// Gets the "single" or "many" data depending on which one was
48+
/// assigned in this document.
49+
/// </summary>
50+
protected object GetPrimaryData()
51+
{
52+
if (IsManyData)
53+
return ManyData;
54+
return SingleData;
55+
}
56+
57+
/// <summary>
58+
/// Sets the primary data depending on if it is "single" or "many" data.
59+
/// </summary>
60+
protected void SetPrimaryData(object value)
61+
{
62+
IsPopulated = true;
63+
if (value is JObject jObject)
64+
SingleData = jObject.ToObject<T>();
65+
else if (value is T ro)
66+
SingleData = ro;
67+
else if (value != null)
68+
{
69+
IsManyData = true;
70+
if (value is JArray jArray)
71+
ManyData = jArray.ToObject<List<T>>();
72+
else
73+
ManyData = (List<T>)value;
74+
}
75+
}
76+
}
77+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
3+
namespace JsonApiDotNetCore.Models.Links
4+
{
5+
[Flags]
6+
public enum Link
7+
{
8+
Self = 1 << 0,
9+
Related = 1 << 1,
10+
Paging = 1 << 2,
11+
NotConfigured = 1 << 3,
12+
None = 1 << 4,
13+
All = Self | Related | Paging
14+
}
15+
}

0 commit comments

Comments
 (0)