Skip to content

Commit

Permalink
Merge pull request #111 from WebApiContrib/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
glennblock committed Nov 30, 2014
2 parents 44d2aff + 1678207 commit 34596a4
Show file tree
Hide file tree
Showing 27 changed files with 80 additions and 212 deletions.
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 3 additions & 0 deletions samples/friendapi/FriendApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
<HintPath>packages\Autofac.WebApi2.3.3.3\lib\net45\Autofac.Integration.WebApi.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="DynamicUtils">
<HintPath>packages\DynamicUtils.1.0.1\lib\portable-net45+sl50+win+wp80\DynamicUtils.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>packages\Newtonsoft.Json.6.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
Expand Down
1 change: 1 addition & 0 deletions samples/friendapi/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<packages>
<package id="Autofac" version="3.5.0" targetFramework="net45" />
<package id="Autofac.WebApi2" version="3.3.3" targetFramework="net45" />
<package id="DynamicUtils" version="1.0.1" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.0" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.0" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.SelfHost" version="5.2.0" targetFramework="net45" />
Expand Down
3 changes: 3 additions & 0 deletions src/CollectionJson.Client/CollectionJson.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="DynamicUtils">
<HintPath>..\packages\DynamicUtils.1.0.1\lib\portable-net40+sl50+win8+wpa81+wp8+monotouch+monoandroid\DynamicUtils.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json">
<HintPath>..\packages\Newtonsoft.Json.6.0.3\lib\portable-net45+wp80+win8+wpa81\Newtonsoft.Json.dll</HintPath>
</Reference>
Expand Down
2 changes: 1 addition & 1 deletion src/CollectionJson.Client/CollectionJson.Client.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
</dependencies>
</metadata>
<files>
<file src="bin\Release\CollectionJson.Client.dll" target="lib\portable-net45+win8+wp8+monotouch+monoandroid" />
<file src="bin\Release\CollectionJson.Client.dll" target="lib\portable-net40+sl50+win8+wpa81+wp8+monotouch+monoandroid" />
</files>
</package>
2 changes: 1 addition & 1 deletion src/CollectionJson.Client/CollectionJsonContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand Down
20 changes: 17 additions & 3 deletions src/CollectionJson.Client/CollectionJsonFormatter.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 =
Expand Down Expand Up @@ -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<object> 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;
Expand Down
1 change: 1 addition & 0 deletions src/CollectionJson.Client/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="DynamicUtils" version="1.0.1" targetFramework="portable-net45+win+wpa81+wp80" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.0" targetFramework="portable-net45+win+wpa81+wp80+MonoAndroid10+MonoTouch10" />
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="portable-net45+win+wpa81+wp80+MonoAndroid10+MonoTouch10" />
<package id="Microsoft.Bcl.Build" version="1.0.14" targetFramework="portable-net45+win+wpa81+wp80+MonoAndroid10+MonoTouch10" />
Expand Down
3 changes: 3 additions & 0 deletions src/CollectionJson/Collection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
using DynamicUtils;

namespace CollectionJson
{
[DataContract]
public class Collection : ExtensibleObject
{
public const string MediaType = "application/vnd.collection+json";

public Collection()
{
Links = new List<Link>();
Expand Down
5 changes: 4 additions & 1 deletion src/CollectionJson/CollectionJson.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="DynamicUtils">
<HintPath>..\packages\DynamicUtils.1.0.1\lib\portable-net40+sl50+win8+wpa81+wp8+monotouch+monoandroid\DynamicUtils.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml" />
Expand All @@ -50,7 +53,6 @@
<Compile Include="CollectionJsonDocumentWriterExtensions.cs" />
<Compile Include="Data.cs" />
<Compile Include="Error.cs" />
<Compile Include="ExtensibleObject.cs" />
<Compile Include="ICollectionJsonDocumentReader_Of_T.cs" />
<Compile Include="ICollectionJsonDocumentWriter_Of_T.cs" />
<Compile Include="IEnumerable_Of_DataExtensions.cs" />
Expand All @@ -69,6 +71,7 @@
<None Include="CollectionJson.nuspec">
<SubType>Designer</SubType>
</None>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
2 changes: 1 addition & 1 deletion src/CollectionJson/CollectionJson.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
<tags>collectionjson</tags>
</metadata>
<files>
<file src="bin\Release\CollectionJson.dll" target="lib\portable-net40+sl5+win8+wp8+monotouch+monoandroid" />
<file src="bin\Release\CollectionJson.dll" target="lib\portable-net40+sl50+win8+wpa81+wp8+monotouch+monoandroid" />
</files>
</package>
1 change: 1 addition & 0 deletions src/CollectionJson/CollectionJsonDocumentWriter_Of_T.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DynamicUtils;

namespace CollectionJson
{
Expand Down
1 change: 1 addition & 0 deletions src/CollectionJson/Data.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using DynamicUtils;

namespace CollectionJson
{
Expand Down
1 change: 1 addition & 0 deletions src/CollectionJson/Error.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using DynamicUtils;

namespace CollectionJson
{
Expand Down
67 changes: 0 additions & 67 deletions src/CollectionJson/ExtensibleObject.cs

This file was deleted.

1 change: 1 addition & 0 deletions src/CollectionJson/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using DynamicUtils;

namespace CollectionJson
{
Expand Down
1 change: 1 addition & 0 deletions src/CollectionJson/Link.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using DynamicUtils;

namespace CollectionJson
{
Expand Down
1 change: 1 addition & 0 deletions src/CollectionJson/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using DynamicUtils;

namespace CollectionJson
{
Expand Down
1 change: 1 addition & 0 deletions src/CollectionJson/Template.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using DynamicUtils;

namespace CollectionJson
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="DynamicUtils">
<HintPath>..\..\src\packages\DynamicUtils.1.0.1\lib\portable-net45+sl50+win+wp80\DynamicUtils.dll</HintPath>
</Reference>
<Reference Include="Moq, Version=4.2.1402.2112, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\src\packages\Moq.4.2.1402.2112\lib\net40\Moq.dll</HintPath>
Expand Down
29 changes: 24 additions & 5 deletions test/CollectionJson.Client.Tests/CollectionJsonFormatterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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()
Expand Down
1 change: 1 addition & 0 deletions test/CollectionJson.Client.Tests/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="DynamicUtils" version="1.0.1" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.0" targetFramework="net45" />
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" />
<package id="Microsoft.Bcl.Build" version="1.0.14" targetFramework="net45" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="DynamicUtils">
<HintPath>..\..\src\packages\DynamicUtils.1.0.1\lib\portable-net45+sl50+win+wp80\DynamicUtils.dll</HintPath>
</Reference>
<Reference Include="Moq, Version=4.2.1402.2112, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\src\packages\Moq.4.2.1402.2112\lib\net40\Moq.dll</HintPath>
Expand Down
1 change: 1 addition & 0 deletions test/CollectionJson.Server.Tests/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="DynamicUtils" version="1.0.1" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.0" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.0" targetFramework="net45" />
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" />
Expand Down
4 changes: 3 additions & 1 deletion test/CollectionJson.Tests/CollectionJson.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="DynamicUtils">
<HintPath>..\..\src\packages\DynamicUtils.1.0.1\lib\portable-net45+sl50+win+wp80\DynamicUtils.dll</HintPath>
</Reference>
<Reference Include="Moq, Version=4.2.1402.2112, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\src\packages\Moq.4.2.1402.2112\lib\net40\Moq.dll</HintPath>
Expand All @@ -56,7 +59,6 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="ExtensibleObjectTest.cs" />
<Compile Include="ExtensionsTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down
Loading

0 comments on commit 34596a4

Please sign in to comment.