英文:
MarshalJSON on array of structs with non standard field in Go
问题
我有以下类型:
type IPFilePair struct {
IP net.IP
FileName string
}
type IPFilePairs []*IPFilePair
我试图使用json.Marshal(sample_ipfilepairs)
将其转换为JSON,但是因为IP
不是字符串,它将其转换为一些奇怪的东西。
如何正确地将输出的IP
作为字符串转换为JSON?
英文:
I have the following types:
type IPFilePair struct {
IP net.IP
FileName string
}
type IPFilePairs []*IPFilePair
I'm trying to marshal the JSON of this using json.Marshal(sample_ipfilepairs)
but because IP
isn't a string, it changes it into something strange.
What is the proper way to make the JSON of this output IP
as a string?
答案1
得分: 10
我认为,如果你可以访问IPFilePair
的定义,那么创建一个本地的net.IP
的typedef,并在其中添加MarshanJSON()
是一个可行的方法:
package main
import (
"encoding/json"
"net"
"fmt"
)
type netIP net.IP
type IPFilePair struct {
IP netIP
FileName string
}
type IPFilePairs []*IPFilePair
func (ip netIP) MarshalJSON() ([]byte, error) {
return json.Marshal(net.IP(ip).String())
}
func main() {
pair1 := IPFilePair{netIP{127, 0, 0, 1}, "file1"}
pair2 := IPFilePair{netIP{127, 0, 0, 2}, "file2"}
sample_ipfilepairs := IPFilePairs{&pair1, &pair2}
b, _ := json.Marshal(sample_ipfilepairs)
fmt.Println(string(b))
}
这将输出:
[{"IP":"127.0.0.1","FileName":"file1"},{"IP":"127.0.0.2","FileName":"file2"}]
当然,如果你需要将其反序列化回相同的Go数据结构,你需要在netIP
上使用net.ParseIP
实现UnmarshalJSON()
。
我当然很好奇是否有更简单的方法来实现这个。
英文:
I think if you have access to the definition of IPFilePair
, creating local typedef of net.IP
that you add MarshanJSON()
to is the way to go:
package main
import (
"encoding/json"
"net"
"fmt"
)
type netIP net.IP
type IPFilePair struct {
IP netIP
FileName string
}
type IPFilePairs []*IPFilePair
func (ip netIP) MarshalJSON() ([]byte, error) {
return json.Marshal(net.IP(ip).String())
}
func main() {
pair1 := IPFilePair{netIP{127, 0, 0, 1}, "file1"}
pair2 := IPFilePair{netIP{127, 0, 0, 2}, "file2"}
sample_ipfilepairs := IPFilePairs{&pair1, &pair2}
b, _ := json.Marshal(sample_ipfilepairs)
fmt.Println(string(b))
}
This outputs:
[{"IP":"127.0.0.1","FileName":"file1"},{"IP":"127.0.0.2","FileName":"file2"}]
Of course, if you ever need to unmarshal that back into the same Go data structure, you'll want to implement UnmarshalJSON()
on netIP
using net.ParseIP
.
I'm certainly curious if anyone knows of an easier way to accomplish this.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论