forked from JSONAPIdotNET/JSONAPI.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleResourceDocumentFormatter.cs
More file actions
105 lines (88 loc) · 3.87 KB
/
Copy pathSingleResourceDocumentFormatter.cs
File metadata and controls
105 lines (88 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System.Linq;
using System.Threading.Tasks;
using JSONAPI.Documents;
using Newtonsoft.Json;
namespace JSONAPI.Json
{
/// <summary>
/// Default implementation of ISingleResourceDocumentFormatter
/// </summary>
public class SingleResourceDocumentFormatter : ISingleResourceDocumentFormatter
{
private readonly IResourceObjectFormatter _resourceObjectFormatter;
private readonly IMetadataFormatter _metadataFormatter;
private const string PrimaryDataKeyName = "data";
private const string RelatedDataKeyName = "included";
private const string MetaKeyName = "meta";
/// <summary>
/// Creates a SingleResourceDocumentFormatter
/// </summary>
/// <param name="resourceObjectFormatter"></param>
/// <param name="metadataFormatter"></param>
public SingleResourceDocumentFormatter(IResourceObjectFormatter resourceObjectFormatter, IMetadataFormatter metadataFormatter)
{
_resourceObjectFormatter = resourceObjectFormatter;
_metadataFormatter = metadataFormatter;
}
public Task Serialize(ISingleResourceDocument document, JsonWriter writer)
{
writer.WriteStartObject();
writer.WritePropertyName(PrimaryDataKeyName);
_resourceObjectFormatter.Serialize(document.PrimaryData, writer);
if (document.RelatedData != null && document.RelatedData.Any())
{
writer.WritePropertyName(RelatedDataKeyName);
writer.WriteStartArray();
foreach (var resourceObject in document.RelatedData)
{
_resourceObjectFormatter.Serialize(resourceObject, writer);
}
writer.WriteEndArray();
}
if (document.Metadata != null)
{
writer.WritePropertyName(MetaKeyName);
_metadataFormatter.Serialize(document.Metadata, writer);
}
writer.WriteEndObject();
return Task.FromResult(0);
}
public async Task<ISingleResourceDocument> Deserialize(JsonReader reader, string currentPath)
{
if (reader.TokenType != JsonToken.StartObject)
throw new DeserializationException("Invalid document root", "Document root is not an object!", currentPath);
IResourceObject primaryData = null;
IMetadata metadata = null;
while (reader.Read())
{
if (reader.TokenType != JsonToken.PropertyName) break;
// Has to be a property name
var propertyName = (string)reader.Value;
reader.Read();
switch (propertyName)
{
case RelatedDataKeyName:
// TODO: If we want to capture related resources, this would be the place to do it
reader.Skip();
break;
case PrimaryDataKeyName:
primaryData = await DeserializePrimaryData(reader, currentPath + "/" + PrimaryDataKeyName);
break;
case MetaKeyName:
metadata = await _metadataFormatter.Deserialize(reader, currentPath + "/" + MetaKeyName);
break;
default:
reader.Skip();
break;
}
}
return new SingleResourceDocument(primaryData, new IResourceObject[] { }, metadata);
}
private async Task<IResourceObject> DeserializePrimaryData(JsonReader reader, string currentPath)
{
if (reader.TokenType == JsonToken.Null) return null;
var primaryData = await _resourceObjectFormatter.Deserialize(reader, currentPath);
return primaryData;
}
}
}