enmime icon indicating copy to clipboard operation
enmime copied to clipboard

Unable to Json.Marshal an Envelope object

Open jnury opened this issue 5 years ago • 3 comments

What I did: json.Marshal(envelope)

What I expected: convert an Envelope object into Json

What I got: a stack overflow when envelope contains at least one part

Release or branch I am using: master

Please find attached a file to reproduce. As I understand the problem, there is an infinite loop while walking the Envelope object as we can go to Envelope.Root.FirstChild.Parent.FirstChild.Parent en so one.

Would it be possible to create a func (e *Envelope) ToJson() (string, error) function ? Or instruct Json.Marshal to not walk thru the Parent property ?

Thanks for your help

enmime_json_marshal.go.txt

jnury avatar Jan 22 '20 20:01 jnury

I think this is worth fixing, thanks for letting us know.

jhillyerd avatar Jan 23 '20 20:01 jhillyerd

How do you envision this working @jhillyerd implementing the encoding/json MarshallJSON interface, or having it as a method function as suggested above?

Also what format do we want? Started putting this together as a jumping off point:

HEADER{
    "key": "",
    "value": ""
}

PART{
    "boundary": "",
    "headers": HEADER[],
    "content": ""
}

ENVELOPE{
    "headers": HEADER[],
    "text": "",
    "html": "",
    "attachments": PART[],
    "inlines": PART[]
}

jawr avatar Apr 04 '21 06:04 jawr

There would be a lot of duplicated information if were to encode both the envelope, and the entire part tree to JSON. I suspect most users would just want the envelope content.

Implementing the MarshallJSON interface would be the path of least surprise, and should let Envelope be embedded into other structs for serialization. Given that, I'd like to match the default attribute naming of Go as much as possible, even though I don't love the way Go does it. :)

Not sure if useful, but this is what one of my other projects outputs when serializing an email parsed by enmime: https://github.com/inbucket/inbucket/wiki/REST-GET-message

jhillyerd avatar Apr 04 '21 15:04 jhillyerd