Created
November 27, 2013 16:46
-
-
Save darrelmiller/7678995 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public class CollectionJsonContent : HttpContent | |
{ | |
private readonly MemoryStream _memoryStream = new MemoryStream(); | |
public CollectionJsonContent(Collection collection) | |
{ | |
var serializerSettings = new JsonSerializerSettings | |
{ | |
NullValueHandling = NullValueHandling.Ignore, | |
Formatting = Newtonsoft.Json.Formatting.Indented, | |
ContractResolver = new CamelCasePropertyNamesContractResolver() | |
}; | |
using (var writer = new JsonTextWriter(new StreamWriter(_memoryStream)){CloseOutput = false}) | |
{ | |
var serializer = JsonSerializer.Create(serializerSettings); | |
serializer.Serialize(writer,collection); | |
writer.Flush(); | |
} | |
_memoryStream.Position = 0; | |
} | |
protected override Task SerializeToStreamAsync(Stream stream, TransportContext context) | |
{ | |
return _memoryStream.CopyToAsync(stream); | |
} | |
protected override bool TryComputeLength(out long length) | |
{ | |
length = _memoryStream.Length; | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment