如何对空字符串进行编码/解码?

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

How to encode/decode a empty string

问题

我正在使用GOB编码进行我的项目,并且我发现(经过长时间的斗争)空字符串没有被正确地编码/解码。在我的代码中,我使用一个错误消息(字符串)来报告任何问题,这个错误消息大部分时间都是空的。如果我编码一个空字符串,它会变成空值,这在解码时给我带来了问题。我不想改变编码/解码,因为这些部分被使用得最多。
我该如何告诉Go如何编码/解码空字符串?

示例:
Playground 正常工作的代码。
Playground 不正常工作的代码。

英文:

I am using the GOB encoding for my project and i figured out (after a long fight) that empty strings are not encoded/decoded correctly. In my code i use a errormessage (string) to report any problems, this errormessage is most of the time empty. If i encode a empty string, it become nothing, and this gives me a problem with decoding. I don't want to alter the encoding/decoding because these parts are used the most.
How can i tell Go how to encode/decode empty strings?

Example:
Playground working code.
Playground not working code.

答案1

得分: 1

问题不在于encoding/gob模块,而是你为Msg声明的自定义MarshalBinary/UnmarshalBinary方法,这些方法无法正确地往返空字符串。在这里,你有两种选择:

  1. 摒弃MarshalBinary/UnmarshalBinary方法,依赖GOB对结构体的默认编码。但仅仅进行这个改变是不够的,因为结构体的字段并没有被导出。如果你愿意导出这些字段,那么这是最简单的选择:https://play.golang.org/p/rwzxTtaIh2

  2. 使用能够正确往返空字符串的编码方式。一个简单的选择是使用GOB本身来编码结构体字段:

     func (m Msg) MarshalBinary() ([]byte, error) {
     	var b bytes.Buffer
     	enc := gob.NewEncoder(&b)
     	if err := enc.Encode(m.x); err != nil {
     		return nil, err
     	}
     	if err := enc.Encode(m.y); err != nil {
     		return nil, err
     	}
     	if err := enc.Encode(m.z); err != nil {
     		return nil, err
     	}
     	return b.Bytes(), nil
     }
    
     // UnmarshalBinary modifies the receiver so it must take a pointer receiver.
     func (m *Msg) UnmarshalBinary(data []byte) error {
     	dec := gob.NewDecoder(bytes.NewBuffer(data))
     	if err := dec.Decode(&m.x); err != nil {
     		return err
     	}
     	if err := dec.Decode(&m.y); err != nil {
     		return err
     	}
     	return dec.Decode(&m.z)
     }
    

你可以在这里尝试这个示例:https://play.golang.org/p/oNXgt88FtK

第一种选择显然更简单,但如果你的实际示例稍微复杂一些,第二种选择可能更有用。不过要小心使用自定义编码器:GOB包含了一些用于检测不兼容性的特性(例如,如果你向结构体添加一个字段并尝试解码旧数据),而这些特性在自定义编码中是缺失的。

英文:

The problem isn't the encoding/gob module, but instead the custom MarshalBinary/UnmarshalBinary methods you've declared for Msg, which can't correctly round trip an empty string. There are two ways you could go here:

  1. Get rid of the MarshalBinary/UnmarshalBinary methods and rely on GOB's default encoding for structures. This change alone wont' be enough because the fields of the structure aren't exported. If you're happy to export the fields then this is the simplest option: https://play.golang.org/p/rwzxTtaIh2

  2. Use an encoding that can correctly round trip empty strings. One simple option would be to use GOB itself to encode the struct fields:

     func (m Msg) MarshalBinary() ([]byte, error) {
     	var b bytes.Buffer
     	enc := gob.NewEncoder(&b)
     	if err := enc.Encode(m.x); err != nil {
     		return nil, err
     	}
     	if err := enc.Encode(m.y); err != nil {
     		return nil, err
     	}
     	if err := enc.Encode(m.z); err != nil {
     		return nil, err
     	}
     	return b.Bytes(), nil
     }
    
     // UnmarshalBinary modifies the receiver so it must take a pointer receiver.
     func (m *Msg) UnmarshalBinary(data []byte) error {
     	dec := gob.NewDecoder(bytes.NewBuffer(data))
     	if err := dec.Decode(&m.x); err != nil {
     		return err
     	}
     	if err := dec.Decode(&m.y); err != nil {
     		return err
     	}
     	return dec.Decode(&m.z)
     }
    

You can experiment with this example here: https://play.golang.org/p/oNXgt88FtK

The first option is obviously easier, but the second might be useful if your real example is a little more complex. Be careful with custom encoders though: GOB includes a few features that are intended to detect incompatibilities (e.g. if you add a field to a struct and try to decode old data), which are missing from this custom encoding.

huangapple
  • 本文由 发表于 2014年12月4日 13:34:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/27287008.html
匿名

发表评论

匿名网友

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

确定