Reading CollectionJson in a Web Api Project

I have a project similar (almost identical) to the Conference API Project , which takes a similar approach to the specified project to return the CollectionJson content. I am having difficulty setting the Collection ReadDocument property (line 30) since it does not have a customizer. I could work around this issue by doing the following change

public CollectionJsonContent(Collection collection)
{
    var serializerSettings = new JsonSerializerSettings
        {
            NullValueHandling = NullValueHandling.Ignore,
            Formatting = Newtonsoft.Json.Formatting.Indented,
            ContractResolver = new CamelCasePropertyNamesContractResolver()
        };
    collection.Version = "1.0";

    Headers.ContentType = new MediaTypeHeaderValue("application/vnd.collection+json");

    using (var writer = new JsonTextWriter(new StreamWriter(_memoryStream)){CloseOutput = false})
    {
        //var readDocument = new ReadDocument(); {IReadDocument.Collection = collection};
        var serializer = JsonSerializer.Create(serializerSettings);
        serializer.Serialize(writer,collection);
        writer.Flush();
    }
    _memoryStream.Position = 0;
}

Although it is above compiling the code and to some extent selects the problem, but again, I will have another problem of the inability to consume JsonCollection content in my controller module tests. Consider the following unit test code snippet:

            using (var request = CreateRequest())
            {                
                var controller = new TestController(DataService) {Request = request};

                var temp = await controller.ListAsync(gridSearchData, sampleSearchData);

                if ((temp is NotFoundResult) && (sampleCollection.Any()))
                {
                    Assert.Fail("Controller did not return any result but query did");
                }

                var json = await temp.ExecuteAsync(cancellationTokenSource);



                var readDocument = json.Content.ReadAsAsync<ReadDocument>(new[] {new CollectionJsonFormatter()}, cancellationTokenSource).Result;

         }

ReadDocument, readDocument , . JsonCollection WEB API?

, Conference Web Api .

+4
3

, . Collection .

release 0.7.0 , , , .

( , )

+1

, ReadDocument, (Collection), ReadDocument.

ReadDocument

"{"Collection": [1,2,3,4,5] }"

,

"[1,2,3,4,5]"

class SerializableReadDocument
{
   public Collection Collection { get; set; }
}

using (var writer = new JsonTextWriter(new StreamWriter(_memoryStream)){CloseOutput = false})
{
    var readDocument = new SerializableReadDocument() { Collection = collection };
    var serializer = JsonSerializer.Create(serializerSettings);
    serializer.Serialize(writer, readDocument);
    writer.Flush();
}

, , ReadDocument Collection, , ReadDocument Collection.

SerializableReadDocument, , .

0

I study this and come up with a solution this weekend that will either make it a public setter or make the setter internal, and have a public ctor that accepts the collection.

Sorry for the difficulty.

0
source

All Articles