Skip to content

Commit

Permalink
Merge pull request #87 from WebApiContrib/dev
Browse files Browse the repository at this point in the history
Release 0.7
  • Loading branch information
glennblock committed Sep 10, 2014
2 parents f05dded + 0ff49a3 commit 95b1bc3
Show file tree
Hide file tree
Showing 14 changed files with 210 additions and 9 deletions.
6 changes: 6 additions & 0 deletions src/CollectionJson.Client/CollectionJson.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@
<Compile Include="..\..\common\VersionInfo.cs">
<Link>Properties\VersionInfo.cs</Link>
</Compile>
<Compile Include="CollectionJsonContent.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="CollectionJsonContractResolver.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="CollectionJsonFormatter.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ReadDocumentExtensions.cs" />
Expand Down
4 changes: 2 additions & 2 deletions src/CollectionJson.Client/CollectionJson.Client.nuspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
<metadata>
<id>WebApiContrib.Formatting.CollectionJson.Client</id>
<id>CollectionJson.Client</id>
<version>$version$</version>
<title>Collection+Json Client Library</title>
<authors>Glenn Block</authors>
Expand All @@ -15,7 +15,7 @@
<tags>collectionjson httpclient</tags>
<dependencies>
<dependency id="Microsoft.AspNet.WebApi.Client" version="5.0"/>
<dependency id="WebApiContrib.CollectionJson" />
<dependency id="CollectionJson" />
</dependencies>
</metadata>
<files>
Expand Down
53 changes: 53 additions & 0 deletions src/CollectionJson.Client/CollectionJsonContent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.IO;
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;

namespace CollectionJson.Client
{
public class CollectionJsonContent : HttpContent
{
private readonly ReadDocument _readDocument;
private readonly JsonSerializer _serializer;

public CollectionJsonContent(Collection collection)
{
_serializer = JsonSerializer.Create(new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
Formatting = Formatting.Indented,
ContractResolver = new CollectionJsonContractResolver()
});

collection.Version = "1.0";
_readDocument = new ReadDocument();
_readDocument.Collection = collection;

Headers.ContentType = new MediaTypeHeaderValue("application/vnd.collection+json");
}


protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)
{
using (var writer = new JsonTextWriter(new StreamWriter(stream)) { CloseOutput = false })
{
_serializer.Serialize(writer, _readDocument);
writer.Flush();
}
return Task.FromResult(0);
}

protected override bool TryComputeLength(out long length)
{
length = -1;
return false;
}
}
}
35 changes: 35 additions & 0 deletions src/CollectionJson.Client/CollectionJsonContractResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System.Reflection;

namespace CollectionJson.Client
{
public class CollectionJsonContractResolver : CamelCasePropertyNamesContractResolver
{
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
var property = base.CreateProperty(member, memberSerialization);
if (property.DeclaringType.Namespace == "CollectionJson")
{
property.ShouldSerialize =
instance =>
{
var val = property.ValueProvider.GetValue(instance);
var list = val as IList;
if (list != null)
{
return list.Count > 0;
}
return true;
};
}
return property;
}

}
}
2 changes: 1 addition & 1 deletion src/CollectionJson.Client/CollectionJsonFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public CollectionJsonFormatter()
SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
SerializerSettings.ContractResolver =
new CamelCasePropertyNamesContractResolver();
new CollectionJsonContractResolver();
}

public override bool CanWriteType(Type type)
Expand Down
3 changes: 1 addition & 2 deletions src/CollectionJson.Server/CollectionJson.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
<HintPath>..\packages\Newtonsoft.Json.6.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Net.Http, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Net.Http.2.0.20710.0\lib\net40\System.Net.Http.dll</HintPath>
</Reference>
Expand Down Expand Up @@ -81,7 +80,7 @@
<None Include="packages.config">
<SubType>Designer</SubType>
</None>
<None Include="CollectionJson.Server..nuspec">
<None Include="CollectionJson.Server.nuspec">
<SubType>Designer</SubType>
</None>
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
</dependencies>
</metadata>
<files>
<file src="bin\Release\WebApiContrib.Formatting.CollectionJson.Server.dll" target="lib\net45\WebApiContrib.Formatting.CollectionJson.Server.dll" />
<file src="bin\Release\CollectionJson.Server.dll" target="lib\net45" />
</files>
</package>
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="CollectionJsonContentTest.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="CollectionJsonContractResolverTest.cs" />
<Compile Include="CollectionJsonFormatterTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ReadDocumentExtensionsTest.cs" />
Expand Down
27 changes: 27 additions & 0 deletions test/CollectionJson.Client.Tests/CollectionJsonContentTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Should;
using Xunit;

namespace CollectionJson.Client.Tests
{
public class CollectionJsonContentTest
{
[Fact]
public async void WhenCreatingCollectionJsonContentObjectIsSerializedToCollectionJson()
{
var coll = new Collection();
var content = new CollectionJsonContent(coll);
var stream = new MemoryStream();
await content.CopyToAsync(stream);
var reader = new StreamReader(stream);
stream.Position = 0;
var json = reader.ReadToEnd();
json.ShouldContain("\"collection\"");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Should;
using Xunit;
using System.Reflection;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;

namespace CollectionJson.Client.Tests
{
public class CollectionJsonContractResolverTest
{
private CollectionJsonContractResolver _resolver = new CollectionJsonContractResolver();
private Func<MemberInfo, CollectionJsonContractResolver, MemberSerialization, JsonProperty> _createProperty;
private PropertyInfo _itemsProperty;
private PropertyInfo _versionProperty;

public CollectionJsonContractResolverTest()
{
var method = _resolver.GetType().GetMethod("CreateProperty", BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.NonPublic);
_createProperty = (m, r, s) =>
{
return (JsonProperty) method.Invoke(r, new object[] {m, s});
};
var collType = typeof(Collection);
_itemsProperty = collType.GetProperty("Items", BindingFlags.Instance | BindingFlags.Public);
_versionProperty = collType.GetProperty("Version", BindingFlags.Instance | BindingFlags.Public);
}


[Fact]
public void WhenPropertyIsListAndItHasNoItemsItWillNotBeSerialized()
{
var collection = new Collection();
var prop = _createProperty(_itemsProperty, _resolver, MemberSerialization.OptOut);
prop.ShouldSerialize(collection).ShouldBeFalse();
}

[Fact]
public void WhenPropertyIsListAndIsHasItemsItWillBeSerialized()
{
var collection = new Collection();
collection.Items.Add(new Item());
var prop = _createProperty(_itemsProperty, _resolver, MemberSerialization.OptOut);
prop.ShouldSerialize(collection).ShouldBeTrue();
}

[Fact]
public void WhenPropertyIsNotListItWillBeSerialized()
{
var collection = new Collection();
var prop = _createProperty(_versionProperty, _resolver, MemberSerialization.OptOut);
prop.ShouldSerialize(collection).ShouldBeTrue();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ public void WhenInitializedShouldSetSupportedMediaTpeToCollectionJson()
}

[Fact]
public void WhenInitializedShouldSetCamelCasePropertyResolver()
public void WhenInitializedShouldSetCollectionJsonContractResolver()
{
formatter.SerializerSettings.ContractResolver.ShouldBeType<CamelCasePropertyNamesContractResolver>();
formatter.SerializerSettings.ContractResolver.ShouldBeType<CollectionJsonContractResolver>();
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,20 @@
<HintPath>..\..\src\packages\Should.1.1.20\lib\Should.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Net.Http, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\src\packages\Microsoft.Net.Http.2.0.20710.0\lib\net40\System.Net.Http.dll</HintPath>
</Reference>
<Reference Include="System.Net.Http.Extensions">
<HintPath>..\..\src\packages\Microsoft.Net.Http.2.2.22\lib\net45\System.Net.Http.Extensions.dll</HintPath>
</Reference>
<Reference Include="System.Net.Http.Formatting, Version=5.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\src\packages\Microsoft.AspNet.WebApi.Client.5.2.0\lib\net45\System.Net.Http.Formatting.dll</HintPath>
</Reference>
<Reference Include="System.Net.Http.Primitives, Version=4.2.22.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\src\packages\Microsoft.Net.Http.2.2.22\lib\net45\System.Net.Http.Primitives.dll</HintPath>
</Reference>
<Reference Include="System.Net.Http.WebRequest, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\src\packages\Microsoft.Net.Http.2.0.20710.0\lib\net40\System.Net.Http.WebRequest.dll</HintPath>
</Reference>
Expand Down Expand Up @@ -100,6 +106,11 @@
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\..\src\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets" Condition="Exists('..\..\src\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" />
<Target Name="EnsureBclBuildImported" BeforeTargets="BeforeBuild" Condition="'$(BclBuildImported)' == ''">
<Error Condition="!Exists('..\..\src\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" Text="This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=317567." HelpKeyword="BCLBUILD2001" />
<Error Condition="Exists('..\..\src\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" Text="The build restored NuGet packages. Build the project again to include these packages in the build. For more information, see http://go.microsoft.com/fwlink/?LinkID=317568." HelpKeyword="BCLBUILD2002" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand Down
4 changes: 4 additions & 0 deletions test/CollectionJson.Server.Tests/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
<assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.0.0" newVersion="5.2.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Net.Http.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.2.22.0" newVersion="4.2.22.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
3 changes: 3 additions & 0 deletions test/CollectionJson.Server.Tests/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<packages>
<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" />
<package id="Microsoft.Bcl.Build" version="1.0.14" targetFramework="net45" />
<package id="Microsoft.Net.Http" version="2.2.22" targetFramework="net45" />
<package id="Moq" version="4.2.1402.2112" targetFramework="net45" />
<package id="Newtonsoft.Json" version="6.0.3" targetFramework="net45" />
<package id="Should" version="1.1.20" targetFramework="net45" />
Expand Down

0 comments on commit 95b1bc3

Please sign in to comment.