Skip to content

Commit

Permalink
fix remaining errors
Browse files Browse the repository at this point in the history
  • Loading branch information
joukevandermaas committed Nov 10, 2015
1 parent ecbff09 commit 769910b
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Saule/ApiResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private static void VerifyPropertyName(string name)

private static T GetUniqueResource<T>() where T : ApiResource, new()
{
var type = typeof (T);
var type = typeof(T);
var resource = Resources.ContainsKey(type)
? Resources[type] as T
: new T();
Expand Down
70 changes: 70 additions & 0 deletions Saule/JsonApiSerializer`1.cs
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; }
}
}
}
14 changes: 14 additions & 0 deletions Saule/ResourceRelationship`1.cs
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)
{
}
}
}
4 changes: 2 additions & 2 deletions Saule/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static string ToPascalCase(this string source)
// SomeString
var parts = SplitAndLower(source);

return string.Join("", parts.Select(s =>
return string.Join(string.Empty, parts.Select(s =>
CultureInfo.InvariantCulture.TextInfo.ToTitleCase(s)).ToArray());
}

Expand All @@ -53,7 +53,7 @@ public static string ToCamelCase(this string source)
? s.ToLowerInvariant()
: CultureInfo.InvariantCulture.TextInfo.ToTitleCase(s));

return string.Join("", cased.ToArray());
return string.Join(string.Empty, cased.ToArray());
}

private static IEnumerable<string> SplitAndLower(string source)
Expand Down

0 comments on commit 769910b

Please sign in to comment.