From 310ae58e3d9f33bf19c02a065e356044af6ae0c9 Mon Sep 17 00:00:00 2001 From: Glenn Block Date: Sat, 29 Nov 2014 18:23:02 -0800 Subject: [PATCH 1/3] Adds support for Reading to an IWriteDocument and tests. Fix for #105. Also did some other cleanup --- .../CollectionJsonContent.cs | 2 +- .../CollectionJsonFormatter.cs | 20 +++++++++++-- src/CollectionJson/Collection.cs | 2 ++ .../CollectionJsonFormatterTest.cs | 29 +++++++++++++++---- 4 files changed, 44 insertions(+), 9 deletions(-) diff --git a/src/CollectionJson.Client/CollectionJsonContent.cs b/src/CollectionJson.Client/CollectionJsonContent.cs index 4590cdd..23f92e5 100644 --- a/src/CollectionJson.Client/CollectionJsonContent.cs +++ b/src/CollectionJson.Client/CollectionJsonContent.cs @@ -30,7 +30,7 @@ public CollectionJsonContent(Collection collection) _readDocument = new ReadDocument(); _readDocument.Collection = collection; - Headers.ContentType = new MediaTypeHeaderValue("application/vnd.collection+json"); + Headers.ContentType = new MediaTypeHeaderValue(Collection.MediaType); } diff --git a/src/CollectionJson.Client/CollectionJsonFormatter.cs b/src/CollectionJson.Client/CollectionJsonFormatter.cs index 3181711..341a1d4 100644 --- a/src/CollectionJson.Client/CollectionJsonFormatter.cs +++ b/src/CollectionJson.Client/CollectionJsonFormatter.cs @@ -1,5 +1,8 @@ -using System.Reflection; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Reflection; using System.Runtime.InteropServices.WindowsRuntime; +using System.Threading.Tasks; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Newtonsoft.Json.Serialization; @@ -15,9 +18,9 @@ namespace CollectionJson.Client { public class CollectionJsonFormatter : JsonMediaTypeFormatter { - public CollectionJsonFormatter() + public CollectionJsonFormatter() { - SupportedMediaTypes.Add(new System.Net.Http.Headers.MediaTypeHeaderValue("application/vnd.collection+json")); + SupportedMediaTypes.Add(new System.Net.Http.Headers.MediaTypeHeaderValue(Collection.MediaType)); SerializerSettings.NullValueHandling = NullValueHandling.Ignore; SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented; SerializerSettings.ContractResolver = @@ -53,6 +56,17 @@ public override System.Threading.Tasks.Task WriteToStreamAsync(Type type, object return base.WriteToStreamAsync(type, value, writeStream, content, transportContext); } + + public override Task ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger) + { + if (typeof (IWriteDocument).GetTypeInfo().IsAssignableFrom(type.GetTypeInfo()) & + type != typeof (WriteDocument)) + { + return base.ReadFromStreamAsync(typeof (WriteDocument), readStream, content, formatterLogger); + } + return base.ReadFromStreamAsync(type, readStream, content, formatterLogger); + } + private class ReadDocumentDecorator : IReadDocument { private readonly IReadDocument _innerReadDocument; diff --git a/src/CollectionJson/Collection.cs b/src/CollectionJson/Collection.cs index a8796ab..ac27468 100644 --- a/src/CollectionJson/Collection.cs +++ b/src/CollectionJson/Collection.cs @@ -10,6 +10,8 @@ namespace CollectionJson [DataContract] public class Collection : ExtensibleObject { + public const string MediaType = "application/vnd.collection+json"; + public Collection() { Links = new List(); diff --git a/test/CollectionJson.Client.Tests/CollectionJsonFormatterTest.cs b/test/CollectionJson.Client.Tests/CollectionJsonFormatterTest.cs index c0171ac..c34968a 100644 --- a/test/CollectionJson.Client.Tests/CollectionJsonFormatterTest.cs +++ b/test/CollectionJson.Client.Tests/CollectionJsonFormatterTest.cs @@ -4,7 +4,9 @@ using System.Linq; using System.Net; using System.Net.Http; +using System.Net.Http.Headers; using System.Text; +using System.Threading.Tasks; using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using CollectionJson; @@ -80,11 +82,11 @@ public void WhenInitializedShouldIgnoreNulls() } [Fact] - public void WhenTypeImplementsIReadDocumentThenWriteToStreamAsyncWritesCollectionJson() + public async Task WhenTypeImplementsIReadDocumentThenWriteToStreamAsyncWritesCollectionJson() { var doc = new TestReadDocument(); var stream = new MemoryStream(); - formatter.WriteToStreamAsync(doc.GetType(), doc, stream, null, null).Wait(); + await formatter.WriteToStreamAsync(doc.GetType(), doc, stream, null, null); var reader = new StreamReader(stream); stream.Position = 0; var content = reader.ReadToEnd(); @@ -93,17 +95,34 @@ public void WhenTypeImplementsIReadDocumentThenWriteToStreamAsyncWritesCollectio } [Fact] - public void WhenTypeImplementsIWriteDocumentThenWriteToStreamAsyncWritesCollectionJson() + public async Task WhenTypeImplementsIWriteDocumentThenWriteToStreamAsyncWritesCollectionJson() { var doc = new TestWriteDocument(); var stream = new MemoryStream(); - formatter.WriteToStreamAsync(doc.GetType(), doc, stream, null, null).Wait(); + await formatter.WriteToStreamAsync(doc.GetType(), doc, stream, null, null); var reader = new StreamReader(stream); stream.Position = 0; - var content = reader.ReadToEnd(); + var content = await reader.ReadToEndAsync(); content.ShouldContain("\"TestValue\""); } + [Fact] + public async Task WhenTypeImplementsIWriteDocumentThenReadToStreamAsyncReturnsWriteDocument() + { + var stream = new MemoryStream(); + var writer = new StreamWriter(stream); + await writer.WriteLineAsync("{\"template\":{\"data\":[{\"name\":\"value\"}]}}"); + await writer.FlushAsync(); + var content = new StreamContent(stream); + content.Headers.ContentLength = stream.Position-1; + stream.Position = 0; + content.Headers.ContentType = new MediaTypeHeaderValue(Collection.MediaType); + var doc = (IWriteDocument) await formatter.ReadFromStreamAsync(typeof (IWriteDocument), stream, content, null); + doc.ShouldNotBeNull(); + doc.Template.ShouldNotBeNull(); + doc.Template.Data.GetDataByName("value").ShouldNotBeNull(); + } + public class TestReadDocument : IReadDocument { public TestReadDocument() From 6402a5a67ca8a3b4854ede906439964ae6508057 Mon Sep 17 00:00:00 2001 From: Glenn Block Date: Sat, 29 Nov 2014 18:39:29 -0800 Subject: [PATCH 2/3] Update readme.md --- readme.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/readme.md b/readme.md index 96d0fda..81cddce 100644 --- a/readme.md +++ b/readme.md @@ -1,5 +1,7 @@ # CollectionJson.NET +[![collectionjson MyGet Build Status](https://www.myget.org/BuildSource/Badge/collectionjson?identifier=0f0fe557-4dbb-441d-bc44-6bf81c736ceb)](https://www.myget.org/) + This library provides support for using the [Collection+JSON] (http://amundsen.com/media-types/collection/) hypermedia mediatype authored by [Mike Amundsen] (http://twitter.com/mamund). ## Features From 6767327e19cdb5238720bb2821073fa9d4006daa Mon Sep 17 00:00:00 2001 From: Glenn Block Date: Sat, 29 Nov 2014 20:05:28 -0800 Subject: [PATCH 3/3] Refactoring to use DynamicUtils.ExtensilbeObject. Fix for #107 --- samples/friendapi/FriendApi.csproj | 3 + samples/friendapi/packages.config | 1 + .../CollectionJson.Client.csproj | 3 + .../CollectionJson.Client.nuspec | 2 +- src/CollectionJson.Client/packages.config | 1 + src/CollectionJson/Collection.cs | 1 + src/CollectionJson/CollectionJson.csproj | 5 +- src/CollectionJson/CollectionJson.nuspec | 2 +- .../CollectionJsonDocumentWriter_Of_T.cs | 1 + src/CollectionJson/Data.cs | 1 + src/CollectionJson/Error.cs | 1 + src/CollectionJson/ExtensibleObject.cs | 67 --------- src/CollectionJson/Item.cs | 1 + src/CollectionJson/Link.cs | 1 + src/CollectionJson/Query.cs | 1 + src/CollectionJson/Template.cs | 1 + .../CollectionJson.Client.Tests.csproj | 3 + .../packages.config | 1 + .../CollectionJson.Server.Tests.csproj | 3 + .../packages.config | 1 + .../CollectionJson.Tests.csproj | 4 +- .../ExtensibleObjectTest.cs | 132 ------------------ test/CollectionJson.Tests/packages.config | 1 + 23 files changed, 34 insertions(+), 203 deletions(-) delete mode 100644 src/CollectionJson/ExtensibleObject.cs delete mode 100644 test/CollectionJson.Tests/ExtensibleObjectTest.cs diff --git a/samples/friendapi/FriendApi.csproj b/samples/friendapi/FriendApi.csproj index a4947e5..8347d92 100644 --- a/samples/friendapi/FriendApi.csproj +++ b/samples/friendapi/FriendApi.csproj @@ -45,6 +45,9 @@ packages\Autofac.WebApi2.3.3.3\lib\net45\Autofac.Integration.WebApi.dll True + + packages\DynamicUtils.1.0.1\lib\portable-net45+sl50+win+wp80\DynamicUtils.dll + packages\Newtonsoft.Json.6.0.3\lib\net45\Newtonsoft.Json.dll diff --git a/samples/friendapi/packages.config b/samples/friendapi/packages.config index 689c43a..bed03cf 100644 --- a/samples/friendapi/packages.config +++ b/samples/friendapi/packages.config @@ -2,6 +2,7 @@ + diff --git a/src/CollectionJson.Client/CollectionJson.Client.csproj b/src/CollectionJson.Client/CollectionJson.Client.csproj index e1cdae4..ee38e58 100644 --- a/src/CollectionJson.Client/CollectionJson.Client.csproj +++ b/src/CollectionJson.Client/CollectionJson.Client.csproj @@ -37,6 +37,9 @@ false + + ..\packages\DynamicUtils.1.0.1\lib\portable-net40+sl50+win8+wpa81+wp8+monotouch+monoandroid\DynamicUtils.dll + ..\packages\Newtonsoft.Json.6.0.3\lib\portable-net45+wp80+win8+wpa81\Newtonsoft.Json.dll diff --git a/src/CollectionJson.Client/CollectionJson.Client.nuspec b/src/CollectionJson.Client/CollectionJson.Client.nuspec index 8ec5ef5..7f148a6 100644 --- a/src/CollectionJson.Client/CollectionJson.Client.nuspec +++ b/src/CollectionJson.Client/CollectionJson.Client.nuspec @@ -19,6 +19,6 @@ - + \ No newline at end of file diff --git a/src/CollectionJson.Client/packages.config b/src/CollectionJson.Client/packages.config index 0c76d21..e4de55b 100644 --- a/src/CollectionJson.Client/packages.config +++ b/src/CollectionJson.Client/packages.config @@ -1,5 +1,6 @@  + diff --git a/src/CollectionJson/Collection.cs b/src/CollectionJson/Collection.cs index ac27468..050ff70 100644 --- a/src/CollectionJson/Collection.cs +++ b/src/CollectionJson/Collection.cs @@ -4,6 +4,7 @@ using System.Runtime.Serialization; using System.Text; using System.Xml; +using DynamicUtils; namespace CollectionJson { diff --git a/src/CollectionJson/CollectionJson.csproj b/src/CollectionJson/CollectionJson.csproj index a095ba4..fc9a0d2 100644 --- a/src/CollectionJson/CollectionJson.csproj +++ b/src/CollectionJson/CollectionJson.csproj @@ -37,6 +37,9 @@ false + + ..\packages\DynamicUtils.1.0.1\lib\portable-net40+sl50+win8+wpa81+wp8+monotouch+monoandroid\DynamicUtils.dll + @@ -50,7 +53,6 @@ - @@ -69,6 +71,7 @@ Designer +