liveBook · Manning

Deserialization

As explained in Go in Action, Second Edition

Unmarshaling is the process of reconstructing an in-memory value from a serialized representation.

Deserialization reconstructs a Go value from a stored or transmitted representation. It lets an application read structured input—such as a configuration file—into types it can inspect and validate. Go’s encoding packages provide format-specific tools, including encoding/json and encoding/xml. section 10.5 section 10.11

For JSON, define a struct with exported fields, then use `json` tags where JSON key names differ from Go field names. Call `json.Unmarshal` with the input bytes and a pointer to the destination value; check its returned error before using the result. Unmarshaling checks JSON values against Go field types and handles nested structures. section 10.5.1

Read a server configuration

Given JSON containing `"name"`, `"debug"`, and `"port"`, a matching destination can be defined and populated as follows: ```go type Config struct { Name string `json:"name"` Debug bool `json:"debug"` Port int `json:"port"` } var cfg Config if err := json.Unmarshal(data, &cfg); err != nil { return err } ``` Here, `data` is the configuration file’s JSON as a byte slice. A separate validation step can check whether the resulting values are sensible for the application. section 10.5.1

When decoding needs domain-specific behavior, a type can implement `UnmarshalJSON`. One pattern decodes into a temporary value, validates a field, and assigns the completed result only after validation succeeds. This keeps format parsing and application rules together without treating a successful type match as sufficient validation. section 10.5.1 section 10.11

Listing 10.17 Defining a configuration structure with JSON tags
// Config represents the structure of a JSON configuration file
type Config struct {
    Name      string `json:"name"` #1
    Version   string `json:"version"`
    Debug     bool   `json:"debug"` #2
    Port      int    `json:"port"` #3
    Host      string `json:"host,omitempty"` #4
    LogLevel  string `json:"-"` #5
}

Inspect the configuration struct’s exported fields and JSON tags to see how input keys map to a Go value. section 10.5.1

How Go in Action, Second Edition covers it

How deep it goes
Works through JSON struct mapping, pointer-based unmarshaling, type checks, and custom validation without descending into decoder internals.
Examples
Uses a server configuration JSON file, its tagged Go struct, and Host validation through a custom unmarshaler.
What it assumes
Assumes familiarity with Go structs, pointers, and byte slices while explaining exported-field and JSON-tag requirements.
What you can do after
Apply it via encoding/json to decode configuration bytes into typed values and add domain-specific validation.
How it connects
Links unmarshaling to struct tags, exported fields, custom UnmarshalJSON methods, and file I/O.

Also explained in

Compare how 10 books explain deserialization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Action, Second Edition. The section links point to the parts of the book it draws on.