Skip to content

Commit 95b1bc3

Browse files
committed
Merge pull request #87 from WebApiContrib/dev
Release 0.7
2 parents f05dded + 0ff49a3 commit 95b1bc3

14 files changed

+210
-9
lines changed

src/CollectionJson.Client/CollectionJson.Client.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@
6262
<Compile Include="..\..\common\VersionInfo.cs">
6363
<Link>Properties\VersionInfo.cs</Link>
6464
</Compile>
65+
<Compile Include="CollectionJsonContent.cs">
66+
<SubType>Code</SubType>
67+
</Compile>
68+
<Compile Include="CollectionJsonContractResolver.cs">
69+
<SubType>Code</SubType>
70+
</Compile>
6571
<Compile Include="CollectionJsonFormatter.cs" />
6672
<Compile Include="Properties\AssemblyInfo.cs" />
6773
<Compile Include="ReadDocumentExtensions.cs" />

src/CollectionJson.Client/CollectionJson.Client.nuspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
33
<metadata>
4-
<id>WebApiContrib.Formatting.CollectionJson.Client</id>
4+
<id>CollectionJson.Client</id>
55
<version>$version$</version>
66
<title>Collection+Json Client Library</title>
77
<authors>Glenn Block</authors>
@@ -15,7 +15,7 @@
1515
<tags>collectionjson httpclient</tags>
1616
<dependencies>
1717
<dependency id="Microsoft.AspNet.WebApi.Client" version="5.0"/>
18-
<dependency id="WebApiContrib.CollectionJson" />
18+
<dependency id="CollectionJson" />
1919
</dependencies>
2020
</metadata>
2121
<files>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Net;
6+
using System.Net.Http;
7+
using System.Net.Http.Headers;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
using Newtonsoft.Json;
11+
using Newtonsoft.Json.Serialization;
12+
13+
namespace CollectionJson.Client
14+
{
15+
public class CollectionJsonContent : HttpContent
16+
{
17+
private readonly ReadDocument _readDocument;
18+
private readonly JsonSerializer _serializer;
19+
20+
public CollectionJsonContent(Collection collection)
21+
{
22+
_serializer = JsonSerializer.Create(new JsonSerializerSettings
23+
{
24+
NullValueHandling = NullValueHandling.Ignore,
25+
Formatting = Formatting.Indented,
26+
ContractResolver = new CollectionJsonContractResolver()
27+
});
28+
29+
collection.Version = "1.0";
30+
_readDocument = new ReadDocument();
31+
_readDocument.Collection = collection;
32+
33+
Headers.ContentType = new MediaTypeHeaderValue("application/vnd.collection+json");
34+
}
35+
36+
37+
protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)
38+
{
39+
using (var writer = new JsonTextWriter(new StreamWriter(stream)) { CloseOutput = false })
40+
{
41+
_serializer.Serialize(writer, _readDocument);
42+
writer.Flush();
43+
}
44+
return Task.FromResult(0);
45+
}
46+
47+
protected override bool TryComputeLength(out long length)
48+
{
49+
length = -1;
50+
return false;
51+
}
52+
}
53+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Newtonsoft.Json;
7+
using Newtonsoft.Json.Serialization;
8+
using System.Reflection;
9+
10+
namespace CollectionJson.Client
11+
{
12+
public class CollectionJsonContractResolver : CamelCasePropertyNamesContractResolver
13+
{
14+
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
15+
{
16+
var property = base.CreateProperty(member, memberSerialization);
17+
if (property.DeclaringType.Namespace == "CollectionJson")
18+
{
19+
property.ShouldSerialize =
20+
instance =>
21+
{
22+
var val = property.ValueProvider.GetValue(instance);
23+
var list = val as IList;
24+
if (list != null)
25+
{
26+
return list.Count > 0;
27+
}
28+
return true;
29+
};
30+
}
31+
return property;
32+
}
33+
34+
}
35+
}

src/CollectionJson.Client/CollectionJsonFormatter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public CollectionJsonFormatter()
2121
SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
2222
SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
2323
SerializerSettings.ContractResolver =
24-
new CamelCasePropertyNamesContractResolver();
24+
new CollectionJsonContractResolver();
2525
}
2626

2727
public override bool CanWriteType(Type type)

src/CollectionJson.Server/CollectionJson.Server.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
<HintPath>..\packages\Newtonsoft.Json.6.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
4141
</Reference>
4242
<Reference Include="System" />
43-
<Reference Include="System.Core" />
4443
<Reference Include="System.Net.Http, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
4544
<HintPath>..\packages\Microsoft.Net.Http.2.0.20710.0\lib\net40\System.Net.Http.dll</HintPath>
4645
</Reference>
@@ -81,7 +80,7 @@
8180
<None Include="packages.config">
8281
<SubType>Designer</SubType>
8382
</None>
84-
<None Include="CollectionJson.Server..nuspec">
83+
<None Include="CollectionJson.Server.nuspec">
8584
<SubType>Designer</SubType>
8685
</None>
8786
</ItemGroup>

src/CollectionJson.Server/CollectionJson.Server..nuspec renamed to src/CollectionJson.Server/CollectionJson.Server.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@
1818
</dependencies>
1919
</metadata>
2020
<files>
21-
<file src="bin\Release\WebApiContrib.Formatting.CollectionJson.Server.dll" target="lib\net45\WebApiContrib.Formatting.CollectionJson.Server.dll" />
21+
<file src="bin\Release\CollectionJson.Server.dll" target="lib\net45" />
2222
</files>
2323
</package>

test/CollectionJson.Client.Tests/CollectionJson.Client.Tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@
7474
</Reference>
7575
</ItemGroup>
7676
<ItemGroup>
77+
<Compile Include="CollectionJsonContentTest.cs">
78+
<SubType>Code</SubType>
79+
</Compile>
80+
<Compile Include="CollectionJsonContractResolverTest.cs" />
7781
<Compile Include="CollectionJsonFormatterTest.cs" />
7882
<Compile Include="Properties\AssemblyInfo.cs" />
7983
<Compile Include="ReadDocumentExtensionsTest.cs" />
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using Should;
8+
using Xunit;
9+
10+
namespace CollectionJson.Client.Tests
11+
{
12+
public class CollectionJsonContentTest
13+
{
14+
[Fact]
15+
public async void WhenCreatingCollectionJsonContentObjectIsSerializedToCollectionJson()
16+
{
17+
var coll = new Collection();
18+
var content = new CollectionJsonContent(coll);
19+
var stream = new MemoryStream();
20+
await content.CopyToAsync(stream);
21+
var reader = new StreamReader(stream);
22+
stream.Position = 0;
23+
var json = reader.ReadToEnd();
24+
json.ShouldContain("\"collection\"");
25+
}
26+
}
27+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Should;
7+
using Xunit;
8+
using System.Reflection;
9+
using Newtonsoft.Json.Serialization;
10+
using Newtonsoft.Json;
11+
12+
namespace CollectionJson.Client.Tests
13+
{
14+
public class CollectionJsonContractResolverTest
15+
{
16+
private CollectionJsonContractResolver _resolver = new CollectionJsonContractResolver();
17+
private Func<MemberInfo, CollectionJsonContractResolver, MemberSerialization, JsonProperty> _createProperty;
18+
private PropertyInfo _itemsProperty;
19+
private PropertyInfo _versionProperty;
20+
21+
public CollectionJsonContractResolverTest()
22+
{
23+
var method = _resolver.GetType().GetMethod("CreateProperty", BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.NonPublic);
24+
_createProperty = (m, r, s) =>
25+
{
26+
return (JsonProperty) method.Invoke(r, new object[] {m, s});
27+
};
28+
var collType = typeof(Collection);
29+
_itemsProperty = collType.GetProperty("Items", BindingFlags.Instance | BindingFlags.Public);
30+
_versionProperty = collType.GetProperty("Version", BindingFlags.Instance | BindingFlags.Public);
31+
}
32+
33+
34+
[Fact]
35+
public void WhenPropertyIsListAndItHasNoItemsItWillNotBeSerialized()
36+
{
37+
var collection = new Collection();
38+
var prop = _createProperty(_itemsProperty, _resolver, MemberSerialization.OptOut);
39+
prop.ShouldSerialize(collection).ShouldBeFalse();
40+
}
41+
42+
[Fact]
43+
public void WhenPropertyIsListAndIsHasItemsItWillBeSerialized()
44+
{
45+
var collection = new Collection();
46+
collection.Items.Add(new Item());
47+
var prop = _createProperty(_itemsProperty, _resolver, MemberSerialization.OptOut);
48+
prop.ShouldSerialize(collection).ShouldBeTrue();
49+
}
50+
51+
[Fact]
52+
public void WhenPropertyIsNotListItWillBeSerialized()
53+
{
54+
var collection = new Collection();
55+
var prop = _createProperty(_versionProperty, _resolver, MemberSerialization.OptOut);
56+
prop.ShouldSerialize(collection).ShouldBeTrue();
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)