Skip to content

Instantly share code, notes, and snippets.

@darrelmiller
Created November 27, 2013 16:46
Show Gist options
  • Save darrelmiller/7678995 to your computer and use it in GitHub Desktop.
Save darrelmiller/7678995 to your computer and use it in GitHub Desktop.
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