英文:
How to write a custom unmarshaller for AWS ION?
问题
我正在使用Amazon ION来对接收自各种AWS服务的数据进行编组和解组。
我需要编写一个自定义的解组函数,并在Amazon ION的官方文档中找到了一个实现方法的示例,请参见这里。
使用上述示例,我编写了以下代码:
package main
import (
"bytes"
"fmt"
"github.com/amzn/ion-go/ion"
)
func main() {
UnmarshalCustomMarshaler()
}
type unmarshalMe struct {
Name string
custom bool
}
func (u *unmarshalMe) UnmarshalIon(r ion.Reader) error {
fmt.Print("UnmarshalIon called")
u.custom = true
return nil
}
func UnmarshalCustomMarshaler() {
ionBinary, err := ion.MarshalBinary(unmarshalMe{
Name: "John Doe",
})
if err != nil {
fmt.Println("Error marshalling ion binary: ", err)
panic(err)
}
dec := ion.NewReader(bytes.NewReader(ionBinary))
var decodedResult unmarshalMe
ion.UnmarshalFrom(dec, &decodedResult)
fmt.Println("Decoded result: ", decodedResult)
}
问题: 上述代码不按预期工作。UnmarshalIon函数没有被调用,但根据文档应该会被调用。我做错了什么?
英文:
I'm using Amazon ION to marshal and unmarshal data received from various AWS services.
I need to write a custom unmarshal function, and I found an example of how thats achieved in Amazon ION's official docs, see here
Using the above example, I have written below code:
package main
import (
"bytes"
"fmt"
"github.com/amzn/ion-go/ion"
)
func main() {
UnmarshalCustomMarshaler()
}
type unmarshalMe struct {
Name string
custom bool
}
func (u *unmarshalMe) UnmarshalIon(r ion.Reader) error {
fmt.Print("UnmarshalIon called")
u.custom = true
return nil
}
func UnmarshalCustomMarshaler() {
ionBinary, err := ion.MarshalBinary(unmarshalMe{
Name: "John Doe",
})
if err != nil {
fmt.Println("Error marshalling ion binary: ", err)
panic(err)
}
dec := ion.NewReader(bytes.NewReader(ionBinary))
var decodedResult unmarshalMe
ion.UnmarshalFrom(dec, &decodedResult)
fmt.Println("Decoded result: ", decodedResult)
}
Problem: The above code does not work as expected. The UnmarshalIon function is not called, but according to docs is should get called. What am I doing wrong?
答案1
得分: 1
你可能正在使用的是默认的v1.1.3版本,该版本不包含该功能。
英文:
You are probably using the v1.1.3, the one by default which does not contain the feature.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论