-
Notifications
You must be signed in to change notification settings - Fork 12
/
JsonApiMetadataMediaTypeFormatter.cs
95 lines (84 loc) · 4.23 KB
/
JsonApiMetadataMediaTypeFormatter.cs
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
using System;
using System.Globalization;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using Hypermedia.Json;
using Hypermedia.JsonApi.WebApi;
using Hypermedia.Metadata;
using Hypermedia.WebApi.Json;
using JsonLite.Ast;
namespace Hypermedia.Sample.WebApi.Services
{
public sealed class JsonApiMetadataMediaTypeFormatter : JsonApiMediaTypeFormatter
{
/// <summary>
/// Constructor.
/// </summary>
/// <param name="contractResolver">The resource contract resolver used to resolve the contracts at runtime.</param>
public JsonApiMetadataMediaTypeFormatter(IContractResolver contractResolver) : base(contractResolver) { }
/// <summary>
/// Constructor.
/// </summary>
/// <param name="contractResolver">The resource contract resolver used to resolve the contracts at runtime.</param>
/// <param name="fieldNamingStratgey">The field naming strategy to use.</param>
/// <param name="outputFormatter">The output formatter to apply when writing the output.</param>
public JsonApiMetadataMediaTypeFormatter(
IContractResolver contractResolver,
IFieldNamingStrategy fieldNamingStratgey,
IJsonOutputFormatter outputFormatter) : base(contractResolver, fieldNamingStratgey, outputFormatter) { }
/// <summary>
/// Returns a specialized instance of the <see cref="T:System.Net.Http.Formatting.MediaTypeFormatter"/> that can format a response for the given parameters.
/// </summary>
/// <param name="type">The type to format.</param>
/// <param name="request">The request.</param>
/// <param name="mediaType">The media type.</param>
/// <returns>Returns <see cref="T:System.Net.Http.Formatting.MediaTypeFormatter"/>.</returns>
public override MediaTypeFormatter GetPerRequestFormatterInstance(Type type, HttpRequestMessage request, MediaTypeHeaderValue mediaType)
{
return new JsonApiMetadataMediaTypeFormatter(ContractResolver, GetPerRequestFieldNamingStrategy(request), GetPerRequestOutputFormatter(request));
}
/// <summary>
/// Serialize the value into an JSON AST.
/// </summary>
/// <param name="type">The type to serialize from.</param>
/// <param name="value">The value to serialize.</param>
/// <returns>The JSON object that represents the serialized value.</returns>
protected override JsonValue SerializeValue(Type type, object value)
{
var jsonValue = base.SerializeValue(type, value);
return new JsonApiMetadataVisitor().WriteMetadata(jsonValue);
}
#region JsonApiMetadataVisitor
class JsonApiMetadataVisitor : JsonAstVisitor
{
/// <summary>
/// Write the metadata for the JSON API outout.
/// </summary>
/// <param name="jsonValue">The JSON value to write the metadata to.</param>
/// <returns>The JSON value that had the metadata written to it.</returns>
public JsonValue WriteMetadata(JsonValue jsonValue)
{
return base.Visit(jsonValue);
}
/// <summary>
/// Visit a JSON object.
/// </summary>
/// <param name="jsonObject">The JSON object to visit.</param>
/// <returns>The JSON value that was modified from the visitor.</returns>
protected override JsonValue Visit(JsonObject jsonObject)
{
var metadata = new JsonMember("meta",
new JsonObject(
new JsonMember("version", new JsonString("1.0.0.0")),
new JsonMember("source", new JsonString("https://mythology.stackexchange.com")),
new JsonMember("attribution", new JsonString("The content published here comes from the Mythology site on the StackExchange network.")),
new JsonMember("time", new JsonString(DateTime.Now.ToString("D")))));
var members = jsonObject.Members.Union(new[] { metadata });
return new JsonObject(members.ToList());
}
}
#endregion
}
}