-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathMetadataFormatter.cs
More file actions
48 lines (45 loc) · 1.41 KB
/
MetadataFormatter.cs
File metadata and controls
48 lines (45 loc) · 1.41 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
using System.Threading.Tasks;
using JSONAPI.Documents;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace JSONAPI.Json
{
/// <summary>
/// Default implementation of IMetadataFormatter
/// </summary>
public class MetadataFormatter : IMetadataFormatter
{
public Task Serialize(IMetadata metadata, JsonWriter writer)
{
if (metadata == null)
{
writer.WriteNull();
}
else
{
if (metadata.MetaObject == null)
throw new JsonSerializationException("The meta object cannot be null.");
metadata.MetaObject.WriteTo(writer);
}
return Task.FromResult(0);
}
public Task<IMetadata> Deserialize(JsonReader reader, string currentPath)
{
IMetadata metadata;
if (reader.TokenType == JsonToken.Null)
{
metadata = null;
}
else if (reader.TokenType == JsonToken.StartObject)
{
var obj = (JObject)JToken.ReadFrom(reader);
metadata = new BasicMetadata(obj);
}
else
{
throw new DeserializationException("", "Expected an object or null for `meta`, but got " + reader.TokenType, currentPath);
}
return Task.FromResult(metadata);
}
}
}