- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在尝试将下面的结构序列化为 byte[]
以将其存储到 DB 中,然后在从 DB 读取它时反序列化它。
type Response struct {
Headers map[string][]string
Body io.Reader
Status int
}
下面是我如何创建响应对象并为其设置值的代码。
resp := new(Response)
resp.Body = bytes.NewReader(outBytes) //outBytes is byte[]
resp.Headers.SetKeyValue("Content-Type", "text/json") //SetKeyValue is the method created for adding headers
resp.Headers.SetKeyValue("Url-Type", "broker")
resp.Status = 200
我正在使用 json.Marshal()
来序列化 resp
对象,如下所示。
b, _ := json.Marshal(resp)
下面是我用来反序列化的代码。
var r Response
r.Body = &bytes.Buffer{}
json.Unmarshal(b,&r)
问题在于反序列化,我无法获取 resp.Body
对象。尽管设置了正文对象(见上文),它始终为 nil
或空白。我能够从反序列化中获取结构的 Headers
和 Status
字段,但不能获取 Body
。
我知道 Body
字段需要处理一些事情,它是一个 io.Reader
。
任何帮助都会很棒。
最佳答案
简答:JSON 编码器不会使用 Read()
函数从 io.Reader 读取字符串。您可以使用实现 Marshaler
接口(interface)的类型,而不是使用 io.Reader
。
Marshaller 的工作原理:Marshal递归遍历值v。如果遇到的值实现了 Marshaler
接口(interface)并且不是 nil 指针,Marshal 将调用其 MarshalJSON
方法来生成 JSON。如果不存在 MarshalJSON 方法但该值实现了 encoding.TextMarshaler
,Marshal 将调用其 MarshalText
方法。 nil 指针异常不是严格必要的,而是模仿 UnmarshalJSON 行为中类似的必要异常。
否则,Marshal 使用以下依赖于类型
的默认编码:
实现这是你可以做的
type Response struct {
Headers map[string][]string
Body *JSONReader
Status int
}
type JSONReader struct {
*bytes.Reader
}
func NewJSONReader(outBytes []byte) *JSONReader {
jr := new(JSONReader)
jr.Reader = bytes.NewReader(outBytes)
return jr
}
func (js JSONReader) MarshalJSON() ([]byte, error) {
data, err := ioutil.ReadAll(js.Reader)
if err != nil {
return nil, err
}
data = []byte(`"` + string(data) + `"`)
return data, nil
}
// UnmarshalJSON sets *jr to a copy of data.
func (jr *JSONReader) UnmarshalJSON(data []byte) error {
if jr == nil {
return errors.New("json.JSONReader: UnmarshalJSON on nil pointer")
}
if data == nil {
return nil
}
data = []byte(strings.Trim(string(data), "\""))
jr.Reader = bytes.NewReader(data)
return nil
}
这是一个带有实现和示例使用的 go playground 链接:link
关于serialization - io.Reader 无法序列化的 Golang 结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41393573/
class ReadLock { private: std::mutex readWriteMutex; std::mutex conditionmtx; std::condi
我在 utf-8 编码文件中有多个 messages.properties 文件(messages_en_US.properties、messages_fr.properties,...)。在这些属性
我正在尝试从 google reader api 中检索单个选定项目。是否可以通过 API 调用通过 ID 获取项目,或者我是否必须访问该项目提要并从那里获取它? 最佳答案 您可以使用 POST 到
好的,所以我有一个应用程序可以与 GR 的“api”紧密结合。 一切正常,但最近我收到来自 Google 的许可被拒绝返回。如果我退出 GR 并使用我的应用程序重新登录,一切都会重新开始。这让我相信
我想要的是最终得到类似的东西: public class InterleavedBufferedReader extends BufferedReader { ... } 并将其用作: Reader[
reader monad 有一个asks 函数,它的定义与reader 函数完全相同,为什么它作为一个单独的函数存在,与 的定义相同读者?为什么不总是使用阅读器? class Monad m => M
当使用csv模块读取文件时,有两种方法可以遍历csv.reader返回的生成器。 with open('foo.csv') as f: reader = csv.reader(f) r
我想在 Go 中按照 here 中的要求做同样的事情. 我正在解析一个巨大的日志文件,我需要逐行解析它。在每一行上,我将该行反序列化为一个结构。数据可能来自任何数据源(文件、网络等)。因此,我在我的函
我在golang的zlib/reader.go文件中找到了很多像r.(flate.Reader)这样的代码片段。这是什么意思? https://golang.org/src/compress/zlib
我正在 Spring MVC 中包装 Freemarker 模板加载器,如所述 here在 html 页面中进行默认转义。 所以,我需要用我的字符串包装来自 java.io.Reader 的内容,而不
为什么这个 PDF 在 Foxit Reader 而不是 Adobe Reader 中显示签名? 这是来自 Syncfusion PDF library 的代码用于生成它(另请参阅有关 signi
我有一个巨大的tbb::concurrent_unordered_map被多个(~60)线程同时“大量读取”。 我每天需要清除一次(完全清除或选择性清除)。在 tbb 中删除显然不是线程安全的实现,因
好像是 Hibernate.createClob(Reader reader, int length)在 3.6.x 版本中已弃用 它建议使用使用 LobHelper.createClob(Reade
这是我的实际解决方案 private def transpose[E, A](readers : Seq[Reader[E, A]]) : Reader[E, Seq[A]] = Read
DataReader[0].ToString() 和 (string)DataReader[0] 有区别吗? 我的猜测是,如果数据库类型不是字符串类型,(string)DataReader[0] 可能
我想制作一个 C# 程序来保存 pdf 和 djvu 文件的书签。如何从 AcroRd32/DjVuReader 进程中找出当前页码? 最佳答案 您可以通过 Adobe Acrobat 支持的 D
什么更好 var s = (string)reader[0] 或 var s = Convert.ToString(reader[0]) ? 最佳答案 我会说 reader.GetString(0
我对非官方阅读器 api 进行了大量研究,并筛选了其他问题,但没有一个完全满足我的要求。如果您知道文章 id 有据可查,如何分享文章,但如果您不知道 id,我想知道如何分享文章(即如何转换 url -
这是一个简单的示例,用于将 xml 文件读入 WebRowSet 对象,然后将数据从该对象加载到数据库。 import javax.sql.rowset.RowSetProvider; import
这样的转换对于任何仿函数都是可能的,不仅是Future: implicit class RichFunctorReader[F[_]: Functor, A, B](fr: F[Reader[A, B
我是一名优秀的程序员,十分优秀!