|
1 |
| -using System; |
2 |
| -using System.Collections; |
3 |
| -using System.Collections.Generic; |
4 |
| -using System.ComponentModel; |
5 |
| -using System.Linq; |
6 |
| -using JsonApiDotNetCore.Abstractions; |
7 |
| -using JsonApiDotNetCore.Attributes; |
8 |
| -using JsonApiDotNetCore.Configuration; |
9 |
| -using JsonApiDotNetCore.Extensions; |
| 1 | +using JsonApiDotNetCore.Abstractions; |
10 | 2 | using JsonApiDotNetCore.JsonApi;
|
11 | 3 | using Newtonsoft.Json;
|
12 | 4 | using Newtonsoft.Json.Serialization;
|
13 |
| -using System.Reflection; |
14 |
| -using JsonApiDotNetCore.Routing; |
15 | 5 |
|
16 | 6 | namespace JsonApiDotNetCore.Services
|
17 | 7 | {
|
18 | 8 | public class JsonApiSerializer
|
19 | 9 | {
|
20 | 10 | private readonly JsonApiContext _context;
|
21 |
| - private readonly JsonApiModelConfiguration _jsonApiModelConfiguration; |
22 |
| - private string _entityName; |
23 |
| - private Type _entityType; |
24 | 11 |
|
25 |
| - public JsonApiSerializer(JsonApiContext jsonApiContext, JsonApiModelConfiguration configuration) |
| 12 | + public JsonApiSerializer(JsonApiContext jsonApiContext) |
26 | 13 | {
|
27 | 14 | _context = jsonApiContext;
|
28 |
| - _jsonApiModelConfiguration = configuration; |
29 |
| - _entityName = GetEntityName(); |
30 |
| - _entityType = GetEntityType(); |
31 |
| - } |
32 |
| - |
33 |
| - private string GetEntityName() { |
34 |
| - return (!(_context.Route is RelationalRoute) ? _context.Route.BaseRouteDefinition.ContextPropertyName |
35 |
| - : _jsonApiModelConfiguration.Routes.Single(r=> r.ModelType == ((RelationalRoute)_context.Route).RelationalType).ContextPropertyName).ToCamelCase(); |
36 |
| - } |
37 |
| - |
38 |
| - private Type GetEntityType() { |
39 |
| - return !(_context.Route is RelationalRoute) ? _context.Route.BaseRouteDefinition.ModelType |
40 |
| - : ((RelationalRoute)_context.Route).RelationalType; |
41 | 15 | }
|
42 | 16 |
|
43 | 17 | public string ToJsonApiDocument(object resultValue)
|
44 | 18 | {
|
| 19 | + var documentBuilder = new DocumentBuilder(_context, resultValue); |
45 | 20 | var response = new JsonApiDocument
|
46 | 21 | {
|
47 |
| - Links = GetJsonApiDocumentLinks(_context), |
48 |
| - Data = GetJsonApiDocumentData(_context, resultValue) |
| 22 | + Links = documentBuilder.GetJsonApiDocumentLinks(), |
| 23 | + Data = documentBuilder.GetJsonApiDocumentData() |
49 | 24 | };
|
50 | 25 |
|
51 | 26 | return JsonConvert.SerializeObject(response, new JsonSerializerSettings
|
52 | 27 | {
|
53 | 28 | ContractResolver = new CamelCasePropertyNamesContractResolver()
|
54 | 29 | });
|
55 | 30 | }
|
56 |
| - |
57 |
| - private object GetJsonApiDocumentData(JsonApiContext context, object result) |
58 |
| - { |
59 |
| - var enumerableResult = result as IEnumerable; |
60 |
| - |
61 |
| - if (enumerableResult == null) return ResourceToJsonApiDatum(context, EntityToJsonApiResource(result)); |
62 |
| - |
63 |
| - var data = new List<JsonApiDatum>(); |
64 |
| - foreach (var resource in enumerableResult) |
65 |
| - { |
66 |
| - data.Add(ResourceToJsonApiDatum(context, EntityToJsonApiResource(resource))); |
67 |
| - } |
68 |
| - return data; |
69 |
| - } |
70 |
| - |
71 |
| - private IJsonApiResource EntityToJsonApiResource(object entity) |
72 |
| - { |
73 |
| - var resource = entity as IJsonApiResource; |
74 |
| - if (resource != null) return resource; |
75 |
| - |
76 |
| - var attributes = TypeDescriptor.GetAttributes(entity); |
77 |
| - var type = ((JsonApiResourceAttribute)attributes[typeof(JsonApiResourceAttribute)]).JsonApiResourceType; |
78 |
| - return (IJsonApiResource)_jsonApiModelConfiguration.ResourceMapper.Map(entity, entity.GetType(), type); |
79 |
| - } |
80 |
| - |
81 |
| - private JsonApiDatum ResourceToJsonApiDatum(JsonApiContext context, IJsonApiResource resource) |
82 |
| - { |
83 |
| - return new JsonApiDatum |
84 |
| - { |
85 |
| - Type = _entityName, |
86 |
| - Id = resource.Id, |
87 |
| - Attributes = GetAttributesFromResource(resource), |
88 |
| - Links = GetJsonApiDatumLinks(context, resource), |
89 |
| - Relationships = BuildRelationshipsObject(context, resource) |
90 |
| - }; |
91 |
| - } |
92 |
| - |
93 |
| - private static Dictionary<string, object> GetAttributesFromResource(IJsonApiResource resource) |
94 |
| - { |
95 |
| - return resource.GetType().GetProperties() |
96 |
| - .Where(propertyInfo => propertyInfo.GetMethod.IsVirtual == false) |
97 |
| - .ToDictionary( |
98 |
| - propertyInfo => propertyInfo.Name, propertyInfo => propertyInfo.GetValue(resource) |
99 |
| - ); |
100 |
| - } |
101 |
| - |
102 |
| - private Dictionary<string, string> GetJsonApiDocumentLinks(JsonApiContext jsonApiContext) |
103 |
| - { |
104 |
| - var request = jsonApiContext.HttpContext.Request; |
105 |
| - var route = jsonApiContext.Route; |
106 |
| - |
107 |
| - return new Dictionary<string, string> { |
108 |
| - { |
109 |
| - "self", $"{request.Scheme}://{request.Host}{request.Path}" |
110 |
| - } |
111 |
| - }; |
112 |
| - } |
113 |
| - |
114 |
| - private Dictionary<string, string> GetJsonApiDatumLinks(JsonApiContext jsonApiContext, IJsonApiResource resource) |
115 |
| - { |
116 |
| - return DocumentBuilder.BuildSelfLink(jsonApiContext.HttpContext.Request.Scheme, |
117 |
| - jsonApiContext.HttpContext.Request.Host.ToString(), _jsonApiModelConfiguration.Namespace, |
118 |
| - _entityName, resource.Id); |
119 |
| - } |
120 |
| - |
121 |
| - private Dictionary<string, object> BuildRelationshipsObject(JsonApiContext jsonApiContext, IJsonApiResource resource) |
122 |
| - { |
123 |
| - var relationships = new Dictionary<string, object>(); |
124 |
| - _entityType.GetProperties().Where(propertyInfo => propertyInfo.GetMethod.IsVirtual).ToList().ForEach( |
125 |
| - virtualProperty => |
126 |
| - { |
127 |
| - relationships.Add(virtualProperty.Name, GetRelationshipLinks(jsonApiContext, resource, virtualProperty.Name.ToCamelCase())); |
128 |
| - }); |
129 |
| - return relationships; |
130 |
| - } |
131 |
| - |
132 |
| - private Dictionary<string, string> GetRelationshipLinks(JsonApiContext jsonApiContext, IJsonApiResource resource, string relationshipName) |
133 |
| - { |
134 |
| - return DocumentBuilder.BuildRelationshipLinks(jsonApiContext.HttpContext.Request.Scheme, |
135 |
| - jsonApiContext.HttpContext.Request.Host.ToString(), _jsonApiModelConfiguration.Namespace, |
136 |
| - _entityName, resource.Id, relationshipName); |
137 |
| - } |
138 | 31 | }
|
139 | 32 | }
|
0 commit comments