Created
September 6, 2014 17:07
-
-
Save darrelmiller/95d978bf0119c8723559 to your computer and use it in GitHub Desktop.
Specialized HttpContent class for CollectionJson
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 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