-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathLinkFormatter.cs
More file actions
75 lines (68 loc) · 2.19 KB
/
LinkFormatter.cs
File metadata and controls
75 lines (68 loc) · 2.19 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
using System;
using System.Threading.Tasks;
using JSONAPI.Documents;
using Newtonsoft.Json;
namespace JSONAPI.Json
{
/// <summary>
/// Default implementation of ILinkFormatter
/// </summary>
public class LinkFormatter : ILinkFormatter
{
private IMetadataFormatter _metadataFormatter;
private const string HrefKeyName = "href";
private const string MetaKeyName = "meta";
/// <summary>
/// Constructs a LinkFormatter
/// </summary>
public LinkFormatter()
{
}
/// <summary>
/// Constructs a LinkFormatter
/// </summary>
/// <param name="metadataFormatter"></param>
public LinkFormatter(IMetadataFormatter metadataFormatter)
{
_metadataFormatter = metadataFormatter;
}
/// <summary>
/// The formatter to use for metadata on the link object
/// </summary>
/// <exception cref="InvalidOperationException"></exception>
public IMetadataFormatter MetadataFormatter
{
get
{
return _metadataFormatter ?? (_metadataFormatter = new MetadataFormatter());
}
set
{
if (_metadataFormatter != null) throw new InvalidOperationException("This property can only be set once.");
_metadataFormatter = value;
}
}
public Task Serialize(ILink link, JsonWriter writer)
{
if (link.Metadata == null)
{
writer.WriteValue(link.Href);
}
else
{
writer.WriteStartObject();
writer.WritePropertyName(HrefKeyName);
writer.WriteValue(link.Href);
writer.WritePropertyName(MetaKeyName);
MetadataFormatter.Serialize(link.Metadata, writer);
writer.WriteEndObject();
}
return Task.FromResult(0);
}
public Task<ILink> Deserialize(JsonReader reader, string currentPath)
{
// The client should never be sending us links.
throw new NotSupportedException();
}
}
}