sarama icon indicating copy to clipboard operation
sarama copied to clipboard

stringEncoder how to decode to json/map/struct/ ?

Open 825644691 opened this issue 2 years ago • 1 comments

msg := &sarama.ProducerMessage{} msg.Topic = "aaa" item := {'aa':aa,'bb':bb} (struct) res,_ := json.Marshal(item) msg.Value = sarama.StringEncoder(res)

has any way decode the StringEncoder to json/struct/map?

825644691 avatar Apr 13 '22 01:04 825644691

@825644691 I don't understand the question? Here you've essentially populated a ProducerMessage with a string value containing the json body that you want to publish via Sarama. You've (correctly) used the StringEncoder type alias so the producer has the Encode() func available via the receiver

When you consume that message with a Consumer, you'll just get a []byte containing your string value, you can decode that back to struct as usual (json.Unmarshal)

https://github.com/Shopify/sarama/blob/095b45ab2157e80e41799e606b09c6c4dab3f5b6/utils.go#L68-L78

dnwe avatar May 11 '22 21:05 dnwe

Thank you for taking the time to raise this issue. However, it has not had any activity on it in the past 90 days and will be closed in 30 days if no updates occur. Please check if the main branch has already resolved the issue since it was raised. If you believe the issue is still valid and you would like input from the maintainers then please comment to ask for it to be reviewed.

github-actions[bot] avatar Aug 18 '23 00:08 github-actions[bot]

Closing as presumed to have been resolved. Please reopen if not

dnwe avatar Aug 18 '23 06:08 dnwe