Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow custom formatting of Links in JSON.NET
- Loading branch information
1 parent
7a4c185
commit ef16b59
Showing
18 changed files
with
323 additions
and
182 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
BUILD_VERSION = "0.10.4" | ||
BUILD_VERSION = "0.11.1" |
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 was deleted.
Oops, something went wrong.
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,65 @@ | ||
namespace Simple.Web.JsonNet.Tests | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using MediaTypeHandling; | ||
using Newtonsoft.Json.Linq; | ||
using TestHelpers; | ||
using TestHelpers.Sample; | ||
using Xunit; | ||
|
||
public class SimpleLinkFormatterTests | ||
{ | ||
[Fact] | ||
public void PicksUpOrdersLinkFromCustomer() | ||
{ | ||
const string idProperty = @"""id"":42"; | ||
const string ordersLink = | ||
@"{""title"":null,""href"":""/customer/42/orders"",""rel"":""customer.orders"",""type"":""application/vnd.list.order+json""}"; | ||
const string selfLink = | ||
@"{""title"":null,""href"":""/customer/42"",""rel"":""self"",""type"":""application/vnd.customer+json""}"; | ||
|
||
var content = new Content(new Uri("http://test.com/customer/42"), new CustomerHandler(), | ||
new Customer {Id = 42}); | ||
var target = new JsonMediaTypeHandler | ||
{ | ||
JsonLinksFormatter = new SimpleJsonLinksFormatter() | ||
}; | ||
string json; | ||
using (var stream = new StringBuilderStream()) | ||
{ | ||
target.Write<Customer>(content, stream).Wait(); | ||
json = stream.StringValue; | ||
} | ||
var jobj = JObject.Parse(json); | ||
var links = jobj["_links"] as JObject; | ||
Assert.NotNull(links); | ||
Assert.Equal(links["customer.orders"].ToString(), "/customer/42/orders"); | ||
Assert.Equal(links["self"].ToString(), "/customer/42"); | ||
} | ||
|
||
[Fact] | ||
public void PicksUpPathFromThing() | ||
{ | ||
const string thingLink = | ||
@"{""title"":null,""href"":""/things?path=%2Ffoo%2Fbar"",""rel"":""self"",""type"":""application/json""}"; | ||
|
||
var content = new Content(new Uri("http://test.com/foo/bar"), new ThingHandler(), | ||
new Thing {Path = "/foo/bar"}); | ||
var target = new JsonMediaTypeHandler | ||
{ | ||
JsonLinksFormatter = new SimpleJsonLinksFormatter() | ||
}; | ||
string json; | ||
using (var stream = new StringBuilderStream()) | ||
{ | ||
target.Write<Thing>(content, stream).Wait(); | ||
json = stream.StringValue; | ||
} | ||
var jobj = JObject.Parse(json); | ||
var links = jobj["_links"] as JObject; | ||
Assert.NotNull(links); | ||
Assert.Equal(links["self"].ToString(), "/things?path=%2Ffoo%2Fbar"); | ||
} | ||
} | ||
} |
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,28 @@ | ||
namespace Simple.Web.JsonNet | ||
{ | ||
using System.Collections.Generic; | ||
using Links; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
|
||
public class DefaultJsonLinksFormatter : IJsonLinksFormatter | ||
{ | ||
private string _linksPropertyName = "links"; | ||
|
||
public string LinksPropertyName | ||
{ | ||
get { return _linksPropertyName; } | ||
set { _linksPropertyName = value; } | ||
} | ||
|
||
public void FormatLinks(JContainer container, IEnumerable<Link> links, JsonSerializer serializer) | ||
{ | ||
var jLinks = new JArray(); | ||
foreach (var link in links) | ||
{ | ||
jLinks.Add(JObject.FromObject(link, serializer)); | ||
} | ||
container[_linksPropertyName] = jLinks; | ||
} | ||
} | ||
} |
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,26 @@ | ||
namespace Simple.Web.JsonNet | ||
{ | ||
using System.Collections.Generic; | ||
using Links; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
|
||
/// <summary> | ||
/// Interface for types that format <see cref="Link"/> collections for JSON.NET | ||
/// </summary> | ||
/// <remarks> | ||
/// The implementing types do not write the actual JSON text, rather, they create a JSON.NET <see cref="JToken"/> | ||
/// allowing them to specify the structure or layout of the object. | ||
/// </remarks> | ||
public interface IJsonLinksFormatter | ||
{ | ||
/// <summary> | ||
/// Formats the links for a JSON item. | ||
/// </summary> | ||
/// <param name="container">The object to which the links will be added</param> | ||
/// <param name="links">The links. Guaranteed to be non-empty.</param> | ||
/// <param name="serializer">The serializer.</param> | ||
/// <returns></returns> | ||
void FormatLinks(JContainer container, IEnumerable<Link> links, JsonSerializer serializer); | ||
} | ||
} |
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
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,36 @@ | ||
namespace Simple.Web.JsonNet | ||
{ | ||
using System.Collections.Generic; | ||
using Links; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
|
||
/// <summary> | ||
/// Formats links in JSON using a light-weight object format. | ||
/// </summary> | ||
/// <example> | ||
/// A customer object might be formatted like this: | ||
/// | ||
/// {"name":"ACME","_links":{"self":"/customers/acme","orders":"customers/acme/orders"}} | ||
/// </example> | ||
public class SimpleJsonLinksFormatter : IJsonLinksFormatter | ||
{ | ||
private string _linksPropertyName = "_links"; | ||
|
||
public string LinksPropertyName | ||
{ | ||
get { return _linksPropertyName; } | ||
set { _linksPropertyName = value; } | ||
} | ||
|
||
public void FormatLinks(JContainer container, IEnumerable<Link> links, JsonSerializer serializer) | ||
{ | ||
var jLinks = new JObject(); | ||
foreach (var link in links) | ||
{ | ||
jLinks[link.Rel] = JValue.CreateString(link.Href); | ||
} | ||
container[_linksPropertyName] = jLinks; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.