forked from joukevandermaas/saule
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ecbff09
commit 769910b
Showing
4 changed files
with
87 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Web.Http; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
using Saule.Queries; | ||
using Saule.Serialization; | ||
|
||
namespace Saule | ||
{ | ||
|
||
/// <summary> | ||
/// Used to manually serialize objects into Json Api. | ||
/// </summary> | ||
/// <typeparam name="T">The resource type of the objects this serializer can serialize.</typeparam> | ||
public sealed class JsonApiSerializer<T> where T : ApiResource, new() | ||
{ | ||
private readonly JsonApiSerializer _serializer; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="JsonApiSerializer{T}"/> class. | ||
/// </summary> | ||
public JsonApiSerializer() | ||
{ | ||
_serializer = new JsonApiSerializer(); | ||
} | ||
|
||
/// <summary> | ||
/// Produces the Json Api response that represents the given @object. | ||
/// </summary> | ||
/// <param name="object">The object to serialize.</param> | ||
/// <param name="requestUri">The request uri that prompted the response.</param> | ||
/// <returns></returns> | ||
public JToken Serialize(object @object, Uri requestUri) | ||
{ | ||
if (!Paginate) return _serializer.Serialize(@object, new T(), requestUri); | ||
|
||
var request = new HttpRequestMessage(HttpMethod.Get, requestUri); | ||
var context = new PaginationContext(request.GetQueryNameValuePairs(), ItemsPerPage); | ||
_serializer.PaginationContext = context; | ||
|
||
return _serializer.Serialize(@object, new T(), requestUri); | ||
} | ||
|
||
/// <summary> | ||
/// Contains converters to influence the serialization process. | ||
/// </summary> | ||
public ICollection<JsonConverter> JsonConverters => _serializer.JsonConverters; | ||
|
||
/// <summary> | ||
/// True if responses should be paginated, otherwise false. | ||
/// </summary> | ||
public bool Paginate { get; set; } | ||
|
||
/// <summary> | ||
/// The number of items per page, if the responses are paginated. | ||
/// </summary> | ||
public int ItemsPerPage { get; set; } | ||
|
||
/// <summary> | ||
/// The url path builder to use during serialization. | ||
/// </summary> | ||
public IUrlPathBuilder UrlPathBuilder | ||
{ | ||
get { return _serializer.UrlPathBuilder; } | ||
set { _serializer.UrlPathBuilder = value; } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace Saule | ||
{ | ||
|
||
/// <summary> | ||
/// Represents a related resource (to-one or to-many). | ||
/// </summary> | ||
internal class ResourceRelationship<T> : ResourceRelationship where T : ApiResource, new() | ||
{ | ||
internal ResourceRelationship(string name, string urlPath, RelationshipKind kind, T resource) | ||
: base(name, urlPath, kind, resource) | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters