public override Task WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
        {
            IServiceProvider serviceProvider = context.HttpContext.RequestServices;

            _configuration = serviceProvider.GetService(typeof(IProvideHalTypeConfiguration)) as IProvideHalTypeConfiguration;

            var hal = BuildHypermedia(context.Object, context.HttpContext);

            var response = context.HttpContext.Response;

            string jsonOfTest = JsonConvert.SerializeObject(hal, _jsonSerializerSettings);

            return(response.WriteAsync(jsonOfTest));
        }
示例#2
0
        private JObject Serialize(object model, IProvideHalTypeConfiguration config, NancyContext context = null)
        {
            if (context == null)
            {
                context = new NancyContext();
            }

            var processor = new HalJsonResponseProcessor(config, new[] { JsonSerializer });
            var response  = (JsonResponse)processor.Process(new MediaRange("application/hal+json"), model, context);
            var stream    = new MemoryStream();

            response.Contents.Invoke(stream);
            stream.Seek(0, SeekOrigin.Begin);
            var text = new StreamReader(stream).ReadToEnd();

            Console.WriteLine(text);
            return(JObject.Parse(text));
        }
 public HalJsonResponseProcessor(IProvideHalTypeConfiguration configuration, IEnumerable<ISerializer> serializers)
 {
     this.configuration = configuration;
     serializer = serializers.FirstOrDefault(x => x.CanSerialize("application/json")); //any json serializer will do
 }
示例#4
0
 public HalJsonResponseProcessor(IProvideHalTypeConfiguration configuration, IEnumerable <ISerializer> serializers)
 {
     this.configuration = configuration;
     serializer         = serializers.FirstOrDefault(x => x.CanSerialize("application/json")); //any json serializer will do
 }
        private JObject Serialize(object model, IProvideHalTypeConfiguration config, NancyContext context = null)
        {
            if (context == null) context = new NancyContext();

            var processor = new HalJsonResponseProcessor(config, new[] { JsonSerializer });
            var response = (JsonResponse)processor.Process(new MediaRange("application/hal+json"), model, context);
            var stream = new MemoryStream();
            response.Contents.Invoke(stream);
            stream.Seek(0, SeekOrigin.Begin);
            var text = new StreamReader(stream).ReadToEnd();

            Console.WriteLine(text);
            return JObject.Parse(text);
        }