Skip to content

Instantly share code, notes, and snippets.

@darrelmiller
Created September 10, 2014 02:57
Show Gist options
  • Save darrelmiller/6fb8207b19b1b912faa1 to your computer and use it in GitHub Desktop.
Save darrelmiller/6fb8207b19b1b912faa1 to your computer and use it in GitHub Desktop.
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 CamelCasePropertyNamesContractResolver()
});
collection.Version = "1.0";
_readDocument = new ReadDocument(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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment