select navigate esc close

JSON - The Fine Print: Part 2 - Emitting JSON

Ardan Labs Blog on ·

Introduction

In part 1 we took a high-level view on serialization and JSON. In this part, we’ll roll our sleeves and start working with JSON, focused on emitting JSON. You might think this is a basic topic, but there is much more to it than just calling json.Marshal.

json.Marshal vs json.Encoder

The encoding/json package has two main APIs: Marshal and NewEncoder. The Marshal function returns a []byte while the NewEncoder function will write to an io.Writer. The question is: When should you use one API over the other?