JSON Encoding Vs. Marshalling

Hi there,

I have some questions regarding the differences between JSON Encoding and JSON Marshalling.

Here is my understanding so far:

  • Encoding takes any data type as input, store in the stream type object
  • Marshal takes any data type as input, store in the byte slice type object

However, I am wondering the advantage for each of them, and their use cases. If the data will be transfer to the byte slice at the end, will it make any difference by using the encoding method? Also, for marshalling, if I want to transfer different type into byte slice, is it recommended to use marshal for doing that?


So, I use Encoder and decoder because in my case I need read data from a slice of a stream and convert it into a struct, it’s simple for me,

  • Marshal => String
  • Encode => Stream

This post is interesting about that

these Encoder and Decoder types can be used in a broad range of scenarios, such as reading and writing to HTTP connections, WebSockets, or files.

This article is interesting too

Thank you for your help. I have another question about it. For json.Marshal, is its purpose to transfer any data type into byte slice? For example, in general, I want to transfer a data structure into string format, is it appropriate or recommended to directly use json.Marshal?


This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.