Home > database >  How to write a custom unmarshaller for AWS ION?
How to write a custom unmarshaller for AWS ION?

Time:01-04

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?

CodePudding user response:

You are probably using the v1.1.3, the one by default which does not contain the feature.

  • Related