Encoding and Decoding
Introduction
Encoding and decoding are used in many forms of communications, including computing, data communications, programming, digital electronics and human communications. These two processes involve changing the format of content for optimal transmission or storage.
Encoding is the process of putting a sequence of characters into a serialized format for efficient transmission or storage.
Decoding is the opposite process, the conversion of an encoded format back into the original sequence of characters.
Implementation Of Encoding-Decoding
JSON is a widely used format for data interchange. Golang provides multiple encoding and decoding packages to work with JSON including to and from built-in and custom data types using the encoding/json
package.
What is Struct Tag ?
A struct tag is additional metadata information inserted into struct fields. The metadata can be acquired through reflection. Struct tags usually provide instructions on how a struct field is encoded to or decoded from a format. Json object is a collection of key-value pairs. While encoding, struct tags acts as key for a particular value of their corresponding field in structure. Without, struct tags encoder would not be able to map struct values to their corresponding keys and generates an error.
Struct tags are used in popular packages including:
- encoding/json
- encoding/xml
- gopkg.in/mgo.v2/bson
- gorm.io/gorm
- github.com/gocarina/gocsv
- gopkg.in/yaml.v2
The example uses struct tags to configure how JSON data is encoded.
type User struct {
Id int `json:"id"`
Name string `json:"name"`
}
With json:"id"
struct tag, we encode the Id
field in lowercase.
Implementation of ENCODING method
Let us define Response structure.
type UserResponse struct {
ID int `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
}
In the request handler, json.NewEncoder
create an encoder with ResponseWriter, and call the encode
method. NewEncoder
returns an encoder object that is used to encode the data into JSON. The purpose of encoding is to transform data so that it can be properly (and safely) consumed by a different type of system, e.g. binary data being sent over email, or viewing special characters on a web page. The goal is not to keep information secret, but rather to ensure that it's able to be properly consumed.
Syntax func (enc *Encoder) Encode(v interface{}) error
func handler(w http.ResponseWriter, r *http.Request) {
resp := &UserResponse{
ID: 123,
Name: req.Name,
Email: req.Email,
}
err := json.NewEncoder(w).Encode(resp)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
}
Output:
{
id: 1
name: ABC
email: abc@google.com
}
Implementation of DECODING method
Input:
{
"name":"ABC",
"email":"abc@google.com"
}
Define request parameters: First, you define a structure that represents the request parameters.
type UserRequest struct {
Name string `json:"name"`
Email string `json:"email"`
}
In the request handler, json.NewDecoder
create a decoder and call the decode
method with the pointer of the structure created above. It uses json/encoding
library. NewDecoder returns a decoder object that reads from request r
.
The decoder introduces its own buffer and may read data from request object 'r' beyond the JSON values requested. Decode reads the next JSON-encoded value from its input and stores it.
func handler(w http.ResponseWriter, r *http.Request) {
var req UserRequest
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
http.Error(w, err.Error(), 400)
return
}
// process with the request parameters
}
Difference between Encoding-Decoding / Encryption-Decryption
We should not get confused between term encoding-decoding and encryption-decryption.
Encoding/decoding is the process of transforming data into such a format that it can be used by a different type of system using publicly available algorithms. The accessible data can be retrieved using decoding. The main purpose is the protection of the integrity of data. The encoded data is less secure. It can easily be decoded. Example of algorithm: AES, RSA, and Blowfish.
Whereas, Encryption/decryption is the process to encode data securely such that only the authorised user who knows the key or password is able to retrieve the accessible data using decryption. The purpose of encryption/decryption is to transform data to keep it secret from others. The encrypted data is more secure. Example of algorithm: ASCII, UNICODE, URL encoding, Base64.
Conclusion
In this blog, we have explored the process of encoding-decoding, its implementation. Also we have specified the difference between encoding-decoding and encryption-decryption.
Subscribe to my newsletter
Read articles from Gaurav Bharadwaj directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by