-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #87 from WebApiContrib/dev
Release 0.7
- Loading branch information
Showing
14 changed files
with
210 additions
and
9 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
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,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
35
src/CollectionJson.Client/CollectionJsonContractResolver.cs
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,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; | ||
} | ||
|
||
} | ||
} |
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
27 changes: 27 additions & 0 deletions
27
test/CollectionJson.Client.Tests/CollectionJsonContentTest.cs
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,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\""); | ||
} | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
test/CollectionJson.Client.Tests/CollectionJsonContractResolverTest.cs
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,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(); | ||
} | ||
} | ||
} |
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