编码和解码结构体

huangapple go评论92阅读模式
英文:

Encoding and decoding structs of

问题

我正在尝试对结构体进行编码和解码,我已经搜索了很多相关的问题,但大多数问题都是关于编码基本类型或简单结构体的。我想要编码的是一个类似这样的结构体:

type someStruct struct {
	Name string           
	Id   int               
	file *os.File          
	keys *ecdsa.PrivateKey 
}

对于名称和ID,没有问题,我可以使用gob或json进行编码。然而,当我想要编码一个文件时,例如使用gob,我会使用gob.Register(os.File{}),但是我会得到一个错误,提示file没有导出的字段,这是因为file结构体中的字段是小写字母开头的。我会使用以下类似的函数:

func EncodeToBytes(p interface{}) []byte {
	buf := bytes.Buffer{}
	enc := gob.NewEncoder(&buf)
	gob.Register(big.Int{})
...
	err := enc.Encode(&p)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("uncompressed size (bytes): ", len(buf.Bytes()))
	return buf.Bytes()
}

我不确定在编码函数中注册是否正确,但是我觉得我不应该为所有被引用的结构体都注册,这似乎不是正确的方法。例如,对于一个文件,我将不得不注册大量的接口。有没有一种简单的方法来编码和解码具有更复杂结构的结构体?

如果我使用json编码来做这个,如果我使用指向另一个结构体的指针,它将始终返回nil。有没有办法获取我想要的所有信息?

谢谢!

英文:

I'm trying to encode and decode structs, I've searched around quite a bit and a lot of the questions regarding this topic is usually people who want to encode primitives, or simple structs. What I want is to encode a struct that could look like this:

	Name string           
	Id   int               
	file *os.File          
	keys *ecdsa.PrivateKey 
}

The name and the ID is no problem, and I can encode them using either gob or json marshalling. However when I want to encode a file for example using gob, I'd usegob.Register(os.File{}) I get an error that file has no exported fields, due to the fields in the file struct being lower case. I would use a function like this

	buf := bytes.Buffer{}
	enc := gob.NewEncoder(&buf)
	gob.Register(big.Int{})
...
	err := enc.Encode(&p)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("uncompressed size (bytes): ", len(buf.Bytes()))
	return buf.Bytes()
}

I'm not sure if it's correct to register within the encode function, however it seems odd that I have to register all structs that is being referenced to for the one specific struct i want to encode. For example with a file, I would have to register a ton of interfaces, it doesn't seem to be the correct way to do it. Is there a simple way to encode and decode structs that have a bit more complexity.

If I use json marshalling to do this it will always return nil if I use a pointer to another struct. Is there a way to get all the information I want?

Thanks!

答案1

得分: 0

假设你的结构体指向/foo/bar/baz.txt中的一个文件,并且你对结构体进行了序列化。然后你将其发送到另一台计算机(可能是在不同的操作系统中),并重新创建结构体。你期望会发生什么?

如果你对结构体进行序列化,然后删除文件(或更新文件内容),并在同一台计算机上重新创建结构体,会发生什么?

一种解决方案是存储文件的内容。

另一种解决方案是存储文件的路径,在反序列化结构体时尝试重新打开文件。你可以通过存储内容的哈希值、大小和其他元数据来添加安全层,以检查文件是否相同。

答案将指导你选择最佳实现方式。

英文:

Imagine your struct ponts to a file in /foo/bar/baz.txt and you serialize your struct. The you send it to another computer (perhaps in a different operational system) and re-create the struct. What do you expect?

What if you serialize, delete the file (or update the content) and re-create the struct in the same computer?

One solution is store the content of the file.

Another solution is to store the path to the file and, when you deserialize the struct you can try to reopen the file. You can add a security layer by storing the hash of the content, size and other metadata to check if the file is the same.

The answer will guide you to the best implementation

huangapple
  • 本文由 发表于 2022年4月8日 00:00:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/71785405.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定