|
1 | 1 | using System.Collections.Generic;
|
2 | 2 | using JsonApiDotNetCore.Builders;
|
| 3 | +using JsonApiDotNetCore.Internal; |
3 | 4 | using JsonApiDotNetCore.Models;
|
| 5 | +using JsonApiDotNetCore.Services; |
| 6 | +using Microsoft.Extensions.Logging; |
4 | 7 | using Newtonsoft.Json;
|
5 | 8 |
|
6 | 9 | namespace JsonApiDotNetCore.Serialization
|
7 | 10 | {
|
8 | 11 | public class JsonApiSerializer : IJsonApiSerializer
|
9 | 12 | {
|
10 | 13 | private readonly IDocumentBuilder _documentBuilder;
|
| 14 | + private readonly ILogger<JsonApiSerializer> _logger; |
| 15 | + private readonly IJsonApiContext _jsonApiContext; |
11 | 16 |
|
12 |
| - public JsonApiSerializer(IDocumentBuilder documentBuilder) |
| 17 | + public JsonApiSerializer( |
| 18 | + IJsonApiContext jsonApiContext, |
| 19 | + IDocumentBuilder documentBuilder) |
13 | 20 | {
|
| 21 | + _jsonApiContext = jsonApiContext; |
| 22 | + _documentBuilder = documentBuilder; |
| 23 | + } |
| 24 | + |
| 25 | + public JsonApiSerializer( |
| 26 | + IJsonApiContext jsonApiContext, |
| 27 | + IDocumentBuilder documentBuilder, |
| 28 | + ILoggerFactory loggerFactory) |
| 29 | + { |
| 30 | + _jsonApiContext = jsonApiContext; |
14 | 31 | _documentBuilder = documentBuilder;
|
| 32 | + _logger = loggerFactory?.CreateLogger<JsonApiSerializer>(); |
15 | 33 | }
|
16 | 34 |
|
17 | 35 | public string Serialize(object entity)
|
18 | 36 | {
|
| 37 | + if (entity == null) |
| 38 | + return GetNullDataResponse(); |
| 39 | + |
| 40 | + if (entity.GetType() == typeof(ErrorCollection) || _jsonApiContext.RequestEntity == null) |
| 41 | + return GetErrorJson(entity, _logger); |
| 42 | + |
19 | 43 | if (entity is IEnumerable<IIdentifiable>)
|
20 |
| - return _serializeDocuments(entity); |
| 44 | + return SerializeDocuments(entity); |
| 45 | + |
| 46 | + return SerializeDocument(entity); |
| 47 | + } |
| 48 | + |
| 49 | + private string GetNullDataResponse() |
| 50 | + { |
| 51 | + return JsonConvert.SerializeObject(new Document |
| 52 | + { |
| 53 | + Data = null |
| 54 | + }); |
| 55 | + } |
21 | 56 |
|
22 |
| - return _serializeDocument(entity); |
| 57 | + private string GetErrorJson(object responseObject, ILogger logger) |
| 58 | + { |
| 59 | + if (responseObject is ErrorCollection errorCollection) |
| 60 | + { |
| 61 | + return errorCollection.GetJson(); |
| 62 | + } |
| 63 | + else |
| 64 | + { |
| 65 | + logger?.LogInformation("Response was not a JSONAPI entity. Serializing as plain JSON."); |
| 66 | + return JsonConvert.SerializeObject(responseObject); |
| 67 | + } |
23 | 68 | }
|
24 | 69 |
|
25 |
| - private string _serializeDocuments(object entity) |
| 70 | + private string SerializeDocuments(object entity) |
26 | 71 | {
|
27 | 72 | var entities = entity as IEnumerable<IIdentifiable>;
|
28 | 73 | var documents = _documentBuilder.Build(entities);
|
29 | 74 | return _serialize(documents);
|
30 | 75 | }
|
31 | 76 |
|
32 |
| - private string _serializeDocument(object entity) |
| 77 | + private string SerializeDocument(object entity) |
33 | 78 | {
|
34 | 79 | var identifiableEntity = entity as IIdentifiable;
|
35 | 80 | var document = _documentBuilder.Build(identifiableEntity);
|
|
0 commit comments