使用JSON序列化请求对象 [英] Serializing a Request Object using JSON

查看:93
本文介绍了使用JSON序列化请求对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在研究概念验证,并遇到一个涉及使用JSON序列化HttpRequest的问题.

I'm currently working on a proof-of-concept and ran into an issue involving using JSON to serialize an HttpRequest.

背景

最初,我认为我可以使用

I originally thought that I would be able to easily accomplish it using the JSON.Encode() method as seen below :

JSON.Encode(HttpContext.Request)

但是,我很快发现这会引发各种循环引用(主要是由于Request对象的实际结构和复杂性).这些仅在实际遇到包含循环引用的属性时才会发生,因为我以前使用以下代码仅获取我需要的特定元素:

However, I quickly found that this causes all sorts of circular references to be thrown (primarily due to the actual structure and complexity of the Request object). These only occur when actually encountering properties that do contain a circular reference, as I have previously used the following code to grab just specific elements that I need :

JSON.Encode(new {HttpContext.Request.Cookies,HttpContext.Request.Headers, ... });

效果很好.

我只是想知道是否有更好的方法来解决这个问题(或者什么是最好的解决方法).我将在下面详细介绍一些我可能采取的方法,以找出可能出问题的地方.

I'm just curious if there is a better method of handling this (or what the best method of handling it may be). I'll detail a few of the approaches that I have taken so far below to possibly find any areas that I may have gone wrong.

以前的方法

  • 使用反射遍历对象中的每个属性请求并尝试构造JSON字符串逐个属性".(在遇到通函时失败参考)

尝试将每个属性存储在Dictionary对象中,然后使用JSON序列化整个Dictionary(,希望它可以将对象扁平化",并使其更易于序列化)

Attempting to store each of the Properties within a Dictionary object and then using JSON to serialize the entire Dictionary (in hopes that it would "flatten" the object out and make it easier to serialize)

使用 JSON.NET库并尝试对其进行序列化通过JsonConvert.SerializeObject()方法(我尝试传递一些其他设置来避免循环引用,但没有运气)

Using the JSON.NET library and trying to serialize it through the JsonConvert.SerializeObject() method (I have tried to pass in several additional settings to avoid a circular reference, but haven't had any luck)

我最新的方法(使用JSON.NET库)我想快要开始工作了,但是我遇到了一个错误,该错误涉及Request内Stream对象的"Timeout"属性.

My latest approach (using the JSON.NET library) I thought would come close to working, however I encountered an error that involved a "Timeout" property on Stream objects within the Request.

我不反对简单地避免序列化Stream对象和循环引用.我只是想尽可能多地获取Request对象,同时避免出现任何此类不幸事件.

I'm not opposed to simply avoiding serializing Stream objects and Circular References. I'm just trying to grab as much of the Request object as possible while avoiding any of these types of mishaps.

推荐答案

您不使用JSON.Net的任何特殊原因?它上面有一个名为 PreserveReferencesHandling 的设置,该设置用附加属性("$ id":"##")标记对象.如果该对象多次出现在序列化中,则不会再次写入该对象,而是将其替换为"$ ref":"##",指向该对象已存在的json.这样可以避免循环引用.

Any particular reason why you aren't using JSON.Net? It has a setting on it called PreserveReferencesHandling, which tags objects with an additional property ("$id":"##"). If that object appears in the serialization more than once, instead of writing the object again it replaces it with "$ref": "##" pointing to the already existing json for the object. This circumvents circular referencing.

我从来没有尝试过将这种格式的JSON返回到$ .ajax调用,所以我不知道在网络侧解析会涉及到什么.

I've never tried returning JSON in that format to an $.ajax call so I don't know what would be involved in parsing it web-side.

这篇关于使用JSON序列化请求对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆